我如何释放Hbitmap [英] How can i free hbitmap
问题描述
您好,我具有以下功能,此后我将在c#中使用该功能来显示带有"Bitmap.FromHbitmap(IntPtr)"的位图,但经过几次使用后,此操作将导致内存泄漏.
Hello i have the following function which i use afterwards from c# to display a bitmap with "Bitmap.FromHbitmap(IntPtr)" but after a few usages this ends with a memory leak .
IntPtr GetFrame(int Width,int Height,int nFrame)
{
width= Width;
height = Height;
HBITMAP hb;
m_piHelper->GetBmp(width,height,nFrame,&hb);
return IntPtr(hb);
}
可能我应该删除某个对象,但我不知道在返回之前我可以在哪里做,但最终却没有图像,但出现gdi错误
Probably i should deleteobject somewere but i dont know where i could do that i tried before return but i end up with no image but a gdi error
谢谢
推荐答案
我假设您正在使用C#.
I assume you are using C#.
您可以使用DeleteObject
删除此HBITMAP.
You can use DeleteObject
to delete this HBITMAP.
这样声明:
using System.Runtime.InteropServices class API { [DllImport("WINGDI32.dll")] public static extern int DeleteObject(IntPtr hObject); }
并删除对象:
API.DeleteObject(hBitmap);
由于您的函数返回了指向HBITMAP的指针,因此您不能在返回之前将其删除.相反,当不再使用此位图时,应将其删除得足够晚.
As your function returns a pointer to the HBITMAP, you cannot delete it before return. Instead, you should delete it later enough when there will be no more use of this bitmap.
这篇关于我如何释放Hbitmap的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!