• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 466
  • Last Modified:

Drawing to a bitmap.

Hi everybody.

 I would like to know how to create a Bitmap which contains the entire view and all the objects displayed n it.

 Note that I'm not asking for a screen capture: the whole idea is drawing the scene n a bitmap instead of n the
screen (as OnDraw does), perhaps creating previously a device context.


I already know how to save a bitmap to a file so do no mess with this point. Please make your answers as explicit
as possible. Code would help.

Hope you know what I mean.


Thanks.
0
Iraklis
Asked:
Iraklis
  • 2
  • 2
1 Solution
 
IraklisAuthor Commented:
Is this question so difficult. Here you are more points.
0
 
migelCommented:
Hi!
it isn`t so difficult
in general you have to make several steps:
1. create memory device context
2. bitmap
3. Select bitmap into the memory DC
4. draw your view using this memory DC
5. deselect bitmap
6. You have bitmap and you can save it to the disk

code example:
CMyView::DrawViewInBitmap()
{
CDC dcMem;
CClientDC dcClient(this);
dcMem.CreateCompatibleDC(&dcClient);
CRect rc;
GetClinetRect(rc);
CBitmap bmView;
bmView.CreateCompatibleBitmap(&dcMem, rc.Width(), rc.Height());
CBitmap* pbmOld = dcMem.SelectObject(&bmView);
OnDraw(&dcMem); // draw in this context
dcMem.SelectObject(pbmOld);
// now you have bitmap
StoreBitmap(&bmView);// save it to disk
}
0
 
IraklisAuthor Commented:
Hi, Miguel.

I forgot to mention (my apologies for this) that I'm using a CSCrollView of wich map mode is set to MM_LOMETRIC. Therefore some functions such OnPrepareDC, LPtoDP and SetMapMode must be added.

On the other hand I doubt about the first parameter in
"CreateCompatibleBitmap". Should it be dcClient instead?

I can add more points if required.

Thanks.
0
 
BeyondWuCommented:
I don't understand you clearly.Are you want to convert a window which maybe have subwindow to a bitmap? If so, see this:

HBITMAP hBitMap = 0;
BOOL GetWndBmp()
{
   if(hBitMap)
      DeleteObject(hBitMap);

   HDC hCatchDC = CreateCompatibleDC(NULL);
   HDC hTempDC = ::GetDC(NULL);
   RECT rect;
   ::GetWindowRect(m_hWnd/*your window handle*/, &rect);
   int nWidth = rect.right - rect.left;
   int nHeight = rect.bottom - rect.top;
   hBitMap = CreateCompatibleBitmap(hTempDC, nWidth, nHeight);
   HBITMAP oldbmp = (HBITMAP)SelectObject(hCatchDC, hBitMap);
   // let the window draw itself on my hCatchDC
   SendMessage(WM_PRINT, (WPARAM)hCatchDC, PRF_CHILDREN | PRF_CLIENT | PRF_ERASEBKGND | PRF_NONCLIENT | PRF_OWNED);

   ::ReleaseDC(NULL, hTempDC);

   SelectObject(hCatchDC, oldbmp);
   DeleteDC(hCatchDC);
   return TRUE;
}

0
 
migelCommented:
oops
of course you must use clientDC
bmView.CreateCompatibleBitmap(&dcClient, rc.Width(), rc.Height());
and add
     OnPrepareDC(&dcMem);
before OnDraw(&dcMem);
in general all preparation for drawing such as: SetMapMode, SetViewportExt and so on, you must do in the OnPrepareDC so no other preparation in my function need.
0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now