|
-
Aug 31st, 2000, 10:02 AM
#1
Thread Starter
Frenzied Member
I can't find anything on how to make images black and white! Does anyone here know?
-
Aug 31st, 2000, 10:45 AM
#2
Well I'm sure there's a 1000 ways to do this better but here goes.
Code:
Public Function GetGrayScale(lngColor As Long) As Long
Dim nRed&, nGreen&, nBlue&
nRed = lngColor Mod 256
nBlue = lngColor \ &H10000
nGreen = (lngColor Mod &H10000) \ 256
nRed = (nRed + nBlue + nGreen) \ 3
GetGrayScale = nRed * &H10000 + nRed * &H100 + nRed
End Function
You can loop through your picture and get every pixels color using the GetPixel API. Pass it to GetGrayScale and use the return value as the argument for SetPixel.
-
Aug 31st, 2000, 10:48 AM
#3
Hmmm... That didn't come out right.
Change
To
&H10000
and
&H100
Whitout the spaces.
-
Aug 31st, 2000, 10:54 AM
#4
Thread Starter
Frenzied Member
Um.......I got lots of errors, and how would I do the whole picture?
-
Aug 31st, 2000, 11:15 AM
#5
this turns anything that is not white, black
Code:
Private Sub Command1_Click()
Dim TColor As Long
For i = 0 To Picture1.Width
For j = 0 To Picture1.Height
TColor = GetPixel(Picture1.hdc, i, j)
If TColor <> vbWhite Then
Call SetPixel(Picture1.hdc, i, j, vbBlack)
End If
Next
Next
End Sub
-
Aug 31st, 2000, 11:40 AM
#6
The errors must be because the post doesn't come out right.
What looks like this:
&H10000 is actually hex for the value 65536
and &H100 is hex for 256
You can use the loop denniswrenn wrote but substitute vbBlack for GetGrayScale(TColor)
Code:
Call SetPixel(Picture1.hdc, i, j, GetGrayScale(TColor))
You have to declare SetPixel and GetPixel.
Code:
Public Declare Function GetPixel _
Lib "gdi32" Alias "GetPixel" ( _
ByVal hdc As Long, _
ByVal x As Long, _
ByVal y As Long) As Long
Public Declare Function SetPixel _
Lib "gdi32" Alias "SetPixel" ( _
ByVal hdc As Long, _
ByVal x As Long, _
ByVal y As Long, _
ByVal crColor As Long) As Long
Good luck!
-
Aug 31st, 2000, 12:10 PM
#7
Thread Starter
Frenzied Member
Originally posted by Joacim Andersson
You can use the loop denniswrenn wrote but substitute vbBlack for GetGrayScale(TColor)
Code:
Call SetPixel(Picture1.hdc, i, j, GetGrayScale(TColor))
I get an error: sub or function not defined
Code:
Dim TColor As Long
For i = 0 To Picture1.Width
For j = 0 To Picture1.Height
TColor = GetPixel(Picture1.hdc, i, j)
If TColor <> vbWhite Then
Call SetPixel(Picture1.hdc, i, j, GetGrayScale(TColor))
End If
Next
Next
I get the error in the bold part....Im kind of understanding this, but not enough to fix the bugs. Thanks guys for all this help! 
-
Sep 1st, 2000, 04:26 AM
#8
Well you have to copy and paste the original code I posted in my first reply! Make the changes to the hex values and run the code.
-
Sep 1st, 2000, 04:33 AM
#9
Fanatic Member
Don't forget to use Picture1.ScaleWidth instead of Picture1.Width! You also need to set the ScaleMode property of the picturebox to 3(pixels).
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|