How can i free hbitmap

戏子无情 提交于 2019-12-10 10:27:52

问题


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);
    }

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

Thank you,


回答1:


I assume you are using C#.

You can use DeleteObject to delete this HBITMAP.

Declare it like this:

using System.Runtime.InteropServices class API { [DllImport("WINGDI32.dll")] public static extern int DeleteObject(IntPtr hObject); }

And delete the object:

API.DeleteObject(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.



来源:https://stackoverflow.com/questions/16944048/how-can-i-free-hbitmap

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!