Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/159.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ Magick++;位图::GetThumbnailImage()阻止文件,无法删除它_C++_Winapi_Gdi+ - Fatal编程技术网

C++ Magick++;位图::GetThumbnailImage()阻止文件,无法删除它

C++ Magick++;位图::GetThumbnailImage()阻止文件,无法删除它,c++,winapi,gdi+,C++,Winapi,Gdi+,各位程序员好 首先,我的C/C++经验只有5周。 (我目前是一名实习生,在实际工作中通常使用Java) 我正在开发一个可以读取.tif文件并将其显示给用户的应用程序。用户查看完图像后,应将其关闭并从硬盘中删除 问题是我无法在关闭图像后删除它。 我认为映像进程仍在运行,这使我的程序无法删除该文件 我猜Gdiplus会以某种方式保持文件打开,即使我使用了deletepclonebmp。有没有人对Gdiplus有过类似的想法或尝试 我尝试用这段代码删除该文件(在下一个代码块中解释): 以下是我试图做的

各位程序员好

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

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

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

我猜Gdiplus会以某种方式保持文件打开,即使我使用了
deletepclonebmp。有没有人对Gdiplus有过类似的想法或尝试

我尝试用这段代码删除该文件(在下一个代码块中解释):

以下是我试图做的:

// 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 );
gdiOrgLoaded=Gdiplus::Image::FromFile(pFilePath,false);
gdizoomedload=Gdiplus::Bitmap::FromFile(pFilePath,false);
HBITMAP结果;//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;
}

不显示打开文件的位置/方式。如果
old\u bmp
打开文件-您需要删除它哦,对不起,我在问题中添加了它<在本例中,code>old\u bmp
gdiOrgLoaded
。这只是一个普通的错误,您在赋值时丢失了原始pCloneBmp指针值。因为您没有删除它,所以您有一个内存泄漏,其最明显的副作用是文件没有关闭。使用另一个变量。
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;
}