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

时间:2017-05-10 15:24:55

标签: c++ winapi gdi+

Hello其他程序员。

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

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

问题是关闭后我无法移除图像。 我认为图像处理仍在运行,并且拒绝我的程序删除文件。

我的猜测是Gdiplus以某种方式保持文件打开,即使我在Bitmap上使用了delete pCloneBmp;。有没有人有想法或尝试过与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 );

0 个答案:

没有答案