Need graphics programmers to analyse my bitmap code!!!!!!

Started by
1 comment, last by Xeon_Boy 22 years, 8 months ago
Hi there! Someone gave me the following code to save the bitmap data and bits(and stuff that describes whata'' bitmap looks like) to disk, and below is it : void CMyProgramDoc::Serialize(CArchive &ar){ if(ar.IsStoring()) { BITMAPINFO *pbmi; BITMAP bm; BITMAPFILEHEADER bmfh; HDC hDC = NULL; DWORD wHeaderSize; void * pBits; CBitmap bitmap; if(!bitmap.GetBitmap(bm))//*** { MessageBox(NULL,"Could not get Bitmap!","Error!",MB_ICONEXCLAMATION); } wHeaderSize = sizeof(BITMAPINFOHEADER); pbmi = (PBITMAPINFO) new char[wHeaderSize]; pbmi->bmiHeader.biSize = sizeof(BITMAPINFOHEADER); pbmi->bmiHeader.biPlanes = 1; pbmi->bmiHeader.biBitCount = 24; pbmi->bmiHeader.biHeight = bm.bmHeight; pbmi->bmiHeader.biWidth = bm.bmWidth; pbmi->bmiHeader.biCompression = BI_RGB; pbmi->bmiHeader.biClrUsed = 0; pbmi->bmiHeader.biClrImportant = 0; pbmi->bmiHeader.biSizeImage = ((pbmi->bmiHeader.biWidth * 24 +31) & ~31)/8 * pbmi->bmiHeader.biHeight; hDC = ::CreateCompatibleDC(NULL); if(!hDC) { MessageBox(NULL,"CreateCompatibleDC() returned NULL","Null now!",MB_ICONEXCLAMATION); } pBits = new char[pbmi->bmiHeader.biSizeImage]; HBITMAP hBitmap = (HBITMAP)LoadBitmap(NULL,MAKEINTRESOURCE(IDB_BITMAP)); if(!hBitmap) { MessageBox(NULL,"GetSafeHandle() returned NULL","Null!",MB_ICONEXCLAMATION); } if(!::GetDIBits(hDC,hBitmap,0,(WORD)pbmi->bmiHeader.biHeight,pBits,pbmi,DIB_RGB_COLORS)) { MessageBox(NULL,"GetDIBits failure","Failure",MB_ICONEXCLAMATION); } bmfh.bfType = 0x4d42; bmfh.bfReserved1 = 0; bmfh.bfReserved2 = 0; bmfh.bfSize = sizeof(BITMAPFILEHEADER) + wHeaderSize + pbmi->bmiHeader.biSizeImage; bmfh.bfOffBits = (DWORD) sizeof(BITMAPFILEHEADER) + pbmi->bmiHeader.biSize + pbmi->bmiHeader.biClrUsed * sizeof (RGBQUAD); ar.Write(&bmfh, sizeof(BITMAPFILEHEADER)); ar.Write(pbmi, wHeaderSize); ar.Write(pBits, pbmi->bmiHeader.biSizeImage); delete[] pBits; delete[] pbmi; } } But when I run the SDI program and clicked the Save menu item under the File menu, I get two error message boxes and they''re "Could not get Bitmap!" and "Failed to document". Help! Can anyone tell me the right way to modify this code so that it really save the bitmap data to disk?(not a bitmap .bmp file, but as a file type of my own with the specific bitmap data) U can create a SDI program and add the code above to ye'' application''s Document class, in the Serialize(...) function. Then, create a .bmp file using the Visual C++ bitmap editor and save it as IDB_BITMAP. Then, compile and run the program and aaarrrggghh!!!!!! The errors popped up. :-( Thanks! I''ve been working on this bitmap serializing thing for 6.5 days, but just can''t get it right!!!!!! AAARRRGGGHHHH!!!!!!!!!!!!!!!!! "The feeling of mastering and understanding hard stuff in Game Programming is just like the feeling u get when u perform an Air-Walk in the basketball court, soaring.....and everyone''s watching in awe......."
"The feeling of mastering and understanding hard stuff in Game Programming is just like the feeling u get when u perform an Air-Walk in the basketball court, soaring.....and everyone's watching in awe......."
Advertisement
Do you really expect people to be able to read that?

War Worlds - A 3D Real-Time Strategy game in development.

Oops! Really sorry! The code formatting wasn''t preserved and it all ended up like that! The code below should be ok now :

void CMyProgramDoc::Serialize(CArchive &ar)
{
if(ar.IsStoring())
{
BITMAPINFO *pbmi;
BITMAP bm;
BITMAPFILEHEADER bmfh;
HDC hDC = NULL;
DWORD wHeaderSize;
void * pBits;

CBitmap bitmap;

if(!bitmap.GetBitmap(bm))//***
{
MessageBox(NULL,"Could not get Bitmap!","Error!",MB_ICONEXCLAMATION);
}

wHeaderSize = sizeof(BITMAPINFOHEADER);
pbmi = (PBITMAPINFO) new char[wHeaderSize];
pbmi->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
pbmi->bmiHeader.biPlanes = 1;
pbmi->bmiHeader.biBitCount = 24;
pbmi->bmiHeader.biHeight = bm.bmHeight;
pbmi->bmiHeader.biWidth = bm.bmWidth;
pbmi->bmiHeader.biCompression = BI_RGB;
pbmi->bmiHeader.biClrUsed = 0;
pbmi->bmiHeader.biClrImportant = 0;
pbmi->bmiHeader.biSizeImage = ((pbmi->bmiHeader.biWidth * 24 +31) & ~31)/8 * pbmi->bmiHeader.biHeight;

hDC = ::CreateCompatibleDC(NULL);

if(!hDC)
{
MessageBox(NULL,"CreateCompatibleDC() returned NULL","Null now!",MB_ICONEXCLAMATION);
}

pBits = new char[pbmi->bmiHeader.biSizeImage];
HBITMAP hBitmap = (HBITMAP)LoadBitmap(NULL,MAKEINTRESOURCE(IDB_BITMAP));

if(!hBitmap)
{
MessageBox(NULL,"GetSafeHandle() returned NULL","Null!",MB_ICONEXCLAMATION);
}

if(!::GetDIBits(hDC,hBitmap,0,(WORD)pbmi->bmiHeader.biHeight,pBits,pbmi,DIB_RGB_COLORS))
{
MessageBox(NULL,"GetDIBits failure","Failure",MB_ICONEXCLAMATION);
}

bmfh.bfType = 0x4d42;
bmfh.bfReserved1 = 0;
bmfh.bfReserved2 = 0;
bmfh.bfSize = sizeof(BITMAPFILEHEADER) + wHeaderSize + pbmi->bmiHeader.biSizeImage;
bmfh.bfOffBits = (DWORD) sizeof(BITMAPFILEHEADER) + pbmi->bmiHeader.biSize + pbmi->bmiHeader.biClrUsed * sizeof (RGBQUAD);
ar.Write(&bmfh, sizeof(BITMAPFILEHEADER));
ar.Write(pbmi, wHeaderSize);
ar.Write(pBits, pbmi->bmiHeader.biSizeImage);

delete[] pBits;
delete[] pbmi;
}

Actually, I wanna save the bitmap data, that is, how and what it looks like, just like the way word processing applications store bitmap data(like MS word/PowerPoint, for example), along with text when the user click the Save button. The bitmap in the document isn''t saved as a separate .bmp file, but within the document itself(for example, .ppt or .doc), so that when the user opens and load the document, the bitmap is loaded too.

As u can see, the code in my posts do this, but it doesn''t do it right. It saves the bitmap data and bits, but there''s some error in the code, and I dunno how to correct it. The CBitmap thing is created on the stack and has no contents, but how do we modify it so that the bitmap data is actually saved?(to the archive, and not as a .bmp file) Thanks!!! :-D


"The feeling of mastering and understanding hard stuff in Game Programming is just like the feeling u get when u perform an Air-Walk in the basketball court, soaring.....and everyone''s watching in awe......."
"The feeling of mastering and understanding hard stuff in Game Programming is just like the feeling u get when u perform an Air-Walk in the basketball court, soaring.....and everyone's watching in awe......."

This topic is closed to new replies.

Advertisement