I’m calling the following function every 100ms. It’s goal is to take an image from renderBuffer, resize it and display it in the dialog’s CStatic control with SetBitmap().
Problem is that I’m observing a rather huge spike of memory usage every second when this function is executed. It displays the resized image in CStatis control without a problem, but I see in task manager that each second process allocates additional 4 megabytes of memory and doesn’t ever stop until the process runs out of memory.
Here is the code, let me know if you have any idea what could be the problem.
void CAppDlg::UpdatePreview( const RenderBuffer* renderBuffer )
{
HBITMAP hbmReturn = NULL;
Gdiplus::Bitmap *bmPhoto = NULL;
Gdiplus::Bitmap bmPhoto( THUMBNAIL_WIDTH, THUMBNAIL_HEIGHT );
CBitmap Bmp1;
Gdiplus::Bitmap image( 780, 780, 4*780, PixelFormat32bppARGB, renderBuffer->buffer );
int sourceWidth = image.GetWidth();
int sourceHeight = image.GetHeight();
int destX = 0,
destY = 0;
float nPercent = 0;
float nPercentW = ((float)THUMBNAIL_WIDTH/(float)sourceWidth);;
float nPercentH = ((float)THUMBNAIL_HEIGHT/(float)sourceHeight);
if(nPercentH < nPercentW)
{
nPercent = nPercentH;
destX = (int)((THUMBNAIL_WIDTH - (sourceWidth * nPercent))/2);
}
else
{
nPercent = nPercentW;
destY = (int)((THUMBNAIL_HEIGHT - (sourceHeight * nPercent))/2);
}
int destWidth = (int)(sourceWidth * nPercent);
int destHeight = (int)(sourceHeight * nPercent);
bmPhoto.SetResolution( image.GetHorizontalResolution(), image.GetVerticalResolution() );
Gdiplus::Graphics *grPhoto = Gdiplus::Graphics::FromImage( &bmPhoto );
Gdiplus::Color colorW(255, 255, 255, 255);
grPhoto->Clear( colorW );
grPhoto->SetInterpolationMode( Gdiplus::InterpolationModeHighQualityBicubic );
grPhoto->DrawImage( &image, Gdiplus::Rect(destX, destY, destWidth, destHeight) );
bmPhoto.GetHBITMAP( colorW, &hbmReturn );
m_BitmapPreview.SetBitmap( hbmReturn ); // ---- without this line memory usage doesn't go up rapidly every second.
DeleteObject( hbmReturn ); // ---- returns non-zero which would point out that it was freed properly.
delete grPhoto;
}
Thanks for any help!
Regards.
I guess you should use DeleteObject.
Here is how the code should look like in my opinion: