Solved

Drawing to a bitmap.

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Error on moodle after upgrade 3 106
ORA-01403: no data found 43 82
Use of condition with 'serial' in ansible 2 36
wordmultiple challenge 12 90
Introduction: Ownerdraw of the grid button.  A singleton class implentation and usage. Continuing from the fifth article about sudoku.   Open the project in visual studio. Go to the class view – CGridButton should be visible as a class.  R…
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…
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.
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.

706 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now