Click here to Skip to main content
15,886,026 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to get pixel from a temporary DC like this:

C++
COLORREF Myfunction(CPoint p)
{
  CBitmap bmpCOM;
  bmpCOM.LoadBitmap(IDB_BMP2);
  CDC dcCOM;
  dcCOM.CreateCompatibleDC(NULL);
  dcCOM.SelectObject(bmpCOM);
  HDC hdcCOM=dcCOM.GetSafeHdc();
  COLORREF clr=GetPixel(hdcCOM,point.x,point.y);
}

My control loaded a bitmap with id: "IDB_BMP1"
but when i click on the control to get the pixel of "IDB_BMP2", the window tell me that i got problem with memory.

I really don't know why. because my code is very simple.
I did get pixel from the ClientDC of this control, but can't get pixel of the hidden dc at all.
Posted
Updated 1-Oct-15 1:50am
v3

Which kind of memory problem occurs (is there an error message)?

Some notes:

  • The SelectObject parameter is a pointer to an object
  • The bitmap must be delesected before it is destroyed (by the destructor)
  • You may check the return values to detect errors immediately when they occur

So change the code to (using debug macros to detect errors and calling CDC::GetPixel):
COLORREF Myfunction(CPoint p)
{
    CBitmap bmpCOM;
    // Returns FALSE upon error
    VERIFY(bmpCOM.LoadBitmap(IDB_BMP2));
    CDC dcCOM;
    // Returns FALSE upon error
    VERIFY(dcCOM.CreateCompatibleDC(NULL));
    // Pass pointer to bmpCOM and save return value for deselection
    CBitmap *pOldBm = dcCOM.SelectObject(&bmpCOM);
    // NULL indicates error
    ASSERT(pOldBm);
    COLORREF clr = dcCOM.GetPixel(p);
    ASSERT(clr != (COLORRREF)-1);
    // Deselect bitmap
    dcCom.SelectObject(pOldPm);
    return clr;
}
 
Share this answer
 
Comments
Tokia 1-Oct-15 21:54pm    
Windows has triggered a breakpoint in Constainer.exe.

This may be due to a corruption of the heap, which indicates a bug in Constainer.exe or any of the DLLs it has loaded.

This may also be due to the user pressing F12 while Constainer.exe has focus.

The output window may have more diagnostic information.

Above is the bug that i got when i debug.
Jochen Arndt 2-Oct-15 6:11am    
This may have been sourced by the bitmap destructor when the bitmap is still selected.
Tokia 1-Oct-15 22:22pm    
I think i solved the PROBLEM with memory but still can't get pixel from another DC.
Thank for helping about memory.
Jochen Arndt 2-Oct-15 6:14am    
While there was a corrupted heap it makes no sense to fix other errors. But what exactly is the problem? Is GetPixel failing?

Or is the value not as expected? Then keep in mind that you have two bitmaps (which may be different). Also which point is used? The memory bitmap uses zero based coordinates while on screen points may have an offset.
Tokia 5-Oct-15 4:41am    
i don't know but it only vie me the location of memory is violent to access.
But i solved tried clean up to prevent memory leak and it's ok now. Thanks for reading and help me.
There's a scaring note in the CBitmap::GetPixel documentation[^]:
CAUTION: Before you delete the object, make sure it is not selected into a device context.
 
Share this answer
 
Comments
Tokia 1-Oct-15 9:18am    
???
I really don't get it. The link is GetPIXEL but actually go to LoadBitMap.
And as u saw, i did use them in my function.
I did not delete anything( i am don't know how and why to delete). I am newbie for C++ and MFC.

I loaded on the clientDC a bitmap.
But when i try to get the 2nd bitmap from the temporary( so that i can get some part of this map appear on the first bitmap in clientDC), i got some bug with memory management.

I searched on the google but could get anything because VS only give me the local of memory that get bug.

Sorry, i am newbie who knows nothing T_T.

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