Note that there are some explanatory texts on larger screens.

plurals
  1. POCorrectly release resources after flipping a bitmap
    primarykey
    data
    text
    <p>I have an application that writes a very large data set as a bitmap. [~500MB] I am writing the data patch wise from top to down. Due to the nature of BMP's file structure it must be flipped once written to the disk. (I wrote it this way because I assumed that flipping a bitmap would be a common application and I would find libraries to do the task)</p> <p>I am using a code snippet I found on the internet to flip the bitmap. This one:</p> <pre><code>// GetInvertedBitmap - Creates a new bitmap with the inverted image // Returns - Handle to a new bitmap with inverted image // hBitmap - Bitmap to invert // bLateral - Flag to indicate whether to invert laterally or vertically HBITMAP CRisatImport::GetInvertedBitmap( HBITMAP hBitmap, BOOL bLateral ) { // Create a memory DC compatible with the display CDC sourceDC, destDC; sourceDC.CreateCompatibleDC( NULL ); destDC.CreateCompatibleDC( NULL ); // Get logical coordinates BITMAP bm; ::GetObject( hBitmap, sizeof( bm ), &amp;bm ); // Create a bitmap to hold the result HBITMAP hbmResult = ::CreateCompatibleBitmap(CClientDC(NULL), bm.bmWidth, bm.bmHeight); // Select bitmaps into the DCs HBITMAP hbmOldSource = (HBITMAP)::SelectObject( sourceDC.m_hDC, hBitmap ); HBITMAP hbmOldDest = (HBITMAP)::SelectObject( destDC.m_hDC, hbmResult ); if( bLateral ) destDC.StretchBlt( 0, 0, bm.bmWidth, bm.bmHeight, &amp;sourceDC, bm.bmWidth-1, 0, -bm.bmWidth, bm.bmHeight, SRCCOPY ); else destDC.StretchBlt( 0, 0, bm.bmWidth, bm.bmHeight, &amp;sourceDC, 0, bm.bmHeight-1, bm.bmWidth, -bm.bmHeight, SRCCOPY ); // Reselect the old bitmaps ::SelectObject( sourceDC.m_hDC, hbmOldSource ); ::SelectObject( destDC.m_hDC, hbmOldDest ); return hbmResult; } </code></pre> <p>The problem is that I have a limited understanding of the above code. I tried to write a function to use the above snippet the best I could from sample code from MSDN. I do not think I am releasing all resources correctly. And I cant seem to figure out the error - mainly because my lack of knowledge about GDI. I would really appreciate it if some one pointed out what I am doing wrong.</p> <p>The program crashes if I try to call this function twice - that is why I suspect that I am releasing resources incorrectly.</p> <p>This is the function I wrote:</p> <pre><code>void CRisatImport::flipBitMapSaveAsJPG( CString outputFileName, CString outputJPGName, bool saveAsJpg) { // Flip the bitmap to correct odd file structure HBITMAP hBitmap; hBitmap = (HBITMAP)::LoadImage(NULL, outputFileName, IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE); hBitmap = GetInvertedBitmap( hBitmap, FALSE ); CImage image; image.Attach(hBitmap); image.Save(outputFileName,Gdiplus::ImageFormatBMP); if(saveAsJpg){ image.Save(outputJPGName,Gdiplus::ImageFormatJPEG); } image.Destroy(); DeleteObject( hBitmap ); } </code></pre> <p>I am using MFC and VS2010 for this application - I have 4GB of RAM and no other applications running.</p> <p>This is the error I get:</p> <p><a href="http://s7.postimage.org/97xswxd23/Capture.jpg" rel="nofollow noreferrer">Error http://s7.postimage.org/97xswxd23/Capture.jpg</a></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload