Results 1 to 9 of 9

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

Threaded View

  1. #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.

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