Magick++ Bitmap::GetThumbnailImage() 块文件,无法删除它
Magick++ Bitmap::GetThumbnailImage() blocks file, unable to delete it
各位程序员大家好。
首先我的 C/C++ 经验只有 5 周。
(我目前是一名实习生,通常在我的实际工作中与 Java 一起工作)
我正在开发一个读取 .tif 文件并将其显示给用户的应用程序。用户查看完图像后,应将其关闭并从硬盘中删除。
问题是我无法在关闭图像后将其删除。
我认为图像处理仍然是 运行 并且拒绝我的程序删除文件。
我猜 Gdiplus 以某种方式保持文件打开,即使我在位图上使用了 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 );
所以我确实弄明白了。正如@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;
}
各位程序员大家好。
首先我的 C/C++ 经验只有 5 周。 (我目前是一名实习生,通常在我的实际工作中与 Java 一起工作)
我正在开发一个读取 .tif 文件并将其显示给用户的应用程序。用户查看完图像后,应将其关闭并从硬盘中删除。
问题是我无法在关闭图像后将其删除。 我认为图像处理仍然是 运行 并且拒绝我的程序删除文件。
我猜 Gdiplus 以某种方式保持文件打开,即使我在位图上使用了 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 );
所以我确实弄明白了。正如@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;
}