Saving & Loading BMP's from API 
Author Message
 Saving & Loading BMP's from API

I'm looking for code or a class that allows you to save and load bmpfiles
from the API. I need this because I want to tack some extra data onto the
end of the binary file. That way when I load the bitmap I also get the data
that goes along with it.

thanks

David



Sat, 30 Jun 2001 03:00:00 GMT  
 Saving & Loading BMP's from API
bitmap files come in many, many flavors: 1, 4, 8, 16, and 24-bit, some
of which can be run-length encoded, etc.  If you limit yourself to one
or two formats, it'll be pretty easy, though.  SetDIBits() is one API
function you can use: there are also DIBSection APIs but I haven't used
them myself, yet.

Here's a related code snippet:

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_4
   bmiHeader As BITMAPINFOHEADER
   bmiColors(15) As RGBQUAD         'Specific to 16-color bitmaps!
End Type

Private Const PIXEL As Integer = 3
Private Const DIB_RGB_COLORS As Long = 0

Private Declare Function GetDIBits4 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_4, _
  ByVal wUsage As Long) As Long

Private Declare Function SetDIBits4 Lib "gdi32" Alias "SetDIBits" _
  (ByVal hdc As Long, ByVal hBitmap As Long, ByVal nStartScan As Long,
_
  ByVal nNumScans As Long, lpBits As Any, lpBI As BITMAPINFO_4, _
  ByVal wUsage As Long) As Long

    'size a buffer for the pixel data (16-color bmp!)
    BufferSize = ((pic.ScaleWidth / 2 + 3) And &HFFFC) *
pic.ScaleHeight
    ReDim SaveBits(0 To BufferSize - 1)
    'fill the header info for the save copy
    With SaveBitmapInfo_4.bmiHeader
        .biSize = 40
        .biWidth = pic.ScaleWidth
        .biHeight = pic.ScaleHeight
        .biPlanes = 1
        .biBitCount = 4    '16-colors!
        .biCompression = 0
        .biClrUsed = 0
        .biClrImportant = 0
        .biSizeImage = BufferSize
    End With
    nLen = Len(SaveBitmapInfo_4)
    'get the bitmap from the picturebox
    Retval = GetDIBits4(pic.hdc, pic.Image, 0, _
        SaveBitmapInfo_4.bmiHeader.biHeight, SaveBits(0),
SaveBitmapInfo_4, DIB_RGB_COLORS)

    'change the palette
    SaveBitmapInfo_4.bmiColors(0).rgbRed = 255
    SaveBitmapInfo_4.bmiColors(0).rgbGreen = 255
    SaveBitmapInfo_4.bmiColors(0).rgbBlue = 0
    'blast it back in
    Retval = SetDIBits4(pic.hdc, pic.Image.Handle, 0, _
        SaveBitmapInfo_4.bmiHeader.biHeight, SaveBits(0),
SaveBitmapInfo_4, DIB_RGB_COLORS)
    pic.Refresh

You'll be getting the BITMAPINFO and SaveBits() data directly out of a
disk file, instead of a picturebox, but the concept is the same.  Be
sure you skip the first 14 bytes of the bitmap file (that's the
BITMAPFILEHEADER).

Also be sure your picturebox has ScaleMode = pixel and AutoRedraw =
True, and that it is exactly the right size for the picture, or
SetDIBits() will fail (use borders = none until you have it working:
then you can figure out how to account for the borders later).

Jim Deutch
MS Dev MVP



Quote:
> I'm looking for code or a class that allows you to save and load
bmpfiles
> from the API. I need this because I want to tack some extra data onto
the
> end of the binary file. That way when I load the bitmap I also get
the data
> that goes along with it.

> thanks

> David



Sun, 01 Jul 2001 03:00:00 GMT  
 Saving & Loading BMP's from API
Thanks. I didn't use all of your code but some of it. I had to make a couple
of minor changes to get what I was looking for. Amazingly enough it works
great. And you can even still look at the bitmap as if there was no other
data on the the end. :)

David.

Quote:
>Here's a related code snippet:

>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_4
>   bmiHeader As BITMAPINFOHEADER
>   bmiColors(15) As RGBQUAD         'Specific to 16-color bitmaps!
>End Type

>Private Const PIXEL As Integer = 3
>Private Const DIB_RGB_COLORS As Long = 0

>Private Declare Function GetDIBits4 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_4, _
>  ByVal wUsage As Long) As Long

>Private Declare Function SetDIBits4 Lib "gdi32" Alias "SetDIBits" _
>  (ByVal hdc As Long, ByVal hBitmap As Long, ByVal nStartScan As Long,
>_
>  ByVal nNumScans As Long, lpBits As Any, lpBI As BITMAPINFO_4, _
>  ByVal wUsage As Long) As Long

>    'size a buffer for the pixel data (16-color bmp!)
>    BufferSize = ((pic.ScaleWidth / 2 + 3) And &HFFFC) *
>pic.ScaleHeight
>    ReDim SaveBits(0 To BufferSize - 1)
>    'fill the header info for the save copy
>    With SaveBitmapInfo_4.bmiHeader
>        .biSize = 40
>        .biWidth = pic.ScaleWidth
>        .biHeight = pic.ScaleHeight
>        .biPlanes = 1
>        .biBitCount = 4    '16-colors!
>        .biCompression = 0
>        .biClrUsed = 0
>        .biClrImportant = 0
>        .biSizeImage = BufferSize
>    End With
>    nLen = Len(SaveBitmapInfo_4)
>    'get the bitmap from the picturebox
>    Retval = GetDIBits4(pic.hdc, pic.Image, 0, _
>        SaveBitmapInfo_4.bmiHeader.biHeight, SaveBits(0),
>SaveBitmapInfo_4, DIB_RGB_COLORS)

>    'change the palette
>    SaveBitmapInfo_4.bmiColors(0).rgbRed = 255
>    SaveBitmapInfo_4.bmiColors(0).rgbGreen = 255
>    SaveBitmapInfo_4.bmiColors(0).rgbBlue = 0
>    'blast it back in
>    Retval = SetDIBits4(pic.hdc, pic.Image.Handle, 0, _
>        SaveBitmapInfo_4.bmiHeader.biHeight, SaveBits(0),
>SaveBitmapInfo_4, DIB_RGB_COLORS)
>    pic.Refresh

>You'll be getting the BITMAPINFO and SaveBits() data directly out of a
>disk file, instead of a picturebox, but the concept is the same.  Be
>sure you skip the first 14 bytes of the bitmap file (that's the
>BITMAPFILEHEADER).

>Also be sure your picturebox has ScaleMode = pixel and AutoRedraw =
>True, and that it is exactly the right size for the picture, or
>SetDIBits() will fail (use borders = none until you have it working:
>then you can figure out how to account for the borders later).

>Jim Deutch
>MS Dev MVP



>> I'm looking for code or a class that allows you to save and load
>bmpfiles
>> from the API. I need this because I want to tack some extra data onto
>the
>> end of the binary file. That way when I load the bitmap I also get
>the data
>> that goes along with it.

>> thanks

>> David



Sun, 01 Jul 2001 03:00:00 GMT  
 Saving & Loading BMP's from API
Nice to see someone take a good solid hint and turn it into a final
product.  Congrats!
I would have stripped the extra data before displaying, but SetDIBits()
knows how much valid data there is and I guess it just stops there.

Jim Deutch
MS Dev MVP



Quote:
> Thanks. I didn't use all of your code but some of it. I had to make a
couple
> of minor changes to get what I was looking for. Amazingly enough it
works
> great. And you can even still look at the bitmap as if there was no
other
> data on the the end. :)

> David.



Mon, 02 Jul 2001 03:00:00 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. save bmp in same depth as loaded bmp

2. Saving API Drawings to a BMP or Converting from WMF to BMP

3. BMP's over 100k won't load into an OLE field

4. using APIs to load a BMP as a file mapping object

5. How to load BMP's in Basic ?

6. Low level Visual Basic 6 API code to save BMP

7. HOW TO: Load BMP's in VBDOS ??!

8. saving bmp file with API calls

9. Low level Visual Basic 6 API code to save BMP

10. how to save the content from bmp's

11. Saving Image or PictureBox control's contents to file (BMP/JPG etc)

12. How save a Bmp File's in Access Database

 

 
Powered by phpBB® Forum Software