-
September 25th, 2015, 07:43 AM
#1
Saving view
I have an application that compound more CBitmaps into single one, and all of them I spread it into CScrollView. How can I save entire view (helped by CImage), but not just visible part in window client ? I saw on internet a lot of examples of how to save CDC, but just visible part ... in my case this is not suitable ... could you help me ?
-
September 27th, 2015, 02:20 AM
#2
Re: Saving view
You save that absolutely same way: create memory dc, create compatible bitmap, draw there as you wish, detach bitmap from dc, save bitmap to file.
Best regards,
Igor
-
October 3rd, 2015, 05:33 AM
#3
Re: Saving view
Once you have the (composed) bitmap, there's no sweat to attach it to a CImage object, then save it in a file.
Example
Code:
HRESULT CFoo::SaveCBitmapToFile(CBitmap* pBitmap, LPCTSTR szFilePath)
{
ASSERT_VALID(pBitmap);
CImage image;
image.Attach((HBITMAP)pBitmap->GetSafeHandle());
HRESULT hr = image.Save(szFilePath);
image.Detach(); // Do not forget this!
return hr;
}
// like a walk in the park.
See
Last edited by ovidiucucu; October 3rd, 2015 at 06:06 AM.
-
October 3rd, 2015, 07:17 AM
#4
Re: Saving view
[ additional note ]
CImage is a wonderful ATL/MFC class which can help you to save lines, time and improve the code readability and maintainability, by making not obviously necessary to write yourself code for creating memory DCs, compatible bitmaps, bit-blitting, and so on, and so on.
Example
Code:
// ...
CImage imageToSave;
imageToSave.Create(nDestWidth, nDestHeight, nBPP);
HDC hDestDC = imageToSave.GetDC();
// fill the destination background
CDC::FromHandle(hDestDC)->FillSolidRect(0, 0, nDestWidth, nDestHeight, crBack);
// draw a bitmap into the destination at coordinates x, y
CImage image1;
image1.Attach((HBITMAP)pBitmap1->GetSafeHandle());
image1.Draw(hDestDC, x, y);
// draw anything else your muscles want
// ...
// save the destination image to a file
HRESULT hr = imageToSave.Save(pszFilePath);
image1.Detach(); // Do not forget Detach if a bitmap was previously attached!
imageToSave.ReleaseDC(); // Do not forget ReleaseDC if GetDC was previously called!
// ...
See also
-
October 7th, 2015, 06:16 AM
#5
Re: Saving view
Sorry for later response ... I have wrote the same code, except one line:
Code:
Image.Attach((HBITMAP)pBitmap->m_hObject);
instead of
Code:
image.Attach((HBITMAP)pBitmap->GetSafeHandle());
I noticed that with my code, I couldn't save extra large images, with yours, I do. Thank you.
Last edited by mesajflaviu; October 16th, 2015 at 03:25 AM.
-
October 8th, 2015, 06:13 AM
#6
Re: Saving view
Originally Posted by mesajflaviu
Sorry for later response ... I have wrote the same code, except one line:
Code:
Image.Attach((HBITMAP)pBitmap->m_hObject);
instead of
Code:
image.Attach((HBITMAP)pBitmap->GetSafeHandle());
Generally, if using pointers, calling CGdiObject::GetSafeHandle is safer than directly accessing the m_hObject member.
In our case, if pBitmap is NULL, pBitmap->GetSafeHandle() will return NULL while pBitmap->m_hObject will lead in an access violation and the application will crash.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|