问题
(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.)
回答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.
回答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.
来源:https://stackoverflow.com/questions/4568057/how-would-i-draw-a-png-image-using-loadimage-and-stretchdibits