0 bytes CImage saved
-
I intend to save CBitmap into a bmp file, here is the code:
// MyDocument.cpp
CMyView* pView = (CMyView*)GetNextView(pos);
CDC* pDC = pView->GetDC();CDC MemDC; MemDC.CreateCompatibleDC(pDC); CBitmap bitmap; bitmap.CreateCompatibleBitmap(pDC, GetTotalCx(), GetTotalCy()); CBitmap\* pOldBitmap = (CBitmap\*)MemDC.SelectObject(&bitmap); // draw the MemDC pDC->BitBlt(0, 0, GetTotalCx(), GetTotalCy(), &MemDC, 0, 0, SRCCOPY); CBitmap\* pFinalBitmap = (CBitmap\*)MemDC.SelectObject(pOldBitmap); CImage Image; Image.Attach((HBITMAP)pFinalBitmap->m\_hObject); Image.Save(ar.GetFile()->GetFileName(), Gdiplus::ImageFormatBMP); bitmap.DeleteObject();
but the resulting bitmap file has 0 bytes ... why ? Could you help me ?
-
I intend to save CBitmap into a bmp file, here is the code:
// MyDocument.cpp
CMyView* pView = (CMyView*)GetNextView(pos);
CDC* pDC = pView->GetDC();CDC MemDC; MemDC.CreateCompatibleDC(pDC); CBitmap bitmap; bitmap.CreateCompatibleBitmap(pDC, GetTotalCx(), GetTotalCy()); CBitmap\* pOldBitmap = (CBitmap\*)MemDC.SelectObject(&bitmap); // draw the MemDC pDC->BitBlt(0, 0, GetTotalCx(), GetTotalCy(), &MemDC, 0, 0, SRCCOPY); CBitmap\* pFinalBitmap = (CBitmap\*)MemDC.SelectObject(pOldBitmap); CImage Image; Image.Attach((HBITMAP)pFinalBitmap->m\_hObject); Image.Save(ar.GetFile()->GetFileName(), Gdiplus::ImageFormatBMP); bitmap.DeleteObject();
but the resulting bitmap file has 0 bytes ... why ? Could you help me ?
-
I intend to save CBitmap into a bmp file, here is the code:
// MyDocument.cpp
CMyView* pView = (CMyView*)GetNextView(pos);
CDC* pDC = pView->GetDC();CDC MemDC; MemDC.CreateCompatibleDC(pDC); CBitmap bitmap; bitmap.CreateCompatibleBitmap(pDC, GetTotalCx(), GetTotalCy()); CBitmap\* pOldBitmap = (CBitmap\*)MemDC.SelectObject(&bitmap); // draw the MemDC pDC->BitBlt(0, 0, GetTotalCx(), GetTotalCy(), &MemDC, 0, 0, SRCCOPY); CBitmap\* pFinalBitmap = (CBitmap\*)MemDC.SelectObject(pOldBitmap); CImage Image; Image.Attach((HBITMAP)pFinalBitmap->m\_hObject); Image.Save(ar.GetFile()->GetFileName(), Gdiplus::ImageFormatBMP); bitmap.DeleteObject();
but the resulting bitmap file has 0 bytes ... why ? Could you help me ?
I don't think that it is the source of your problem, but there is an error in your code: You are deleting the
CBitmap
while it is still attached to theCImage
. So you should detach first or useCImage::Destroy
which will detach and delete the bitmap. You should also check the return value of theSave
function. -
I don't think that it is the source of your problem, but there is an error in your code: You are deleting the
CBitmap
while it is still attached to theCImage
. So you should detach first or useCImage::Destroy
which will detach and delete the bitmap. You should also check the return value of theSave
function.CDC* pDC = pView->GetDC();
int nSaveDC = pDC->SaveDC();CDC MemDC; MemDC.CreateCompatibleDC(pDC); CBitmap bitmap; bitmap.CreateCompatibleBitmap(pDC, GetTotalCx(), GetTotalCy()); CBitmap\* pOldBitmap = (CBitmap\*)MemDC.SelectObject(&bitmap); // draw MemDC pDC->BitBlt(0, 0, GetTotalCx(), GetTotalCy(), &MemDC, 0, 0, SRCCOPY); CBitmap\* pFinalBitmap = (CBitmap\*)MemDC.SelectObject(pOldBitmap); CImage Image; Image.Attach((HBITMAP)pFinalBitmap->m\_hObject); Image.Save(lpszPathName, Gdiplus::ImageFormatBMP); Image.Detach(); pDC->RestoreDC(nSaveDC); bitmap.DeleteObject();
Here is the new code ... but strange, CImage::Save return 0... it is OK ? The saved image looks good ...
-
CDC* pDC = pView->GetDC();
int nSaveDC = pDC->SaveDC();CDC MemDC; MemDC.CreateCompatibleDC(pDC); CBitmap bitmap; bitmap.CreateCompatibleBitmap(pDC, GetTotalCx(), GetTotalCy()); CBitmap\* pOldBitmap = (CBitmap\*)MemDC.SelectObject(&bitmap); // draw MemDC pDC->BitBlt(0, 0, GetTotalCx(), GetTotalCy(), &MemDC, 0, 0, SRCCOPY); CBitmap\* pFinalBitmap = (CBitmap\*)MemDC.SelectObject(pOldBitmap); CImage Image; Image.Attach((HBITMAP)pFinalBitmap->m\_hObject); Image.Save(lpszPathName, Gdiplus::ImageFormatBMP); Image.Detach(); pDC->RestoreDC(nSaveDC); bitmap.DeleteObject();
Here is the new code ... but strange, CImage::Save return 0... it is OK ? The saved image looks good ...
CImage::Save
returns aHRESULT
where zero indicates success. -
CImage::Save
returns aHRESULT
where zero indicates success.