How to download an image from a resource using GDI +?

I am trying to load a non-bmp image (png in my case) into a Bitmap / Image instance from a resource in my application. Since the Bitmap constructor only has an overload for the bitmap resource, this is what I found:

I allocate memory in the global heap and then copy the resource data into it. Then I create an IStream for this global memory block (using CreateStreamOnHGlobal) and use the Image / Bitmap constructor that accepts this stream. This basically works, although I'm not sure if this is the best way to do this: I noticed that if I free this memory block after creating the image, it will not be drawn (calling DrawImage will not do anything). Which raises two questions:

  • How can I control the lifetime of this memory block? I doubt that he will be freed from the destruction of the image.

  • Does the Image / Bitmap class use png data in compressed form and translate it into raw data with every DrawImage call? Seems very ineffective.

Any sugestions?

+3
source share
2 answers

When using resources without a bitmap, I based my code on codeproject .

IStream* pStream = NULL;
::CreateStreamOnHGlobal(m_hBuffer, FALSE, &pStream)
m_pBitmap = Gdiplus::Bitmap::FromStream(pStream);
pStream->Release();

Looking at this code, as soon as you make a stream from a stream to create a bitmap, you call Release()in the stream to remove the counter of links to the stream, thereby linking the lifetime of the stream to the bitmap.

, .

+2
0

Source: https://habr.com/ru/post/1707162/


All Articles