Click here to Skip to main content
15,896,063 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I am using the following piece of code to get a snapshot of the DC and convert it to a bitmap, however some objects on my DC do not show up or are black boxes instead of the objects. I think there is layering going on here and some objects are not getting picked up for some reason, anyway to get the last DC that would have all objects in it?


C++
  HDC hdcScreen;
CDC * dcWindow;

dcWindow = this->GetDC();
CClientDC dc7(this);
CDC         memDC;
//memDC.CreateCompatibleDC(&dc7);
memDC.CreateCompatibleDC(dcWindow);

memDC.SetMapMode(MM_ANISOTROPIC);
memDC.SetViewportExt(2,2);
memDC.SetWindowExt(3,3);

CPreview p;
p.m_Bmp.DeleteObject();
p.m_Bmp.CreateCompatibleBitmap(&memDC,m_lWidth*2/3,m_iHight*2/3);

BITMAP bmpinfo;
p.m_Bmp.GetBitmap(&bmpinfo);
int xb=bmpinfo.bmWidthBytes;

byte *buffer=new byte[xb*m_iHight];
p.m_Bmp.GetBitmapBits(xb*m_iHight,buffer);
for (long i=0;i<xb*m_iHight;i++)
    buffer[i]=0xff;
p.m_Bmp.SetBitmapBits(xb*m_iHight,buffer);


CBitmap* d=memDC.SelectObject(&p.m_Bmp);



Thanks,
Shirzad
Posted
Comments
KarstenK 7-Jun-15 4:33am    
I cant see the problem in your code. And shouldnt store a DC because it is some handle to graphics memory which isnt fix. Your for-loop is overriding the memory. Do you really want that?
Shirzad Sharif 7-Jun-15 5:12am    
your correct, the code does work fine most of the time, however for some objects which I have added that display correctly normally on the canvas they seem to show up as black boxes when this piece of code executes.

What I mean is that all the objects are returned in the resulting captured image but then those specific images that I mentioned above then show up as black boxes.

I think these objects are probably drawn on another Device Context which might be on top of the captured Device Context that does not get returned by (this) which might be why they show up as black boxes...

Does that make sense? Thanks a lot for your reply, really appreciate your help!
Shirzad Sharif 10-Jun-15 1:21am    
A helpful link with some examples and a similar issue...

http://stackoverflow.com/questions/29635393/screen-capture-only-returns-a-black-image
KarstenK 12-Jun-15 14:08pm    
It sounds like some of your objects/handles go out of scope. Be sure to save all bits befor the DC got invalid/deleted. Sometime it helps to write the Bitmap on disk and check it.

1 solution

Determine the window's bounding rectangle in screen coordinates, retrieve the screen DC and extra the pixels within the bounding rectangle. This will be the "top most" DC / pixels to create your bitpmap.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900