-
Apr 13th, 2024, 11:22 AM
#1
Thread Starter
Fanatic Member
Use GDIPlus to display image from a resource file?
Is it possible to read an image (ICO, BMP, PNG, etc) from a resource file and display it using GDIPlus?
I've found some code to get an image from a resource file and display it using other APIs "LoadIconWithScaleDown, and OleCreatePictureIndirect" but the quality doesn't appear to be as sharp as the GDIplus functions produce.
-
Apr 13th, 2024, 11:37 AM
#2
Re: Use GDIPlus to display image from a resource file?
GdipCreateBitmapFromResource
Or you can load them and get an HICON or HBITMAP... GdipCreateBitmapFromHICON, GdipCreateBitmapFromHBITMAP.
-
Apr 13th, 2024, 01:55 PM
#3
Re: Use GDIPlus to display image from a resource file?
Yeah, I've never used the GdipCreateBitmapFromResource. But, for me, this is two separate issues.
- Loading an image from resources and getting it into a StdPicture object.
- Knowing enough about GdiPlus to get that StdPicture into a PictureBox using it.
I've done both of those, but I'm not sure I've done them both in the same project. The times I've messed with GdiPlus, I was dealing with either PNG or TGA files, and I was doing things with the alpha channel within those files. Without some rough trickery, the GdiPlus is about the only way we can "get at" the alpha channel.
But, there's plenty of stuff in these forums about doing either/both of those steps.
Any software I post in these forums written by me is provided "AS IS" without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. To all, peace and happiness.
-
Apr 13th, 2024, 02:26 PM
#4
Thread Starter
Fanatic Member
Re: Use GDIPlus to display image from a resource file?
 Originally Posted by Elroy
Yeah, I've never used the GdipCreateBitmapFromResource. But, for me, this is two separate issues.
- Loading an image from resources and getting it into a StdPicture object.
- Knowing enough about GdiPlus to get that StdPicture into a PictureBox using it.
I've done both of those, but I'm not sure I've done them both in the same project. The times I've messed with GdiPlus, I was dealing with either PNG or TGA files, and I was doing things with the alpha channel within those files. Without some rough trickery, the GdiPlus is about the only way we can "get at" the alpha channel.
But, there's plenty of stuff in these forums about doing either/both of those steps.
Actually I think that's the approach I DON'T want to take. I've got code which does that. It uses LoadIconWithScaleDown to load the image from resource file, and OleCreatePictureIndirect to create a stdPicture object which I can display in a Picture Box. That works OK but the images are a bit fuzzy. It's not that the image is being scaled down too much as the RES file contains multiple sizes for each ICO file and there's one in there that matches the requested size. So no scaling should be happening.
I've also got some GDI Plus code which reads a file from disk (the same file as is in the resource file but just not in the RES file) which displays the same image but it's MUCH sharper when done this way. That's why I'm looking for a pure GDI Plus solution which can handle different image formats in the RES file (BMP, GIF, JPG, ICO, PNG).
Hope I'm describing these things properly. I'm a total novice at these concepts and simply comparing two different approaches I've seen in some third party code and the quality differences I'm seeing when loading the same image in two different ways.
Last edited by AAraya; Apr 13th, 2024 at 03:18 PM.
-
Apr 13th, 2024, 02:47 PM
#5
Re: Use GDIPlus to display image from a resource file?
 Originally Posted by AAraya
That's why I'm looking for a pure GDI solution ...
You need to be aware that GDI and GdiPlus are two VERY different things.
Any software I post in these forums written by me is provided "AS IS" without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. To all, peace and happiness.
-
Apr 13th, 2024, 03:19 PM
#6
Thread Starter
Fanatic Member
Re: Use GDIPlus to display image from a resource file?
 Originally Posted by Elroy
You need to be aware that GDI and GdiPlus are two VERY different things.
Thank you. Yes, I'm aware of that - just misspoke but appreciate you making sure I was aware of the distinction.
-
Apr 13th, 2024, 03:22 PM
#7
Re: Use GDIPlus to display image from a resource file?
You can use the "LoadResData" function to load an image resource (BMP, GIF, JPG, PNG) into a ByteArray and display it in a PictureBox:
Code:
With New WIA.Vector
.BinaryData = ByteArray: Set PictureBox1.Picture = .Picture
End With
As for ICO resources you could load them from the resources with "LoadIcon" and then use "GetIconInfo" to obtain a hBitmap which you could pass to "OleCreatePictureIndirect" to put it in a PictureBox.
-
Apr 14th, 2024, 02:34 AM
#8
Re: Use GDIPlus to display image from a resource file?
My idea for this: LoadResData -> ByteArray -> SHCreateMemStream -> IStream -> GdipLoadImageFromStream -> GdipCreateHBITMAPFromBitmap -> OleCreatePictureIndirect
-
Apr 14th, 2024, 07:58 AM
#9
Re: Use GDIPlus to display image from a resource file?
yeah. like Franky I also use "kind of" the same procedure.
using GdipLoadImageFromStream ObjPtr(Stream), Image and GdipLoadImageFromFile StrPtr(Filename$), Image
to create an "image" depending if reading from a file or from memory.
after that I do convert the image into a hdc so I can use alphablend/bitblt when I render. surely u can use Gdi+ to render directly into a picturebox if u want.
-
Apr 15th, 2024, 11:18 AM
#10
Thread Starter
Fanatic Member
Re: Use GDIPlus to display image from a resource file?
 Originally Posted by -Franky-
My idea for this: LoadResData -> ByteArray -> SHCreateMemStream -> IStream -> GdipLoadImageFromStream -> GdipCreateHBITMAPFromBitmap -> OleCreatePictureIndirect
Hi Franky,
Many of these APIs are new for me and truth be told I'm not interested in going down that rabbit hole right now for my limited need. Do you happen to have some simple sample code showing this, that you could share?
-
Apr 15th, 2024, 11:22 AM
#11
Re: Use GDIPlus to display image from a resource file?
All those APIs are encapsulated in the simple example I posted above (post #7), just set a reference to "Microsoft Windows Image Acquisition v2.0" and you're all set.
-
Apr 16th, 2024, 04:04 AM
#12
Re: Use GDIPlus to display image from a resource file?
 Originally Posted by AAraya
Hi Franky,
Many of these APIs are new for me and truth be told I'm not interested in going down that rabbit hole right now for my limited need. Do you happen to have some simple sample code showing this, that you could share?
Check out the code of this GdipLoadPictureArray procedure. It implements the ByteArray -> SHCreateMemStream -> IStream -> GdipLoadImageFromStream -> GdipCreateHBITMAPFromBitmap -> OleCreatePictureIndirect part but can be used like Set oPic = GdipLoadPictureArray(LoadResData(101, "CUSTOM")) for the full workflow.
cheers,
</wqw>
-
Apr 16th, 2024, 09:09 AM
#13
Thread Starter
Fanatic Member
Re: Use GDIPlus to display image from a resource file?
 Originally Posted by wqweto
Check out the code of this GdipLoadPictureArray procedure. It implements the ByteArray -> SHCreateMemStream -> IStream -> GdipLoadImageFromStream -> GdipCreateHBITMAPFromBitmap -> OleCreatePictureIndirect part but can be used like Set oPic = GdipLoadPictureArray(LoadResData(101, "CUSTOM")) for the full workflow.
cheers,
</wqw>
Thanks! That's extremely helpful.
Now the only part I guess I need to solve is the reading of the resource into a byte array. My images are stored in an external DLL which holds both ICO and PNG files with most images being available in multiple sizes for DPI support. I don't believe the LoadResData solution suggested will work for that. Any pointers to help me with that part?
-
Apr 16th, 2024, 04:03 PM
#14
Addicted Member
Re: Use GDIPlus to display image from a resource file?
There is a class module (cGDIPlusCache) by Olaf Schmidt posted in the codebank which has such functionality built-in I believe:
https://www.vbforums.com/showthread....-cls-revisited
-
Apr 17th, 2024, 02:54 AM
#15
Re: Use GDIPlus to display image from a resource file?
 Originally Posted by AAraya
Now the only part I guess I need to solve is the reading of the resource into a byte array. My images are stored in an external DLL which holds both ICO and PNG files with most images being available in multiple sizes for DPI support. I don't believe the LoadResData solution suggested will work for that. Any pointers to help me with that part?
To do this you would have to get an HMODULE on the DLL -> API LoadLibrary(Ex). Then you can search for the resource to see if it exists -> API FindResource(Ex) -> hResInfo and if so, then you can load the resource -> API LoadResource -> HGLOBAL. Then API LockResource and API SizeofResource -> SHCreateMemStream. At the end, release handles that are no longer needed. There are other APIs: LoadBitmap, LoadCursor, LoadIcon, etc.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|