Magick++ Bitmap::GetThumbnailImage() 阻止文件,无法删除它



各位程序员大家好。

首先,我的C/C++经验只有5周。(我目前是一名实习生,通常在我真正的工作中使用 Java(

我正在开发一个读取.tif文件并将其显示给用户的应用程序。用户查看完图像后,应将其关闭并从硬盘中删除。

问题是关闭图像后我无法删除图像。我认为图像进程仍在运行,这拒绝我的程序删除该文件。

我的猜测是,即使我在位图上使用delete pCloneBmp;,Gdiplus也会以某种方式保持文件打开状态。有没有人对 Gdiplus 有想法或尝试过类似的东西?

我尝试了这段代码来删除文件(下一个代码块中的解释(:

delete pCloneBmp;
delete gdiZoomedLoaded;
delete gdiOrgLoaded;
DeleteObject( bmpLoaded );
remove( filenames.at( 0 ).c_str( ) );
WORD error = GetLastError( ); 
// error codes:
// 0 == Success
// 32 == The process cannot access the file because it is being used by another process. 

这是我尝试做的:

// This is how I call the function.
scale_image( (Gdiplus::Bitmap*)gdiOrgLoaded , x_bmp , y_bmp , org_width_bmp , org_height_bmp , scale_bmp );
// vvv Function definition vvv
HBITMAP scale_image( Gdiplus::Bitmap *old_bmp , int x , int y , int width ,  int height , float scale )
{
HBITMAP result = NULL;
int newWidth = width * scale;
int newHeight = height * scale;
Gdiplus::RectF bmpRect( 0 , 0 , newWidth , newHeight );
Gdiplus::Bitmap *pCloneBmp = old_bmp->Clone( 0 , 0 , width , height , old_bmp->GetPixelFormat() );
// If I run the deletion code here the error code is 0
// and the file gets deleted
// vvv It seems that this line causes the problem vvv
pCloneBmp = ( Gdiplus::Bitmap* )pCloneBmp->GetThumbnailImage( newWidth , newHeight );
// If I run the deletion code here the error code is 32
// and the file won't be deleted
Gdiplus::Graphics *pGraphics = Gdiplus::Graphics::FromImage( pCloneBmp );
pGraphics->DrawImage( pCloneBmp , bmpRect , 0 , 0 , newWidth , newHeight , Gdiplus::UnitPixel );
if ( pCloneBmp )
{
    pCloneBmp->GetHBITMAP( Gdiplus::Color( 255 , 255 , 255 ) , &result );
}
delete( gdiZoomedLoaded );
gdiZoomedLoaded = pCloneBmp;
delete pGraphics;
return result;
}

任何帮助都非常感谢:-(

编辑:这是我加载图像的方式

gdiOrgLoaded = Gdiplus::Image::FromFile( pFilePath , false );
gdiZoomedLoaded = Gdiplus::Bitmap::FromFile( pFilePath , false );
HBITMAP result; // <-- This is bmpLoaded
gdiZoomedLoaded->GetHBITMAP( Gdiplus::Color( 255 , 255 , 255 ) , &result );
所以我

确实想通了。正如@HansPassant所暗示的那样,这确实是一些内存泄漏。

这是我的工作职能。我也设法缩短了很多时间。

HBITMAP scale_image( Gdiplus::Bitmap *old_bmp , int x , int y , int width , int height , float scale )
{
HBITMAP result = NULL;
int newWidth = width * scale;
int newHeight = height * scale;
Gdiplus::Bitmap* newBitmap = new Gdiplus::Bitmap( newWidth , newHeight , old_bmp->GetPixelFormat( ) );
Gdiplus::Graphics *pGraphics = Gdiplus::Graphics::FromImage( newBitmap );
pGraphics->DrawImage( old_bmp , 0 , 0 , newWidth , newHeight );
delete( gdiZoomedLoaded );
gdiZoomedLoaded = newBitmap;
delete pGraphics;
return result;
}

最新更新