mfc >> How do you save a CBitmap to a BMP file

by moogle33 » Thu, 27 Nov 2003 17:38:57 GMT

Does anybody know how to do this ?
You would think it would be easy... The internet has loads of references to
loading BMP's into CBitmaps but none for the other way round :(

James




mfc >> How do you save a CBitmap to a BMP file

by David A. Mair » Fri, 28 Nov 2003 03:12:52 GMT





to

Here's some code I use to do it in my WebCam app:

HANDLE hf = CreateFile(bmp, GENERIC_WRITE, FILE_SHARE_READ, NULL,
CREATE_ALWAYS, NULL, NULL );

if( hf == INVALID_HANDLE_VALUE )
return false;

// write out the file header
//
BITMAPFILEHEADER bfh;
memset( &bfh, 0, sizeof( bfh ) );
bfh.bfType = 'MB';
bfh.bfSize = sizeof( bfh ) + m_CBInfo.lBufferSize + sizeof(
BITMAPINFOHEADER );
bfh.bfOffBits = sizeof( BITMAPINFOHEADER ) + sizeof( BITMAPFILEHEADER );

DWORD dwWritten = 0;
WriteFile( hf, &bfh, sizeof( bfh ), &dwWritten, NULL );

// and the bitmap format
//
VIDEOINFOHEADER * vih = (VIDEOINFOHEADER*) m_StillMediaType.pbFormat;
BITMAPINFOHEADER bih;
memset( &bih, 0, sizeof( bih ) );
bih.biSize = sizeof( bih );
bih.biWidth = vih->bmiHeader.biWidth;
bih.biHeight = vih->bmiHeader.biHeight;
bih.biPlanes = 1;
bih.biBitCount = 24;

dwWritten = 0;
WriteFile( hf, &bih, sizeof( bih ), &dwWritten, NULL );

// and the bits themselves
//
dwWritten = 0;
WriteFile( hf, m_CBInfo.pBuffer, m_CBInfo.lBufferSize, &dwWritten, NULL );
CloseHandle( hf );

Note that in CreateFile() the bmp argument is a CString holding the filename
to use when creating the file. The BITMAPINFOHEADER is what I write on the
second WriteFile(), the VIDEOINFOHEADER is only used to get the width and
height of the image since it is coming from a video stream and I'm writing
frames as-is. Note also that I do almost no error checking, you should -
I'm not finished yet. Finally, I know I have 24 bit pixels and one colour
plane, you may need to make these fields of the BITMAPINFOHEADER
configurable.





Similar Threads

1. How can I save CBitmap to BMP file?

2. Saving CBitmap as .bmp

Hi,

I need to save a CBitmap as a DIB to a CBitmap file.
I already searched the web for any information, but
everything did't work for me. The code I already wrote is this :

void CDibManager::OnRoiSavebmp(CDC *pDC, CString filename, CBitmap &bitm)
{
 HDC hDC = pDC->m_hDC;//GetSafeHdc();

 CString s;
 s = filename;
 s += ".bmp";

 if(bitm.m_hObject == NULL)
  return;


 BITMAPFILEHEADER hdr; // bitmap file-header
 void *pBits;            // memory pointer
 BITMAPINFO bmi;   //

 bmi.bmiHeader.biSize = sizeof(BITMAPINFO);
 bmi.bmiHeader.biBitCount = 0;

 if (!GetDIBits(hDC, HBITMAP(bitm), 0, 0, NULL, &bmi, DIB_RGB_COLORS))
  return ;

 pBits = new BYTE[bmi.bmiHeader.biSizeImage];

 if (!GetDIBits(hDC, HBITMAP(bitm), 0, bmi.bmiHeader.biHeight, pBits, &bmi,
DIB_RGB_COLORS))
  return;

    // Create the .BMP file.
    CFile * pFile;
 try{
  pFile = new CFile(s,CFile::modeCreate|CFile::modeWrite);
  hdr.bfType = 0x4d42;        // 0x42 = "B" 0x4d = "M"
  // Compute the size of the entire file.
  hdr.bfSize = (DWORD) (sizeof(BITMAPFILEHEADER) + bmi.bmiHeader.biSize +
bmi.bmiHeader.biClrUsed  * sizeof(RGBQUAD) + bmi.bmiHeader.biSizeImage);
  hdr.bfReserved1 = 0;
  hdr.bfReserved2 = 0;

  // Compute the offset to the array of color indices.
  hdr.bfOffBits = (DWORD) sizeof(BITMAPFILEHEADER) + bmi.bmiHeader.biSize +
bmi.bmiHeader.biClrUsed * sizeof (RGBQUAD);
  // Copy the BITMAPFILEHEADER into the .BMP file.
  pFile->Write(&hdr,sizeof(BITMAPFILEHEADER));

  // Copy the BITMAPINFOHEADER and RGBQUAD array into the file.

  pFile->Write(&bmi.bmiHeader,sizeof(BITMAPINFOHEADER) +
bmi.bmiHeader.biClrUsed * sizeof (RGBQUAD));

  // Copy the array of color indices into the .BMP file.

  pFile->Write(pBits,bmi.bmiHeader.biSizeImage); // CRASHES !!!!

  pFile->Close();
  delete pFile;
  pFile = NULL;
 }catch(CException* pE){
  pFile->Abort
  pE->ReportError();
  pE->Delete();

 }

 delete pBits;
 pBits = NULL;

}

The code crashes in the line with the // CRASHES !!!! comment.
The pointer pBits seems to be at least part of the problem, because after
the line :

 if (!GetDIBits(hDC, HBITMAP(bitm), 0, bmi.bmiHeader.biHeight, pBits, &bmi,
DIB_RGB_COLORS))


if has the adress 0x00ffffff.

Can anybody help, or hint me to a website which could help me ?

Thanks in advanve,

Jonas


3. Cutting a BMP file to two BMP file

4. save bmp file

How can I save the bmp file to mono style?


5. Capture a client window and save as a file (bmp)

6. how to save a bmp from the clipboard to a file

Hello Im really stuck with this one, cus im a bit out of my legue i
found this code that copies the screen to the clipboard and I can
paste the picture into paint but I want to know how to save it as a
bmp file.

CWnd *pWndThis= (CWnd *) this; 
 CBitmap BitmapClip;          
 CClientDC ClientDC(this);     
 CDC MemDC;
 CRect rc;
 pWndThis->GetClientRect(&rc);         
 BitmapClip.CreateCompatibleBitmap( &ClientDC,rc.Width(),rc.Height());
 MemDC.CreateCompatibleDC(&ClientDC);
 MemDC.SelectObject(&BitmapClip); 
 MemDC.BitBlt(0,0,rc.Width(),rc.Height(),&ClientDC,0,0,SRCCOPY);
 
 pWndThis->OpenClipboard(); 
 ::EmptyClipboard();  
 ::SetClipboardData(  
    CF_BITMAP,BitmapClip.m_hObject);
 BitmapClip.Detach();
 ::CloseClipboard(); 

Any ideas would be appreiciated 

Thanks

7. Saving BMP file renders Black Image

8. Saving bitmap with drawing primitives to bmp file

Hello,

I have a bitmap displayed with some drawing primitives drawn on the
bitmap (FrameRect, Ellipse, etc.).

How do I save these primitives and the bitmap to disk in bmp format?

Thanks