CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Oct 2001
    Location
    Greece
    Posts
    5

    How to convert 24bit images to 2 color images and save them ?

    Does anybody know how to convert a true color bitmap to 2 colors ?


  2. #2
    Join Date
    Oct 2001
    Location
    Phoenix, AZ
    Posts
    54

    Re: How to convert 24bit images to 2 color images and save them ?


    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

  3. #3
    Join Date
    Oct 2001
    Location
    Phoenix, AZ
    Posts
    54

    Re: How to convert 24bit images to 2 color images and save them ?

    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

  4. #4
    Join Date
    Oct 2001
    Location
    Greece
    Posts
    5

    Re: How to convert 24bit images to 2 color images and save them ?

    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 ?


  5. #5
    Join Date
    Oct 2001
    Location
    Phoenix, AZ
    Posts
    54

    Re: How to convert 24bit images to 2 color images and save them ?

    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

  6. #6
    Join Date
    Oct 2001
    Location
    Greece
    Posts
    5

    Re: How to convert 24bit images to 2 color images and save them ?

    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



  7. #7
    Join Date
    Oct 2001
    Location
    Phoenix, AZ
    Posts
    54

    Re: How to convert 24bit images to 2 color images and save them ?

    Could you email me the image you are working with? My email is [email protected].

    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured