Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Problems painting into memory device context with GDI

Posted on 2004-08-26
11
192 Views
Last Modified: 2013-11-20
Here is a snippet of some GDI code, which uses a memory device context and copies it onto the view device context.  It appears to work fine but it's wrong since the old bitmap pointer is not saved:

CDC xdc;
CBitmap xbitmap;

xdc.CreateCompatibleDC(pDC);
xbitmap.CreateCompatibleBitmap(pDC, tex, wry);
xdc.SelectObject(xbitmap);
//...Draw onto xdc
//...BitBlt is used

Here is the code including the fixed resource leak.  It creates an error when SelectObject does not return a valid CBitmap pointer.

xdc.CreateCompatibleDC(pDC);
xbitmap.CreateCompatibleBitmap(pDC, tex, wry);
CBitmap* oldbitmap = xdc.SelectObject(xbitmap);
//...Draw onto xdc
//...BitBlt is used
//...
xdc.SelectObject(oldbitmap);

Of course, SelectObject takes a pointer to the object, so here is the fixed line.  Now it doesn't work at all; after the BitBlt operaton, the view is still empty.

CBitmap* oldbitmap = xdc.SelectObject(&xbitmap);

What am I doing wrong with this GDI code?
0
Comment
Question by:Alkali_Guy
  • 7
  • 2
11 Comments
 
LVL 6

Expert Comment

by:nabehs
ID: 11909327
i dont see anything wrong with the code, maybe the problem in BitBlt. can u post it?
0
 
LVL 5

Author Comment

by:Alkali_Guy
ID: 11909501
pDC->BitBlt( 0, 0, mex, cry, &xdc, 0, 0, SRCCOPY );
0
 
LVL 5

Author Comment

by:Alkali_Guy
ID: 11909517
I have only seen this code work when I pass the xbitmap object to SelectObject, instead of passing its pointer the way I am supposed to.
0
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 5

Author Comment

by:Alkali_Guy
ID: 11909542
Other information:
Similar code later in the same function works fine, so what is currently happening is:

memdc   >-BitBlt->  xdc  >-BitBlt->  *pDC

I have also tryed CreateCompatibleBitmap passing &xdc instead of pDC.
0
 
LVL 6

Expert Comment

by:nabehs
ID: 11909557
R U declaring xbitmap as CBitmap* xbitmap? It should be CBitmap xbitmap; I know that it is posted here correctly but maybe in your original code it is not.

0
 
LVL 5

Author Comment

by:Alkali_Guy
ID: 11909600
xbitmap is declared correctly in the code.
0
 
LVL 5

Author Comment

by:Alkali_Guy
ID: 11916041
To reiterate the code where the fault is:

xdc.CreateCompatibleDC(pDC);
xbitmap.CreateCompatibleBitmap(pDC, x, y);
CBitmap* pOldBitmap = xdc.SelectObject( &xbitmap );

//Draw stuff

pDC->BitBlt( 0, 0, x, y, &xdc, 0, 0, SRCCOPY );
xdc.SelectObject(oldbitmap);

Note that x and y are not necessarily the width and height of *pDC.  Do I have to convert coordinates before use?
0
 
LVL 5

Author Comment

by:Alkali_Guy
ID: 11916058
[The last line in above code should read:
xdc.SelectObject(pOldBitmap);]
0
 
LVL 5

Accepted Solution

by:
Alkali_Guy earned 0 total points
ID: 11916200
I found the problem:
I accidentally restored the pOldBitmap (oldbitmap) into xdc before BitBlting it onto the screen.  Since the above code is correct, there was no indication here of incorrect order in my own code.
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying 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
notReplace  challenge 53 141
string initialization in java 11 115
fizzArray2 challenge 1 90
NotAlone Challenge 20 84
In this article, I'll describe -- and show pictures of -- some of the significant additions that have been made available to programmers in the MFC Feature Pack for Visual C++ 2008.  These same feature are in the MFC libraries that come with Visual …
Introduction: Displaying information on the statusbar.   Continuing from the third article about sudoku.   Open the project in visual studio. Status bar – let’s display the timestamp there.  We need to get the timestamp from the document s…
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.
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

840 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