Solved

Drawing to a bitmap.

Posted on 2002-05-10
5
445 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Introduction: Database storage, where is the exe actually on the disc? Playing a game selected randomly (how to generate random numbers).  Error trapping with try..catch to help the code run even if something goes wrong. Continuing from the seve…
Introduction: Dialogs (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
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.
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

733 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