Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Convert mult-color bitmap to monochrom bitmap 3

Status
Not open for further replies.

jbs

Programmer
Feb 19, 2000
121
US
I'm sort of a beginner at VBasic. I can do a lot of the basics but I need a little help with changing a multi-color bitmap file to a black-&-white bitmap file.

I need to create a program that will read a multi-colored bitmap file and save it as a monochrome (black & white) bitmap file. I need to be able to perform the change in a single, efficient pass of the bitmap file.

The code should look at each pixel of the bitmap and determine if it's real close to white in color, if so, then the pixel should be changed to pure white ("real close" most likely needs to be defined by a numeric value that can be adjusted). If the pixel is real close to black then the pixel should be changed to pure black. This process needs to be done for every pixel that isn't pure black or pure white when the original file is open.

The monochrome bitmap file should then be saved to a separate file.

I know how to setup a lot of the standard parts of the program however I don't know how to access a single pixel, determine if it's closest to white or black, and then change the pixel. I also don't know how to look at each pixel in an orderly fashion throughout the bitmap.

I suspect that I might need to call and use some type of win32 API from VBasic but I'm not sure....I also don't know how to call a Win32 API from VBasic.

Help?

thanks,

Jerry
 
These two:

Declare Function GetPixel Lib "gdi32.dll" (ByVal hdc As Long, ByVal nXPos As Long, ByVal nYPos As Long) As Long

Declare Function SetPixel Lib "gdi32.dll" (ByVal hdc As Long, ByVal X As Long, ByVal Y As Long, ByVal crColor As Long) As Long

You just need a handle to the DC of the object/picture

Or without api:

Picture1.Point(x,y)=color 'to get pixel color
Picture1.Pset(x,y),color 'to draw pixel

Then you can loop through the picture using it's height & width (scalemode->pixel) and change the color.
But I don't know how to save the changed bitmap, and there might a better way to do this.

Remedy
 
THANKS !! I'll try it later today.
Again, thanks./jerry
 
hello jbs,
this is the solution you wanted
this small program converts the bitmap set as a background image of Form1 to Monochrome.
what more you can see it happening,
good question buddy

aniket


Private Declare Function GetPixel Lib "gdi32" (ByVal hdc As Long, ByVal x As Long, ByVal y As Long) As Long
Private Declare Function SetPixel Lib "gdi32" (ByVal hdc As Long, ByVal x As Long, ByVal y As Long, ByVal crColor As Long) As Long

'I have set the Form1 background as a picture
'this picture will get converted to black and white
Private Sub Command1_Click()
Dim rgbval As Long
Dim x, y As Long
MsgBox Form1.Width
MsgBox Form1.Height
For x = 1 To 1000 'take this as the max x coordinate
For y = 1 To 600 'take this as max y coordinate
rgbval1 = GetPixel(Form1.hdc, x, y)
If 16777215 - rgbval1 > rgbval1 Then
rgbval = SetPixel(Form1.hdc, x, y, 0)
Else
rgbval = SetPixel(Form1.hdc, x, y, 16777215)
'16777215 corresponds to value for white and 0 corresponds to black
End If
Next
Next
End Sub
 
hello kbs

sorry the above solution is incomplete i forgot that you need to save the mon bmp to a file

bye
 
Now that was really cool, explains the whole process, and is sort of fun to see the program run!

For some reason I didn't get the email notification on this.... otherwise I would have responded much sooner!!



/jerry
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top