Converting 16/24 bit image to 8 bit 
Author Message
 Converting 16/24 bit image to 8 bit
Howdy -

I'm trying to create a customizable screen saver where my client can enter
various company names and the names become part of the graphic and are
manipulated (distorted etc.) along with the rest of the graphic image.

After trying a bunch of ways, I finally got the company name on the image
by using the PRINT method and using SAVEPICTURE to save the image.  That's
fine.  The problem I have now is that my app is expecting an 8 bit image,
but if the customization is done while the user is in a higher video mode
the image is saved as 24 bit.  

Is there any way of fairly quickly converting a 24 bit image to 8 bit it
vb?  I'm using vb5 if that makes a difference.

Thanks!

george




Fri, 10 Aug 2001 03:00:00 GMT  
 Converting 16/24 bit image to 8 bit
Bitmap files start with a fileheader, then an infoheader, followed by
the palette entries (unless it's a 24-bit bmp), and finally the bitmap
bits themselves.  The only complication is that the palette and bitmap
bits are Dword-aligned, which means that palette entries are RGBQUADs,
and each scan line of bitmap bits has to have an even multiple of 4
bytes (up to 3 padding bytes are added if necessary).

I've got a routine here to save a picturebox as a monochrome bitmap:
you should find it pretty easy to change it for 16- and 256-color
standard palettes (if not, just ask more questions here!).  Custom
palettes are quite a bit more work...

Oh, and the picturebox must have AutoRedraw = True, ScaleMode = vbPixel
(3).

Good luck!

Jim Deutch
MS Dev MVP

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
   biXPel{*filter*}eter As Long
   biYPel{*filter*}eter 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
    ReDim SaveBits(0 To BufferSize - 1)
    'fill the header info for the save copy
    With SaveBitmapInfo_1.bmiHeader
        .biSize = 40
        .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



Quote:
> Howdy -

> I'm trying to create a customizable screen saver where my client can
enter
> various company names and the names become part of the graphic and
are
> manipulated (distorted etc.) along with the rest of the graphic
image.

> After trying a bunch of ways, I finally got the company name on the
image
> by using the PRINT method and using SAVEPICTURE to save the image.
That's
> fine.  The problem I have now is that my app is expecting an 8 bit
image,
> but if the customization is done while the user is in a higher video
mode
> the image is saved as 24 bit.  

> Is there any way of fairly quickly converting a 24 bit image to 8 bit
it
> vb?  I'm using vb5 if that makes a difference.

> Thanks!

> george




Fri, 10 Aug 2001 03:00:00 GMT  
 Converting 16/24 bit image to 8 bit
Jim -

Thanks for your help!  I aam using a custom palatte - so I'm afraid this is
more than I can handle for the current project, but I'll save your code for
future use!  Thanks again.

george



Quote:
> Bitmap files start with a fileheader, ...



Sat, 11 Aug 2001 03:00:00 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Convert 16 bit bmp to 24 bit bmp

2. BMP 24-bit to BMP 1-bit converting

3. Fading 8 & 16/24 bit bitmaps

4. How can I convert a 16-bit number into 2 8-bit numbers

5. Desperate for help Converting 16 bit app to 32 bit

6. Convert 16-bit vbp to 32-bit?

7. Converting 16 bit to 32 bit

8. Convert 32-BIT MDB Database to 16-bit database

9. Problems converting from 16 bit to 32 bit

10. Converting Reports from 16-bit to 32-bit??

11. converting 16-bit VB4/Access2 to 32-bit

 

 
Powered by phpBB® Forum Software