Skip to main content
GameDev.net gamedev.net
🔒 Locked

Saving HBITMAP/BITMAP to *.bmp file

Started by Jesper T Apr 15, 2002 at 8:49 AM 4 replies 850+ views
Original Post
Jesper T
Jesper T
Howdy Im using msvc++, win32 API. I have used LoadImage() to load a bitmap into my program as a HBITMAP (meaning "handle to bitmap" right?) The question is, how can I write this HBITMAP or BITMAP struct back to a *.bmp or *.dib file ? Do I have to convert it into BITMAPFILEHEADER, BITMAPINFOHEADER etc structs? (or are those classes?) If I have to do that, then how? I couldnt find any SaveImage()-type functions, are there any built in the win32 API? I have used: ofstream blah("file", ios::out); earlier for text files, but im not to familiar with the "FILE *file = something" style (thats c++ and c respectively right?) Anyways, so Id hope that it is possible to do it via the ofstream class. Id appreciate it if someone could explain or point me to some tutorials (Ive looked at the wotsit fileformats, but I need more programming info) Thanks JT (There is no border around this image)
eldee
eldee
well, if you were planning to use an ofstream to save the file
you could just manually write the bmp header (for info
about the header check out wotsit.org) and then just dump
the contents of the bitmap into the file.


-eldee
;another space monkey;
[ Forced Evolution Studios ]

::evolve::

Jesper T
Jesper T
So there arent any standard c++ or win32 functions to save a BITMAP struct or to convert it to BITMAPINFOHEADER/BITMAPFILEHEADER etc.. ?


(There is no border around this image)
Jesper T
Jesper T
ok I tried some variations of this code:


  
bool SaveBmpFile(BITMAP &bmpFile, LPCTSTR pszFileName)
{
ofstream filef( pszFileName, ios::binary );

BITMAPFILEHEADER bmfh;
BITMAPINFOHEADER bmih;
RGBQUAD *aColors = NULL;
BYTE *aBitmapBits = (BYTE *)bmpFile.bmBits;

bmfh.bfType = bmpFile.bmType;
bmfh.bfSize = bmpFile.bmHeight * bmpFile.bmWidth * (bmpFile.bmBitsPixel/8);
bmfh.bfReserved1 = 0;
bmfh.bfReserved2 = 0;
bmfh.bfOffBits =(sizeof(bmfh.bfType)
+sizeof(bmfh.bfSize)
+sizeof(bmfh.bfReserved1)
+sizeof(bmfh.bfReserved2)
+sizeof(bmfh.bfOffBits))/8;

bmih.biSize = sizeof(bmih);
bmih.biWidth = bmpFile.bmWidth;
bmih.biHeight = bmpFile.bmHeight;
bmih.biPlanes = 1;
bmih.biBitCount = bmpFile.bmBitsPixel;
bmih.biCompression = BI_RGB;
bmih.biSizeImage = 0;
bmih.biXPelsPerMeter = 72;
bmih.biYPelsPerMeter = 72;
bmih.biClrUsed = 0;
bmih.biClrImportant = 0;

filef << bmfh.bfType << bmfh.bfSize << bmfh.bfReserved1 << bmfh.bfReserved2 << bmfh.bfOffBits;
filef << bmih.biSize << bmih.biWidth << bmih.biHeight << bmih.biPlanes << bmih.biBitCount << bmih.biCompression
<< bmih.biSizeImage << bmih.biXPelsPerMeter << bmih.biYPelsPerMeter << bmih.biClrUsed << bmih.biBitCount;
filef << aColors;
filef.write((const char *)aBitmapBits, sizeof(bmpFile) );


But the output file is not a valid bmp file, and its far smaller than the bmp file I loaded.

Id appreciate it if anyone could give me some pointers of what I ''m doing wrong, thanks

JT


(There is no border around this image)

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.