Author Topic: Loading both JPG and PNG as resources.  (Read 1589 times)


  • Administrator
  • Member
  • ******
  • Posts: 6393
  • Mnemonic Driven API Grinder
    • The MASM32 SDK
Loading both JPG and PNG as resources.
« on: October 22, 2016, 09:24:23 PM »
The technique I have used with GDI+ seems to be very flexible as it loads both file formats without any problems. I have attached a test piece that has a PNG toolbar and a JPG image that is shown in the help dialog box. I don't have all the support file finished but the code to do it is posted below.

    mov hImage,    rv(ResImageLoad,40) ; load an image from a resource, the trailing parameter is the resource ID.

// The RC script entries

10 ICON "icon.ico"
20 RCDATA "toolbar.png"
30 BITMAP "gradient.bmp"
40 RCDATA "image.jpg"

  ; ========================================================

  ; the two macros that start and finish GDI+

    GdiPlusBegin MACRO token
        gdii GdiplusStartupInput <>
        GDItoken@@@@ dq ?
      mov gdii.GdiplusVersion, 1
      mov gdii.DebugEventCallback, 0
      mov gdii.SuppressBackgroundThread, 0
      mov gdii.SuppressExternalCodecs, 0
      invoke GdiplusStartup,ADDR GDItoken@@@@,ADDR gdii,0

    GdiPlusEnd MACRO token
      invoke GdiplusShutdown,GDItoken@@@@        ; cleanup on exit

  ; ========================================================

The procedure that does that work.

; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤

ResImageLoad proc ResID:QWORD

    LOCAL hRes  :QWORD
    LOCAL pRes  :QWORD
    LOCAL lRes  :QWORD
    LOCAL pbmp  :QWORD
    LOCAL hBmp  :QWORD
    LOCAL npng  :QWORD

    mov hRes, rv(FindResource,0,ResID,RT_RCDATA)        ; find the resource
    mov pRes, rv(LoadResource,0,hRes)                   ; load the resource
    mov lRes, rv(SizeofResource,0,hRes)                 ; get its size
    mrm npng, "@@@@.@@@"                                ; temp file name

    invoke save_file,npng,pRes,lRes                     ; create the temp file

    invoke GdipCreateBitmapFromFile,L(npng),ADDR pbmp   ; create a GDI+ bitmap
    invoke GdipCreateHBITMAPFromBitmap,pbmp,ADDR hBmp,0 ; create normal bitmap handle from it
    invoke GdipDisposeImage,pbmp                        ; remove the GDI+ bitmap

    invoke DeleteFile,npng                              ; delete the temp file

    mov rax, hBmp                                       ; return the bitmap handle in RAX


ResImageLoad endp

; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤

hutch at movsd dot com    :biggrin:  :skrewy: