Click to See Complete Forum and Search --> : How to convert 24bit images to 2 color images and save them ?


johnanast
October 19th, 2001, 04:03 AM
Does anybody know how to convert a true color bitmap to 2 colors ?

JHamilton
October 20th, 2001, 03:08 AM
private Type BITMAPFILEHEADER '14 bytes
bfType as Integer
bfSize as Long
bfReserved1 as Integer
bfReserved2 as Integer
bfOffBits as Long
End Type

private Type BITMAPINFOHEADER '40 bytes
biSize as Long
biWidth as Long
biHeight as Long
biPlanes as Integer
biBitCount as Integer
biCompression as Long
biSizeImage as Long
biXPelsPerMeter as Long
biYPelsPerMeter as Long
biClrUsed as Long
biClrImportant as Long
End Type

private Type RGBQUAD
rgbBlue as Byte
rgbGreen as Byte
rgbRed as Byte
rgbReserved as Byte
End Type

private Type BITMAPINFO_1
bmiHeader as BITMAPINFOHEADER
bmiColors(1) as RGBQUAD
End Type

private Const PIXEL as Integer = 3
private Const DIB_RGB_COLORS as Long = 0
private Const PALVERSION = &H300

private Declare Function GetDIBits1 Lib "gdi32" Alias "GetDIBits" _
(byval aHDC as Long, byval hBitmap as Long, byval nStartScan as Long, _
byval nNumScans as Long, lpBits as Any, lpBI as BITMAPINFO_1, _
byval wUsage as Long) as Long


private Sub Save1(pic as PictureBox)
Dim SaveBitmapInfo_1 as BITMAPINFO_1
Dim SaveFileHeader as BITMAPFILEHEADER
Dim SaveBits() as Byte
Dim BufferSize as Long
Dim fNum as Long
Dim Retval as Long
Dim nLen as Long
Const BitsPixel = 1

'size a buffer for the pixel data
BufferSize = ((pic.ScaleWidth / 8 + 3) And &HFFFC) * pic.ScaleHeight * BitsPixel
ReDim SaveBits(0 to BufferSize - 1)
'fill the header info for the save copy
With SaveBitmapInfo_1.bmiHeader
.biSize = len(SaveFileHeader)
.biWidth = pic.ScaleWidth
.biHeight = pic.ScaleHeight
.biPlanes = 1
.biBitCount = BitsPixel
.biCompression = 0
.biClrUsed = 0
.biClrImportant = 0
.biSizeImage = BufferSize
End With
nLen = len(SaveBitmapInfo_1)
'get the bitmap from the picturebox
Retval = GetDIBits1(pic.hDC, pic.Image, 0, _
SaveBitmapInfo_1.bmiHeader.biHeight, SaveBits(0), SaveBitmapInfo_1, _
DIB_RGB_COLORS)

' create a header for the save file
With SaveFileHeader
.bfType = &H4D42
.bfSize = len(SaveFileHeader) + nLen + BufferSize
.bfOffBits = len(SaveFileHeader) + nLen
End With

' save it to disk
fNum = FreeFile

Open m_SaveFileName for binary as fNum
Put fNum, , SaveFileHeader
Put fNum, , SaveBitmapInfo_1
Put fNum, , SaveBits()
Close fNum

End Sub




Software is like sex, it's better when it's free - Linus Torvalds
Software is like sex, it's better when I get paid for it. - me

JHamilton
October 20th, 2001, 03:10 AM
That .biSize = len(SaveFileHeader)

should be .biSize = 40



Software is like sex, it's better when it's free - Linus Torvalds
Software is like sex, it's better when I get paid for it. - me

johnanast
October 22nd, 2001, 01:23 AM
Thank you for your response. But when I save a picture from a picturebox I get a 2 color bmp file but empty (All is black). Is something wrong here ?

JHamilton
October 22nd, 2001, 04:02 AM
I just retried the code I gave you, and it worked for me. I'm not sure what could be causing the problem.

Software is like sex, it's better when it's free - Linus Torvalds
Software is like sex, it's better when I get paid for it. - me

johnanast
October 22nd, 2001, 05:12 AM
The GetDIBits1 function returns the actual lines read and it is correct.
But the image file is Black.
Thanks anyway. If you find something please send me a note

JHamilton
October 22nd, 2001, 05:54 AM
Could you email me the image you are working with? My email is john@vapidimagery.net.

Software is like sex, it's better when it's free - Linus Torvalds
Software is like sex, it's better when I get paid for it. - me