` Printed Icetips Article

Icetips Article



Windows API: Saving DIB as BMP
2002-05-26 -- Jim Kane
 
Newsgroups: softvelocity.products.c55ee


It depends on what you mean by bitmap.  I'm guessing from the title of the
email that you mean a dib and have an hdib and not a hbitmap. you need to
add a small file header to the dib then write it to disk.
The code to add the file header is like this.  the code was pull together
from two spots so it's possible I missed an equate or two.

BmpLibType.SaveDIB              PROCEDURE  (unsigned hdib, *cstring filename)
 ! Declare Procedure
bisizeimage long
bisize      long
fhandle     unsigned
byteswritten ulong
LphDib      Long
hDibSize    Long
hbmpfile    unsigned
lpbmpfile   Long
hbmpSize    Long

BITMAPFILEHEADER       GROUP
bfType                  ushort
bfSize                  ulong
bfReserved1             ushort
bfReserved2             ushort
bfOffbits               ulong
                       END

bmfhSignature string(2),over(bitmapfileheader.bftype)
FILE_BEGIN                      equate(0)
FILE_SHARE_READ                 equate(1)
FILE_SHARE_WRITE                equate(2)
FILE_ATTRIBUTE_NORMAL           equate(80H)
FILE_ATTRIBUTE_TEMPORARY        equate(100H)
FILE_FLAG_RANDOM_ACCESS         equate(10000000H)
FILE_FLAG_SEQUENTIAL_SCAN       equate(08000000H)
FILE_FLAG_DELETE_ON_CLOSE       equate(04000000H)
GENERIC_READ                    equate(80000000H)
GENERIC_WRITE                   equate(40000000H)
GENERIC_EXECUTE                 equate(20000000H)
GENERIC_ALL                     equate(10000000H)
CREATE_NEW                      equate(1)
CREATE_ALWAYS                   equate(2)
OPEN_EXISTING                   equate(3)
OPEN_ALWAYS                     equate(4)
TRUNCATE_EXISTING               equate(5)


  CODE
   hdibsize    =   GlobalSize(hdib)
   lphdib      =   GlobalLock(hdib)
   memcpy(address(biSizeImage),lphdib+20,4)
   memcpy(address(bisize),lphdib,4)
   Bitmapfileheader.bfOffBits = SIZE(BitmapFileHeader)+ |
                                bisize + self.PaletteSize(lphdib)
   BitMapFileHeader.bfSize = bitmapfileheader.bfOffBits + biSizeImage
   bmfhSignature = 'BM' ! STRING OVER bmfh.bfType
   hbmpfile    =   GlobalAlloc(42H, BitMapFileHeader.bfSize)
   If ~hbmpfile then globalunlock(hdib)
     return(3)
   End
   lpbmpfile   =   GlobalLock(hbmpfile)
   memcpy( lpbmpfile, address(bitmapfileHeader), Size(bitmapfileHeader))
   memcpy( lpbmpfile + size(bitmapfileheader), lphdib, |
           Bitmapfileheader.bfsize - size(bitmapfileheader))
   hdibsize    =  BitMapFileHeader.bfSize
   globalunlock(hdib)

  fHandle=CreateFile(FileName,Generic_write,File_Share_Write,0, |
                     Create_Always,FILE_FLAG_SEQUENTIAL_SCAN,0)
  if fhandle= 0FFFFFFFFH then
    !message(getlasterror(),'Create File Error')
    globalFree(hbmpfile)
    return(3)
  end
  WriteFile(fhandle,lpbmpfile,hdibsize,byteswritten,0)
  closehandle(fhandle)
  globalfree(hbmpfile)  !free memory
  if byteswritten < hdibsize then
    Return(3)
  end
  Return(0)



Printed May 14, 2024, 7:25 am
This article has been viewed/printed 35121 times.
Google search has resulted in 291 hits on this article since January 25, 2004.