Solved

Drawing to a bitmap.

Posted on 2002-05-10
5
444 Views
Last Modified: 2013-11-20
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
Comment
Question by:Iraklis
  • 2
  • 2
5 Comments
 

Author Comment

by:Iraklis
ID: 7003156
Is this question so difficult. Here you are more points.
0
 
LVL 12

Expert Comment

by:migel
ID: 7003196
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
 

Author Comment

by:Iraklis
ID: 7004706
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
 
LVL 9

Expert Comment

by:BeyondWu
ID: 7005058
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
 
LVL 12

Accepted Solution

by:
migel earned 251 total points
ID: 7005361
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Windows Drag & Drop Location 2 108
Replacement selected text 2 63
viewing source code from eclipse 13 110
sum28 challenge 31 115
Introduction: Hints for the grid button.  Nested classes, templated collections.  Squash that darned bug! Continuing from the sixth article about sudoku.   Open the project in visual studio. First we will finish with the SUD_SETVALUE messa…
If you use Adobe Reader X it is possible you can't open OLE PDF documents in the standard. The reason is the 'save box mode' in adobe reader X. Many people think the protected Mode of adobe reader x is only to stop the write access. But this fe…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

860 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question