Save CBitmap to File
-
I have a CBitmap object and want to save it as a file . Please help me how to achieve it. Thanks & regards. Dhiraj
-
I have a CBitmap object and want to save it as a file . Please help me how to achieve it. Thanks & regards. Dhiraj
Starting from CBitmap (MFC) or a GDI HBITMAP (what MFC wraps) it's not so easy: A sample is on MSDN http://msdn.microsoft.com/en-us/library/ms532340(VS.85).aspx[^] Otherwise, cosider using GDI+, where all this stuffs are wrappen in the Gdiplus::Bitmap class that can be created from an HBITMAP. (but you have to link for GDI+ libraries and run startup/termination appropriately: just llok GDI+ under MSDN)
2 bugs found. > recompile ... 65534 bugs found. :doh:
-
I have a CBitmap object and want to save it as a file . Please help me how to achieve it. Thanks & regards. Dhiraj
You may use
CImage
class [^], check out theCImage::Attach
method. I suppose (I didn't test it) the following code will do the trick:CBitmap myBmp;
// bitmap initialization code
CImage myImg;
myImg.Attach(myBmp);
myImg.Save(_T("my.bmp"));
myImg.Detach();:)
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles] -
You may use
CImage
class [^], check out theCImage::Attach
method. I suppose (I didn't test it) the following code will do the trick:CBitmap myBmp;
// bitmap initialization code
CImage myImg;
myImg.Attach(myBmp);
myImg.Save(_T("my.bmp"));
myImg.Detach();:)
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles]Its giving assertion related to size.
-
Its giving assertion related to size.
Dhiraj kumar Saini wrote:
Its giving assertion related to size.
This is not a good way of getting help. You should be more precise and detailed when reporting errors. :)
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles] -
Its giving assertion related to size.
Did you trace it ? and can you show your code?
-
Did you trace it ? and can you show your code?
Ya sure.
void CCaptureDesktopDlg::OnBnClickedButton1()
{
// TODO: Add your control notification handler code hereCString sFName;
CDC ScreenDC;
ScreenDC.Attach(::GetDC(NULL));CBitmap Capture;
CSize Dimensions(GetSystemMetrics(SM_CXSCREEN), GetSystemMetrics(SM_CYSCREEN));
Capture.CreateCompatibleBitmap(&ScreenDC, Dimensions.cx, Dimensions.cy);CDC MemDC;
MemDC.CreateCompatibleDC(&ScreenDC);CBitmap *OldBitmap = MemDC.SelectObject(&Capture);
MemDC.BitBlt(0, 0, Dimensions.cx, Dimensions.cy, &ScreenDC, 0, 0, SRCCOPY);
//added on 28Nov08
CFileDialog fd( FALSE, // true for open, false for save
_T("BMP"), // default extention
NULL, // initial filename in box);
OFN_HIDEREADONLY & // flags for detailed behaviour
OFN_OVERWRITEPROMPT,
NULL, // file-filter pairs
NULL); // pointer to parent window
fd.m_ofn.lpstrTitle = _T("Enter file name...");
fd.m_ofn.lpstrInitialDir = NULL;
fd.m_ofn.lpstrFilter = _T("BMP files (*.bmp)\000*.BMP\000");
if(fd.DoModal() == IDOK)
sFName = fd.GetPathName();
else
return;
HANDLE hDib = DDBToDIB( Capture, BI_RGB, NULL );
BOOL b = WriteDIB( sFName.GetBuffer(sFName.GetLength()) , hDib);
if(!b)
{
CString sMess = _T("Unable to write to file: \n");
sMess += sFName;
AfxMessageBox(sMess);
}
MemDC.SelectObject(Capture);
Capture.Detach(); // make sure bitmap not deleted with CBitmap object
//----------------
MemDC.SelectObject(OldBitmap);
MemDC.DeleteDC();::ReleaseDC(NULL, ScreenDC.Detach());
}
BOOL CCaptureDesktopDlg::WriteDIB( LPTSTR szFile, HANDLE hDIB){
BITMAPFILEHEADER hdr;
LPBITMAPINFOHEADER lpbi;
if (!hDIB)
return FALSE;
CFile file;
if( !file.Open( szFile, CFile::modeWrite|CFile::modeCreate) )
return FALSE;
lpbi = (LPBITMAPINFOHEADER)hDIB;
/* int nColors = 1 << lpbi->biBitCount;
*/
int nColors = 1 << lpbi->biBitCount;
if (nColors > 256)
nColors = 0;
// Fill in the fields of the file header
hdr.bfType = ((WORD) ('M' << 8) | 'B'); // is always "BM"
hdr.bfSize = GlobalSize (hDIB) + sizeof( hdr );
hdr.bfReserved1 = 0;
hdr.bfReserved2 = 0;
//// hdr.bfOffBits = (DWORD) (sizeof( hdr ) + lpbi->biSize +
//// nColors * sizeof(RGBQUAD))
-
Ya sure.
void CCaptureDesktopDlg::OnBnClickedButton1()
{
// TODO: Add your control notification handler code hereCString sFName;
CDC ScreenDC;
ScreenDC.Attach(::GetDC(NULL));CBitmap Capture;
CSize Dimensions(GetSystemMetrics(SM_CXSCREEN), GetSystemMetrics(SM_CYSCREEN));
Capture.CreateCompatibleBitmap(&ScreenDC, Dimensions.cx, Dimensions.cy);CDC MemDC;
MemDC.CreateCompatibleDC(&ScreenDC);CBitmap *OldBitmap = MemDC.SelectObject(&Capture);
MemDC.BitBlt(0, 0, Dimensions.cx, Dimensions.cy, &ScreenDC, 0, 0, SRCCOPY);
//added on 28Nov08
CFileDialog fd( FALSE, // true for open, false for save
_T("BMP"), // default extention
NULL, // initial filename in box);
OFN_HIDEREADONLY & // flags for detailed behaviour
OFN_OVERWRITEPROMPT,
NULL, // file-filter pairs
NULL); // pointer to parent window
fd.m_ofn.lpstrTitle = _T("Enter file name...");
fd.m_ofn.lpstrInitialDir = NULL;
fd.m_ofn.lpstrFilter = _T("BMP files (*.bmp)\000*.BMP\000");
if(fd.DoModal() == IDOK)
sFName = fd.GetPathName();
else
return;
HANDLE hDib = DDBToDIB( Capture, BI_RGB, NULL );
BOOL b = WriteDIB( sFName.GetBuffer(sFName.GetLength()) , hDib);
if(!b)
{
CString sMess = _T("Unable to write to file: \n");
sMess += sFName;
AfxMessageBox(sMess);
}
MemDC.SelectObject(Capture);
Capture.Detach(); // make sure bitmap not deleted with CBitmap object
//----------------
MemDC.SelectObject(OldBitmap);
MemDC.DeleteDC();::ReleaseDC(NULL, ScreenDC.Detach());
}
BOOL CCaptureDesktopDlg::WriteDIB( LPTSTR szFile, HANDLE hDIB){
BITMAPFILEHEADER hdr;
LPBITMAPINFOHEADER lpbi;
if (!hDIB)
return FALSE;
CFile file;
if( !file.Open( szFile, CFile::modeWrite|CFile::modeCreate) )
return FALSE;
lpbi = (LPBITMAPINFOHEADER)hDIB;
/* int nColors = 1 << lpbi->biBitCount;
*/
int nColors = 1 << lpbi->biBitCount;
if (nColors > 256)
nColors = 0;
// Fill in the fields of the file header
hdr.bfType = ((WORD) ('M' << 8) | 'B'); // is always "BM"
hdr.bfSize = GlobalSize (hDIB) + sizeof( hdr );
hdr.bfReserved1 = 0;
hdr.bfReserved2 = 0;
//// hdr.bfOffBits = (DWORD) (sizeof( hdr ) + lpbi->biSize +
//// nColors * sizeof(RGBQUAD))
Change to it: ;) if(fd.DoModal() == IDOK) sFName = fd.GetPathName(); else return; CImage m2; m2.Attach(Capture); m2.Save(fd.GetPathName()); m2.Detach(); /*HANDLE hDib = DDBToDIB( Capture, BI_RGB, NULL ); BOOL b = WriteDIB( sFName.GetBuffer(sFName.GetLength()) , hDib); if(!b) { CString sMess = _T("Unable to write to file: \n"); sMess += sFName; AfxMessageBox(sMess); } */ MemDC.SelectObject(Capture); Capture.Detach();
-
Change to it: ;) if(fd.DoModal() == IDOK) sFName = fd.GetPathName(); else return; CImage m2; m2.Attach(Capture); m2.Save(fd.GetPathName()); m2.Detach(); /*HANDLE hDib = DDBToDIB( Capture, BI_RGB, NULL ); BOOL b = WriteDIB( sFName.GetBuffer(sFName.GetLength()) , hDib); if(!b) { CString sMess = _T("Unable to write to file: \n"); sMess += sFName; AfxMessageBox(sMess); } */ MemDC.SelectObject(Capture); Capture.Detach();
Thanx its working.
-
Thanx its working.
You're welcome. ;)