Advertisement

Saving BITMAP to HD...??

Started by July 12, 2001 10:28 AM
7 comments, last by JaZsT 23 years, 7 months ago
Hello !! Can anyone please tell me how to save a BITMAP to a .bmp file on HD???? I create HBITMAP using CreateCompatibleBitmap, and then i put it into a DC, then I do some blitting.....evrything OK so far. But how do I save the BITMAP to a .bmp file - to hard disk??? Thanx alot! Life lived unexplored is life not worth living !
Life lived unexplored is life not worth living !
Basically you create the headers and write those to a file and then write the bit data.
Im just about to leave work but if no-one has answered you by the time I get home I''ll explain a bit better if you still need help.
Just my thoughts take them as you will. "People spend too much time thinking about the past, whatever else it is, its gone"-Mel Gibson, Man Without A Face
Advertisement
Hey,

Thanx for your answer, but It doesn''t help me much, cause I''m quite new in vc++. I only know how to write to a file with ofstream...( So, please tell me some more )

Thanx
Life lived unexplored is life not worth living !
Can you post your code for how you have created the bitmap?
Just my thoughts take them as you will. "People spend too much time thinking about the past, whatever else it is, its gone"-Mel Gibson, Man Without A Face
Hello !!

Sure!

HDC hdc=GetDC(hWndMain);
hdcMem=CreateCompatibleDC(hdc);
MyBMP=CreateCompatibleBitmap(hdc,100,100);
hbmOld=(HBITMAP)SelectObject(hdcMem,MyBMP);

hWndMain is a handle to main window
hdcMem is a HDC global

So, is this code ok??

Thanx
--
Uros

Life lived unexplored is life not worth living !
Life lived unexplored is life not worth living !
Im afraid i dont quite get what it is you are trying to do. Im not familier with the CreateCompatibleBitmap (i normally use CreateDibSection() to get a hbitmap).

Generally to create a bitmap on disk you write out the BITMAPFILEHEADER then the BITMAPINFO and then the bits. To get the bits from a hbitmap you need to call GetDIBits().

If you can give me a bit more info on what it is exactly you are trying to create a bitmap of then I might be able to help more.
Just my thoughts take them as you will. "People spend too much time thinking about the past, whatever else it is, its gone"-Mel Gibson, Man Without A Face
Advertisement
Hello!!

Here''s the code I came up with so far(some is copied from MSDN):

BITMAP bmp;
DWORD cClrBits;

GetObject(MyBMP,sizeof(BITMAP),(LPSTR)&bmp);

cClrBits = (WORD)(bmp.bmPlanes * bmp.bmBitsPixel);


BITMAPINFOHEADER bih;
ZeroMemory(&bih,sizeof(bih));
bih.biSize=sizeof(BITMAPINFOHEADER);
bih.biWidth=bmp.bmWidth;
bih.biHeight=bmp.bmHeight;
bih.biPlanes=bmp.bmPlanes;;
bih.biBitCount=bmp.bmBitsPixel;
bih.biCompression=BI_RGB;
bih.biSizeImage=((bih.biWidth * cClrBits +31) & ~31) /8 * bih.biHeight;
//bih.biXPelsPerMeter=800;
//bih.biYPelsPerMeter=600;
bih.biClrUsed=0;
bih.biClrImportant=0;

RGBQUAD rgbquad;
ZeroMemory(&rgbquad,sizeof(rgbquad));
rgbquad.rgbBlue=255; //-----???????????????????????????????? - i don''t know with what to fill these
rgbquad.rgbGreen=255; //-----???????????????????????????????? - i don''t know with what to fill these
rgbquad.rgbRed=255; //-----???????????????????????????????? - i don''t know with what to fill these
rgbquad.rgbReserved=0;

BITMAPINFO bitmapinfo;
ZeroMemory(&bitmapinfo,sizeof(bitmapinfo));
bitmapinfo.bmiHeader=bih;
bitmapinfo.bmiColors[1]=rgbquad;

LPBYTE lpBits;
DWORD dwTotal;
DWORD cb;
BYTE *hp;

lpBits = (LPBYTE) GlobalAlloc(GMEM_FIXED, bih.biSizeImage);

GetDIBits(hdcMem,MyBMP,0,(WORD) bih.biHeight, lpBits, &bitmapinfo,DIB_RGB_COLORS);


LPBITMAPINFOHEADER lpbi=&bih
LPCSTR szFile = "bmpFile.bmp";

BITMAPFILEHEADER hdr;
int fh;
OFSTRUCT of;

fh = OpenFile(szFile,&of, OF_CREATE | OF_READWRITE);

hdr.bfType = 0x4d42;
hdr.bfSize = (DWORD) (sizeof(BITMAPFILEHEADER) + lpbi->biSize + lpbi->biClrUsed * sizeof(RGBQUAD) + lpbi->biSizeImage);
hdr.bfReserved1 = 0;
hdr.bfReserved2 = 0;
hdr.bfOffBits = (DWORD) sizeof(BITMAPFILEHEADER) + lpbi->biSize + lpbi->biClrUsed * sizeof (RGBQUAD);


/* Write the file header */
if(!_lwrite(fh, (LPSTR) &hdr, sizeof(BITMAPFILEHEADER)))PostQuitMessage(0);

/* Write the DIB header and the bits */
if(!_lwrite(fh, (LPSTR) lpbi, (lpbi->biSize + lpbi->biClrUsed * sizeof(RGBQUAD) + lpbi->biSizeImage)))PostQuitMessage(0);

// Copy the array of color indices into the .BMP file.
dwTotal = cb = bih.biSizeImage;
hp = lpBits;

if(!_lwrite(fh,(LPSTR)hp,(int) cb))PostQuitMessage(0);

_lclose(fh);

GlobalFree((HGLOBAL)lpBits);

That''s it. MyBMP which i use to get the BITMAP object is a valid HBITMAP.
The code compiles & executes OK, but I cannot open the file it creates (not valid bmp file).
Can you perhaps figure out what is not OK?? I''d be very thankful!!

Thanx
--
Uro
Life lived unexplored is life not worth living !
If it''s a 24-bit bitmap, I don''t think you need to write the RGBQUAD and color indexes to the file. Try skipping those.

Just write the BITMAPFILEHEADER and BITMAPINFOHEADER, followed by the bitmap bits.

I''m not sure, but I think I remember something about bitmap bits being written with some trash at the end of every line. The bitmap bits are written row by row and if the width of the bitmap isn''t a multiple of 4, it''ll write how ever many extra, junk, bits it needs at the end of each row of bitmap bits to make the width a multiple of 4.

If that whole trash thing seems confusing, just work with bitmaps whose widths are multiples of 4 and it won''t be an issue.


I''m not sure about 8-bit bitmaps, though.

Hi.

I just created a simple app with the code you posted and it sort of works. It creates the bmp on disk and mspaint will let me open it but just shows a black square (monochrome bmp). Im assuming this is sort of correct because as far as i can tell you havent actually created an image but a blank bmp. Anywho i changed the first bit so just in case here is what I changed:

instead of

HDC hdc=GetDC(hWndMain);
hdcMem=CreateCompatibleDC(hdc);
MyBMP=CreateCompatibleBitmap(hdc,100,100);
hbmOld=(HBITMAP)SelectObject(hdcMem,MyBMP);

i used

hdcMem=CreateCompatibleDC(NULL);
HBITMAP MyBMP;
MyBMP=CreateCompatibleBitmap(hdcMem,100,100);
HGDIOBJ hbmOld=(HBITMAP)SelectObject(hdcMem,MyBMP);

I doubt if this is the reason it tells you the file is not a valid bmp but I cant see anything wrong with your code.


Edited by - Zeke on July 14, 2001 6:17:18 PM
Just my thoughts take them as you will. "People spend too much time thinking about the past, whatever else it is, its gone"-Mel Gibson, Man Without A Face

This topic is closed to new replies.

Advertisement