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
1. How can I save CBitmap to BMP file?
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
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