Results 1 to 9 of 9

Thread: save(or convert) image in monochrome bitmap [RESOLVED]

  1. #1

    Thread Starter
    New Member
    Join Date
    Apr 2004
    Location
    ttafha
    Posts
    6

    Question save(or convert) image in monochrome bitmap [RESOLVED]

    Hello friends!
    i'm facing a little problem and hope you guys can help.

    SITUATION

    i have a monochrome bitmap file called abc.bmp
    i called this file in VB6 using loadpicture() and put it in a picture box. After manipulated the picture (invert and flip vertical) to another picturebox( called picBmp2), i've called the savepicture method in VB6---SavePicture(picBmp2.Image, "c:\modified.bmp")
    to save the modified picture.

    the problem is :
    Windows did save the file but in 24-bit bitmap. But what i desired is only a 1-bit monochrome bitmap file. The original file is also in monochrome mode. This image file will send to a thermal printer(black and white) to print out. Now i can only open the MSPAINT and manually save as monochrome the modified 24-bit bitmap. It would be great if i can straight get a monochrome bitmap.

    some data:
    original file---- monochrome bitmap 462 bytes
    bitmapOffset=62 bytes

    after save-----24 bit bitmap 6454 bytes
    bitmapOffset=54 bytes



    Please help.
    Thank you very much.
    Last edited by ahfatt; Apr 28th, 2004 at 03:34 AM.

  2. #2
    Retired G&G Mod NoteMe's Avatar
    Join Date
    Oct 2002
    Location
    @ Opera Software
    Posts
    10,190
    Not sure if VB can do that....maybe there is an API for it. But if not, I don't think it is sooo hard to write your own BMP saver. The format is pretty simple.

  3. #3
    Ex-Super Mod'rater Electroman's Avatar
    Join Date
    Sep 2000
    Location
    Newcastle, England
    Posts
    4,349
    Check out: My Examples Page
    The example named "Bitmap File Format Example", it will show you how to save BMP file using the Binary File access in VB. I suggest you do as you have already done then open the 24bit BMP up using this method, convert it to 1bit then save again.

    If you have any trouble just give me a shout and I'll do some code for you.
    When your thread has been resolved please edit the original post in the thread ()
    and amend "-[RESOLVED]-" to the end of the title and change the icon to , Thank you.

    When posting Code use the [VBCode]Code Here[/VBCode] tags to be able to use the code highlighting.

  4. #4

    Thread Starter
    New Member
    Join Date
    Apr 2004
    Location
    ttafha
    Posts
    6

    Angry

    Hello guys, thank you for helping. After read the above example, i tried to write a monochrome bitmap saver based on the Electroman's code but i get stucked somewhere when i want to get the image data from the picture box. Hopes you guy can give me a hand.
    i post the code.

    VB Code:
    1. Private Sub Command2_Click()
    2.  
    3. Dim Pos As Integer
    4. Dim i As Integer
    5. Dim j As Integer
    6. Dim k As Integer
    7. Dim color As Long
    8.     'this is the original file's setting
    9.    
    10.     Pic1bit.bmih.biSize = 40
    11.     Pic1bit.bmih.biWidth = 42
    12.     Pic1bit.bmih.biHeight = 50
    13.     Pic1bit.bmih.biPlanes = 1
    14.     Pic1bit.bmih.biBitCount = 1
    15.     Pic1bit.bmih.biCompression = 0&  ' RGB
    16.     Pic1bit.bmih.biSizeImage = 400
    17.     Pic1bit.bmih.biXPelsPerMeter = 3780
    18.     Pic1bit.bmih.biYPelsPerMeter = 3780
    19.     Pic1bit.bmih.biClrUsed = 0
    20.     Pic1bit.bmih.biClrImportant = 0
    21.    
    22.     Pic1bit.bmfh.bfOffBits = 62
    23.    
    24.     Pic1bit.bmfh.bfType = &H4D42
    25.     Pic1bit.bmfh.bfSize = 462
    26.     Pic1bit.bmfh.bfReserved1 = 0
    27.     Pic1bit.bmfh.bfReserved2 = 0
    28.    
    29.    
    30.     ReDim Pic1bit.aColors(0 To 1)
    31.     Pic1bit.aColors(0).B = 0:   Pic1bit.aColors(0).G = 0:  Pic1bit.aColors(0).R = 0:   Pic1bit.aColors(0).Reserved = 0
    32.     Pic1bit.aColors(1).B = 255:  Pic1bit.aColors(1).G = 255:  Pic1bit.aColors(1).R = 255:   Pic1bit.aColors(1).Reserved = 0
    33.    
    34.     ReDim Pic1bit.aBitmapBits(0 To Pic1bit.bmih.biSizeImage - 1)
    35.    
    36.     For i = 0 To Pic1bit.bmih.biSizeImage - 1
    37.       'let say i have a picture need to be save as monochrome is loaded
    38.       'in a picture box called picBmp.
    39.       'i think i should grab the image data from the picture box
    40.       ' and #put it in the binary file.
    41.       'so i've tried like this and i got stuck here........
    42.        For j = 0 To picBmp.ScaleWidth - 1
    43.          For k = 0 To picBmp.ScaleHeight - 1
    44.             color = picBmp.Point(i, j)
    45.             'stuck here.......
    46.          Next k
    47.        Next j
    48.     Next
    49.      
    50.     Open "C:\Temp.bmp" For Binary As #1
    51.         Put #1, , Pic1bit.bmfh
    52.         Put #1, , Pic1bit.bmih
    53.         Put #1, , Pic1bit.aColors
    54.         Put #1, , Pic1bit.aBitmapBits  ' dim as byte
    55.     Close #1
    56.    
    57.  
    58. End Sub

    thank you for help.

  5. #5
    Ex-Super Mod'rater Electroman's Avatar
    Join Date
    Sep 2000
    Location
    Newcastle, England
    Posts
    4,349
    The bitmap you started with should only be Black & White yes? so if there are no other colors at all you can simpily put an if statement to check if the color is black (Color=0). Then the tricky bit is that each element in the data array is going to be representing 8 pixels, so you may need to do something where the element only chnages every 8 times and inbetween that you have a number that gets halfed starting at 256 and use OR with the data & this number. I'll write some code to explain what I mean in a minute.....
    When your thread has been resolved please edit the original post in the thread ()
    and amend "-[RESOLVED]-" to the end of the title and change the icon to , Thank you.

    When posting Code use the [VBCode]Code Here[/VBCode] tags to be able to use the code highlighting.

  6. #6
    Ex-Super Mod'rater Electroman's Avatar
    Join Date
    Sep 2000
    Location
    Newcastle, England
    Posts
    4,349
    Here is what I have so far but I've made an error somewhere so I'm trying to figure it out at the moment:

    EDIT: Code removed, see next post

    The load bitmap function is in the Example you got off my site.
    Last edited by Electroman; Apr 20th, 2004 at 11:59 AM.
    When your thread has been resolved please edit the original post in the thread ()
    and amend "-[RESOLVED]-" to the end of the title and change the icon to , Thank you.

    When posting Code use the [VBCode]Code Here[/VBCode] tags to be able to use the code highlighting.

  7. #7
    Ex-Super Mod'rater Electroman's Avatar
    Join Date
    Sep 2000
    Location
    Newcastle, England
    Posts
    4,349
    Here we go, Fixed it:
    VB Code:
    1. Private Sub Convert24BitTo1Bit(ByVal iFileName As String)
    2. Dim Bitmap24 As BITMAPFILE24Bit
    3. Dim Bitmap As BITMAPFILE
    4. Dim sFormat As String
    5. Dim ScanLineLen As Long
    6. Dim ScanLine24Len As Long
    7. Dim Pos As Integer
    8. Dim R As Integer
    9. Dim G As Integer
    10. Dim B As Integer
    11. Dim x As Integer
    12. Dim y As Integer
    13. Dim CurBit As Byte
    14. Dim TmpByte As Byte
    15.    
    16.     LoadBitmap iFileName, Bitmap, Bitmap24, sFormat
    17.    
    18.     If sFormat <> "24Bit" Then Exit Sub
    19.    
    20.    
    21.     Bitmap.bmih.biSize = Len(Bitmap.bmih)
    22.     Bitmap.bmih.biWidth = Bitmap24.bmih.biWidth
    23.     Bitmap.bmih.biHeight = Bitmap24.bmih.biHeight
    24.     Bitmap.bmih.biPlanes = Bitmap24.bmih.biPlanes
    25.     Bitmap.bmih.biBitCount = 1
    26.     Bitmap.bmih.biCompression = 0&  ' RGB
    27.     ScanLineLen = (RoundUp((Bitmap.bmih.biWidth / 8) / 4) * 4)
    28.     ScanLine24Len = (RoundUp((Bitmap.bmih.biWidth * 3) / 4) * 4)
    29.     Bitmap.bmih.biSizeImage = ScanLineLen * Bitmap.bmih.biHeight
    30.     Bitmap.bmih.biXPelsPerMeter = Bitmap24.bmih.biXPelsPerMeter
    31.     Bitmap.bmih.biYPelsPerMeter = Bitmap24.bmih.biYPelsPerMeter
    32.     Bitmap.bmih.biClrUsed = 2
    33.     Bitmap.bmih.biClrImportant = 2
    34.    
    35.    
    36.     Bitmap.bmfh.bfOffBits = Len(Bitmap.bmfh) + Len(Bitmap.bmih) + (Len(Bitmap.aColors(0)) * Bitmap.bmih.biClrUsed)
    37.    
    38.     Bitmap.bmfh.bfType = &H4D42
    39.     Bitmap.bmfh.bfSize = Bitmap.bmfh.bfOffBits + Bitmap.bmih.biSizeImage
    40.     Bitmap.bmfh.bfReserved1 = 0
    41.     Bitmap.bmfh.bfReserved2 = 0
    42.    
    43.    
    44.     ReDim Bitmap.aColors(0 To Bitmap.bmih.biClrUsed - 1)
    45.     Bitmap.aColors(0).B = 0:    Bitmap.aColors(0).G = 0:    Bitmap.aColors(0).R = 0:     Bitmap.aColors(0).Reserved = 0
    46.     Bitmap.aColors(1).B = 255:  Bitmap.aColors(1).G = 255:  Bitmap.aColors(1).R = 255:   Bitmap.aColors(1).Reserved = 0
    47.    
    48.     ReDim Bitmap.aBitmapBits(0 To Bitmap.bmih.biSizeImage - 1)
    49.    
    50.     For y = 0 To Bitmap.bmih.biHeight - 1
    51.        
    52.         Pos = y * ScanLineLen
    53.         CurBit = 128
    54.        
    55.         For x = 0 To Bitmap.bmih.biWidth - 1
    56.             'For every pixel in the file (x,y)...
    57.            
    58.             B = Bitmap24.aBitmapBits((y * ScanLine24Len) + (x * 3))
    59.             G = Bitmap24.aBitmapBits((y * ScanLine24Len) + (x * 3) + 1)
    60.             R = Bitmap24.aBitmapBits((y * ScanLine24Len) + (x * 3) + 2)
    61.            
    62.             If R = 0 And G = 0 And B = 0 Then
    63.                 'This pixel is black...
    64.                
    65.                 'We dont really need to do this bit because by default the bits will
    66.                 'already be set to zero. I thought I'd show you how to anyway...
    67.                
    68.                 TmpByte = 255 Xor CurBit
    69.                 Bitmap.aBitmapBits(Pos) = Bitmap.aBitmapBits(Pos) And TmpByte
    70.                
    71.             Else
    72.                 'This pixel is none black so we assume its white...
    73.                
    74.                 Bitmap.aBitmapBits(Pos) = Bitmap.aBitmapBits(Pos) Or CurBit
    75.                
    76.             End If
    77.            
    78.             If CurBit = 1 Then
    79.                 CurBit = 128
    80.                 Pos = Pos + 1
    81.             Else
    82.                 CurBit = CurBit / 2
    83.             End If
    84.         Next
    85.     Next
    86.    
    87.     'Delete the file becuase the one we create is smaller...
    88.     '--------------------------------------
    89.     Kill iFileName
    90.     '--------------------------------------
    91.    
    92.     Open iFileName For Binary As #1
    93.         Put #1, , Bitmap.bmfh
    94.         Put #1, , Bitmap.bmih
    95.         Put #1, , Bitmap.aColors
    96.         Put #1, , Bitmap.aBitmapBits
    97.     Close #1
    98.    
    99. End Sub
    Last edited by Electroman; Apr 20th, 2004 at 12:03 PM.
    When your thread has been resolved please edit the original post in the thread ()
    and amend "-[RESOLVED]-" to the end of the title and change the icon to , Thank you.

    When posting Code use the [VBCode]Code Here[/VBCode] tags to be able to use the code highlighting.

  8. #8

    Thread Starter
    New Member
    Join Date
    Apr 2004
    Location
    ttafha
    Posts
    6

    Smile Thanks!

    Thank you a thousand times, Electroman.
    Because of this problem i can't sleep well for a few nights.....
    i gained quite a lot of knowledge here.
    here is a bow from me....
    thank you.

  9. #9
    Ex-Super Mod'rater Electroman's Avatar
    Join Date
    Sep 2000
    Location
    Newcastle, England
    Posts
    4,349

    Re: Thanks!

    Posted by ahfatt
    Thank you a thousand times, Electroman.
    Because of this problem i can't sleep well for a few nights.....
    i gained quite a lot of knowledge here.
    here is a bow from me....
    thank you.
    No worries mate .
    When your thread has been resolved please edit the original post in the thread ()
    and amend "-[RESOLVED]-" to the end of the title and change the icon to , Thank you.

    When posting Code use the [VBCode]Code Here[/VBCode] tags to be able to use the code highlighting.

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