How would I draw a PNG image using LoadImage and StretchDIBits?
Solution 1
I think the PNG support inside bitmaps is only really for use by printer drivers. For screen display, you will probably have to decompress the PNG data yourself using some code such as libpng.
Solution 2
I wrote a blog post (on displaying a splash screen with C++) that contains a full code sample that loads a PNG from a resource and converts it to an HBITMAP (using WIC). You could then use CreateCompatibleDC, SelectObject, and BitBlt to display it in the target HDC.
While I haven't ever tried it, you should be able to use StretchDIBits to display the PNG directly if you load the raw bytes from the resource into memory, and set up a BITMAPINFOHEADER struct with the width, height, etc. as demonstrated in this MSDN sample.
C# and VB.NET solutions would be quite different because you could use System.Drawing (a wrapper for GDI+) or System.Windows.Media.Imaging (in a WPF app) to handle the image loading and drawing.
user541686
Updated on June 28, 2022Comments
-
user541686 almost 2 years
(This is related to the question on How would I load a PNG image using Win32/GDI (no GDI+ if possible)?.)
Hi all,
I was wondering, given that you have a PNG resource embedded in a binary file with the ID
IDB_PNG1
, and anLPDRAWITEMSTRUCT
to draw into (so that means you have theHDC
and the rectangle for your target), how do you draw that image onto the screen usingStretchDIBits
? I've worked withBitBlt
andTransparentBlt
before, butStretchDIBits
seems to work differently, andLoadImage
doesn't seem to work with PNG's.Thank you!
(Note: The language I put is C++, but that's not too important... C, C#, Visual Basic, etc. would be fine; I'm just trying to figure out the steps, not the exact code needed.)