Question : CImage ReleaseDC Exception Mystery

I have been applying the CImage class within a Visual C++/MFC DLL.  I am experiencing a mysterious exception in atlimage.h line 1217 when I call CImage::GetPixel(0,0).  The offending statement is:

hBitmap = HBITMAP( ::SelectObject( m_hDC, m_hOldBitmap ) );
ATLASSERT( hBitmap == m_hBitmap );

I have successfully loaded an 8x13 jpg 24 bpp image from disk and the pixel value is successfully read, yet the m_hOldBitmap member is null and thus SelectObject returns null.  The m_hBitmap member is valid and thus does not match hBitmap.

Has anyone experienced this error and found a solution?

Thanks!

Answer : CImage ReleaseDC Exception Mystery

Please disregard the question.  I found that it was operator error with CImage use.

If you use the CImage assignment operator, CImage a = CImage b, it merely copies the HBITMAP value from b to a.  But when b is destroyed, it destroys the HBITMAP as well.  But, a has no idea this has occurred and any subsequent operations will result in exceptions being generated.

The proper way to assign from one CImage to another is to use CImage::Attach and CImage::Detach functions.  This only moves the HBITMAP from one class to the next, but prevents it from being shared between two classes.
Random Solutions  
 
programming4us programming4us