Load PNG/JPEG from resource using GDI+ (MASM)

Following my previous post on how to load JPG/PNG file from resource using GDI+:
http://devpinoy.org/blogs/cvega/archive/2008/07/28/loading-png-and-jpg-image-from-resource-using-gdi.aspx

I’ve written an equivalent function for MASM32 users:
http://www.masm32.com/board/index.php?topic=10191.0

Just follow these simple steps and you’re set to use resource image in your program in no time.

    gdiplusLoadBitmapFromResource,
              hInstance,
              resourceName,
              resourceType,
              outImage

    where:
       hInstance      - the module handle where to load the resource
       resourceName   - the name or the ID of the resource
       resourceType   - string pointer that will indicate the type
                        of the resource., e.g., "PNG",0
       outImage       - address of variable where to put the handle
                        of loaded image


Here's how to use it:

1. Add the function gdiplusLoadBitmapFromResource to your program, or include gdiplusLoadBitmapFromResource.inc.

2. Add the proto (so you can use INVOKE to call gdiplusLoadBitmapFromResource):

gdiplusLoadBitmapFromResource proto :HMODULE, :LPSTR, :LPSTR, :DWORD


3. Initialize GDI+ on your program's start

GdiplusStartupInput struc
    GdiplusVersion dd ?
    DebugEventCallback dd ?
    SuppressBackgroundThread BOOL ?
    SuppressExternalCodecs BOOL ?
GdiplusStartupInput ends

.data
      gdiplusToken dd ?
      gdiplusSInput GdiplusStartupInput <1, NULL, FALSE, FALSE>

.code
      invoke GetModuleHandle, 0
      mov hInstance, eax
     
      ; Initialize GDI+
      invoke GdiplusStartup, addr gdiplusToken, addr gdiplusSInput, NULL


4. In your resource script, add your image with specified type, something like:

9000 PNG "myImage.png"


where
9000 is the ID of the resource, and PNG is the type

5. Add a null-terminated string entry to your data section for the type of resource, e.g., "PNG"

.data
    pngImage dd NULL
    pngType db "PNG", 0


6. Load the image using gdiplusLoadBitmapFromResource:

invoke gdiplusLoadBitmapFromResource, hInstance, 9000, addr pngType, addr pngImage


7. When you're done using the image, dispose it using GdipDisposeImage function:

invoke GdipDisposeImage, addr pngImage


8. Finally, shutdown GDI+ before you exit your program:

invoke GdiplusShutdown, gdiplusToken


Hope you find this little code useful.



Cheers,

-chris



 

 

Attachment: gdiplres.zip
Published 10-26-2008 7:45 AM by cvega
Filed under: