segunda-feira, 25 de junho de 2007

Saving HBITMAP to file

While implementing the dynamic mode of the magnifier I needed a function to save a HBITMAP to a file. A googled a lot, but I only found C code out there, so I had to convert this to Pascal. I just needed this for a quick test, so I needed to erase this from my software, but I will keep it here. Just in case I ever need it again or it helps someone.

procedure SaveBitmapToFile(bmp: HBITMAP);
myFile: file;
AssignFile(myFile, 'Test.bmp');
ReWrite(myFile, 1); // Define a single 'record' as 1 bytesvar

bmfh.bfType := Byte('B') + (Byte('M') shl 8);
bmfh.bfOffBits := sizeof(BITMAPFILEHEADER) + BitmapInfo.bmiHeader.biSize;
bmfh.bfSize := bmfh.bfOffBits + BitmapInfo.bmiHeader.biSizeImage;
bmfh.bfReserved1 := 0;
bmfh.bfReserved2 := 0;
//Write the bitmap file header
BlockWrite(myFile, bmfh, sizeof(BITMAPFILEHEADER));
//And then the bitmap info header
BlockWrite(myFile, BitmapInfo.bmiHeader, sizeof(BITMAPINFOHEADER));
//Finally, write the image data itself
//-- the data represents our drawing
BlockWrite(myFile, FImageData^, BitmapInfo.bmiHeader.biSizeImage);


Nenhum comentário: