Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How would I draw a PNG image using LoadImage and StretchDIBits?

(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 an LPDRAWITEMSTRUCT to draw into (so that means you have the HDC and the rectangle for your target), how do you draw that image onto the screen using StretchDIBits? I've worked with BitBlt and TransparentBlt before, but StretchDIBits seems to work differently, and LoadImage 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.)

like image 617
user541686 Avatar asked Sep 20 '25 09:09

user541686


2 Answers

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.

like image 149
Bradley Grainger Avatar answered Sep 22 '25 22:09

Bradley Grainger


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.

like image 42
Greg Hewgill Avatar answered Sep 22 '25 22:09

Greg Hewgill



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!