Results 1 to 15 of 15

Thread: Use GDIPlus to display image from a resource file?

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Aug 2011
    Location
    Palm Coast, FL
    Posts
    518

    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.

  2. #2
    PowerPoster
    Join Date
    Jul 2010
    Location
    NYC
    Posts
    5,930

    Re: Use GDIPlus to display image from a resource file?

    GdipCreateBitmapFromResource

    Or you can load them and get an HICON or HBITMAP... GdipCreateBitmapFromHICON, GdipCreateBitmapFromHBITMAP.

  3. #3
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    10,199

    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.

    1. Loading an image from resources and getting it into a StdPicture object.
    2. 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.

  4. #4

    Thread Starter
    Fanatic Member
    Join Date
    Aug 2011
    Location
    Palm Coast, FL
    Posts
    518

    Re: Use GDIPlus to display image from a resource file?

    Quote Originally Posted by Elroy View Post
    Yeah, I've never used the GdipCreateBitmapFromResource. But, for me, this is two separate issues.

    1. Loading an image from resources and getting it into a StdPicture object.
    2. 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.

  5. #5
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    10,199

    Re: Use GDIPlus to display image from a resource file?

    Quote Originally Posted by AAraya View Post
    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.

  6. #6

    Thread Starter
    Fanatic Member
    Join Date
    Aug 2011
    Location
    Palm Coast, FL
    Posts
    518

    Re: Use GDIPlus to display image from a resource file?

    Quote Originally Posted by Elroy View Post
    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.

  7. #7
    Frenzied Member VanGoghGaming's Avatar
    Join Date
    Jan 2020
    Location
    Eve Online - Mining, Missions & Market Trading!
    Posts
    1,542

    Talking 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.

  8. #8
    Addicted Member -Franky-'s Avatar
    Join Date
    Dec 2022
    Location
    Bremen Germany
    Posts
    254

    Re: Use GDIPlus to display image from a resource file?

    My idea for this: LoadResData -> ByteArray -> SHCreateMemStream -> IStream -> GdipLoadImageFromStream -> GdipCreateHBITMAPFromBitmap -> OleCreatePictureIndirect

  9. #9
    The Idiot
    Join Date
    Dec 2014
    Posts
    2,747

    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.

  10. #10

    Thread Starter
    Fanatic Member
    Join Date
    Aug 2011
    Location
    Palm Coast, FL
    Posts
    518

    Re: Use GDIPlus to display image from a resource file?

    Quote Originally Posted by -Franky- View Post
    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?

  11. #11
    Frenzied Member VanGoghGaming's Avatar
    Join Date
    Jan 2020
    Location
    Eve Online - Mining, Missions & Market Trading!
    Posts
    1,542

    Talking 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.

  12. #12
    PowerPoster wqweto's Avatar
    Join Date
    May 2011
    Location
    Sofia, Bulgaria
    Posts
    5,232

    Re: Use GDIPlus to display image from a resource file?

    Quote Originally Posted by AAraya View Post
    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>

  13. #13

    Thread Starter
    Fanatic Member
    Join Date
    Aug 2011
    Location
    Palm Coast, FL
    Posts
    518

    Re: Use GDIPlus to display image from a resource file?

    Quote Originally Posted by wqweto View Post
    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?

  14. #14
    Addicted Member
    Join Date
    Feb 2015
    Posts
    176

    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

  15. #15
    Addicted Member -Franky-'s Avatar
    Join Date
    Dec 2022
    Location
    Bremen Germany
    Posts
    254

    Re: Use GDIPlus to display image from a resource file?

    Quote Originally Posted by AAraya View Post
    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
  •  



Click Here to Expand Forum to Full Width