[英]How do I release a BITMAP struct?
After using a BITMAP struct to get a bitmap's information... 使用BITMAP结构获取位图的信息后...
BITMAP bm;
HBITMAP hBitmap;
hBitmap = (HBITMAP)LoadImage(...);
GetObject(phBitmap, sizeof(BITMAP), &bm );
How am I supposed to release/free/dispose bm
? 我应该如何释放/释放/处置
bm
?
Tried with delete
and DeleteObject
, none worked. 尝试过
delete
和DeleteObject
,没有一个起作用。
I'd say this 我会这样说
HBITMAP hBitmap;
hBitmap = (HBITMAP)LoadImage(...);
should better/nicer be 应该更好/更好
HANDLE handle = LoadImage(...);
if (NULL == handle)
{
/*handle error */
}
else
{
HBITMAP hbitmap = handle;
{
BITMAP bm = {0}; /* allocate and
initialise bm on the stack. */
GetObject(hbitmap, sizeof bm, &bm);
/* Use bm here. */
} /* Have bm be deallocated or at least
be inaccessible from here on. */
DeleteObject(hbitmap);
}
It is the handle that refers to the resource, which should be freed when not used anymore. 它是引用资源的句柄,当不再使用时应将其释放。
You would only need to use the operator delete
if you had allocated the BITMAP
with the operator new
. 如果您为
BITMAP
分配了new
则只需要使用operator delete
。 (Modern, idiomatic C++ discourages manual memory management as well, but since you asked about this in your question, there you go). (现代的,惯用的C ++也不鼓励手动进行内存管理,但是既然您在问题中对此提出了疑问,那么您就可以了)。
A BITMAP
is a struct of POD values dimensions and pixel data, and an HBITMAP
can be a handle to a BITMAP
if it is assigned as such. BITMAP
是POD值维和像素数据的结构,如果按这样分配,则HBITMAP
可以是BITMAP
的句柄。 An HBITMAP
does need to be released with a call to DeleteObject
when it's no longer needed and no longer selected into a DC, even if it was allocated on the stack; HBITMAP
确实需要在不再需要且不再选择到DC中时通过调用DeleteObject
来释放,即使已将其分配在堆栈中也是如此; a BITMAP
on the stack will disappear when it goes out of scope. 超出范围时,堆栈上的
BITMAP
将消失。
There is a difference between the operator delete
and the Windows GDI function DeleteObject
. 操作员
delete
和Windows GDI函数DeleteObject
之间有区别。 delete
is a C++ keyword for removing objects from the heap; delete
是用于从堆中删除对象的C ++关键字; DeleteObject
is an API call for freeing GDI resources. DeleteObject
是用于释放GDI资源的API调用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.