Announcement

Collapse
No announcement yet.

Displaying a JPEG (JPG) or PNG Image

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Displaying a JPEG (JPG) or PNG Image

    Looking for information on how to go about displaying a JPG (or a PNG) image? Not sure where to start looking for examples/info on how to do this.
    Thoughts??
    Thanks.. John

  • #2
    Howdy, John!

    Several options. Would you like to load it into a memory bitmap, so that you can copy it anywhere after that? Do you want to load it directly into a Graphic Control? Do you want to resize the image to fit the Graphic Control, maintaining aspect ratio?

    I know I and other have posted such code, handling PNG, BMP, JPG, etc. It's usually all based on Jose's includes. Let me go see if I can help find it for you.



    Comment


    • #3
      You can embed your PNG as a resource:

      Code:
      #RESOURCE RcData, 6008, "..\images\GDI\upload.png"
      And then get the handle to it like this:

      Code:
      hUpload     = GDIpLoadFromResources(6008)
      Using this function:

      Code:
      Declare Function CreateStreamOnHGlobal2 Lib "OLE32.DLL" Alias "CreateStreamOnHGlobal" (ByVal hglobal As Dword, ByVal fdeleteonrelease As Dword, pstm As Dword) As Long
      Declare Function GdipCreateBitmapFromStream2 Lib "GDIPLUS.DLL" Alias "GdipCreateBitmapFromStream" (ByVal pstream As Dword, m_pbitmap As Long) As Long
      
      Function GDIpLoadFromResources(ByVal Resno As Long) As Dword
      
      Local FF As Long
      Local RNDName As String
      Local pImage As Dword
      Local result As Long
      Local CONTENTS As String
      
      Contents = Resource$(RcData, Resno)
      
      Local hBuffer As Dword
      Local pBuffer As Dword
      Local pStream As Dword
      
      Globalmem Alloc Len(contents) To hBuffer
      Globalmem Lock hBuffer To pBuffer
      
      Memory Copy StrPtr(Contents), pBuffer, Len(Contents)
      
      CreateStreamOnHGlobal2(hBuffer, ByVal %Null, pStream)
      GdipCreateBitmapFromStream2(pStream, pImage)
      
      Function = pImage
      
      Globalmem Unlock pBuffer To Result
      Globalmem Free hBuffer To result
      
      End Function​
      After you get the pImage you can draw it in a DC with:

      Code:
      GdipCreateFromHDC(HmemDC, pGraphics)
      gdiAPKDrawImage(pGraphics, hUpload, X, Y, X+AfxScaleX(38), Y+AfxScaleY(38))
      If pGraphics Then GdipDeleteGraphics(pGraphics)


      Dont forget to use GDI to dispose the image and avoid memory leaks.
      www.pluribasic.com
      www.patreon.com/pluribasic

      Comment


      • #4
        Howdy, John!

        What Brian said ...

        Also, here's a relatively recent app I posted that shows how to load, display and resize images while maintaining aspect ratios. There's probably more there than you want, but the procedures should let you choose which actions you want code for.

        https://forum.powerbasic.com/forum/u...-image-gallery

        If you need something simpler let us know.

        Comment


        • #5
          A "fairly simple" demo: https://forum.powerbasic.com/forum/u...029#post799029

          Comment


          • #6
            To show an image file in its associated viewer using PBCC
            '
            Code:
            #DIM All
            #COMPILE EXE
            #Break On                               ' PBCC
            #Console off
            #INCLUDE "Win32api.INC"
            #INCLUDE "ComDlg32.inc"                 ' inc for OpenFileDialog()
             
            SUB GetPicFile(ByVal hParent As Dword, sFile As String)
             LOCAL sFilter  AS STRING
             LOCAL lRes, lFlags, nFile AS LONG
              sFilter     = CHR$("Image files",0,"*.jpg;*.bmp;*.gif;*.png;*.tif",0,"All",0, "*.*",0)
              lFlags      = %OFN_FILEMUSTEXIST
             
              lRes = OpenFileDialog(hParent, _      ' parent window
                    "Open Image File", _            ' caption
                    sFile, _                        ' filename  (full pathname out)
                    Exe.Path$, _                    ' start directory (curDir$ if no path in sFileSpec)
                    sFilter, _                      ' filetype filter
                    ".jpg", _                       ' default extension
                    lFlags )
            END SUB
            '------------------/GetPicFile
             
            SUB ShowImgFile()                       ' using program (locally associated by .ext)
             Local sFile As String
              GetPicFile(%Null, sFile)
                If Len(sFile) Then
                  ShellExecute (%NULL, "Open", Trim$(sFile), $Nul, $Nul, %SW_Show)
                End If
            END SUB
            '------------------/ShowImgFile
             
            FUNCTION PBMain()
              ShowImgFile
              WinBeep 800,200
            END FUNCTION
            '------------------/PBMain

            Last edited by Dave Biggs; 27 Sep 2022, 08:53 PM. Reason: Small tweaks - rename sub to more general ShowImgFile (was 'ShowJpgFile') & remove spurious ref to 'CGI' - should have been GDI ;)
            Rgds, Dave

            Comment


            • #7
              Originally posted by Dave Biggs View Post
              To show an image file in its associated viewer using PBCC
              Didn't notice that this was the PBCC forum. My linked post will require a bit of modification to use a GRAHIC WINDOW instead of a GRAPHIC CONTROL in a Dialog.

              Alternatively, Dave's idea stripped to the essentials (and usable for ANY file type that WIndows knows about (Txt, PDF, Docx etc as well as images)

              '
              Code:
              #COMPILE EXE
              #DIM ALL
              %UNICODE = 1
              #INCLUDE "win32api.inc"
              FUNCTION PBMAIN () AS LONG
              LOCAL filename AS WSTRINGZ * %MAX_PATH
              filename = "MyJPG.jpg"
              ShellExecute 0, "Open", filename, "", "", %SW_NORMAL
              END FUNCTION
              '

              Comment


              • #8
                John,

                This is the simplest code I have for your purpose. It accepts JPGs, PNGs, TIFs and GIFs

                Code:
                'PBWIN10/PBCC6 Program. Manuel Valdes - 2012
                #COMPILE EXE
                #IF %DEF(%PB_CC32)
                 #CONSOLE OFF
                #ENDIF
                #OPTIMIZE SPEED
                '#DEBUG ERROR ON
                #DIM ALL
                
                TYPE GdiplusStartupInput
                 GdiplusVersion           AS DWORD
                 DebugEventCallback       AS DWORD
                 SuppressBackgroundThread AS LONG
                 SuppressExternalCodecs   AS LONG
                END TYPE
                
                TYPE GdiplusStartupOutput
                 NotificationHook         AS DWORD
                 NotificationUnhook       AS DWORD
                END TYPE
                
                DECLARE FUNCTION GdiplusStartup LIB "GDIPLUS.DLL" ALIAS "GdiplusStartup" (token AS DWORD, inputbuf AS GdiplusStartupInput, outputbuf AS GdiplusStartupOutput) AS LONG
                DECLARE SUB GdiplusShutdown LIB "GDIPLUS.DLL" ALIAS "GdiplusShutdown" (BYVAL token AS DWORD)
                DECLARE FUNCTION GdipLoadImageFromFile LIB "GDIPLUS.DLL" ALIAS "GdipLoadImageFromFile" (BYVAL flnameptr AS DWORD, lpImage AS DWORD) AS LONG
                DECLARE FUNCTION GdipGetImageDimension LIB "GDIPLUS.DLL" ALIAS "GdipGetImageDimension" (BYVAL nImage AS LONG, nWidth AS SINGLE, Height AS SINGLE) AS LONG
                DECLARE FUNCTION GdipCreateFromHDC LIB "GDIPLUS.DLL" ALIAS "GdipCreateFromHDC" (BYVAL hdc AS LONG, graphics AS LONG) AS LONG
                DECLARE FUNCTION GdipDisposeImage LIB "GDIPLUS.DLL" ALIAS "GdipDisposeImage" (BYVAL lpImage AS DWORD) AS LONG
                DECLARE FUNCTION GdipDrawImageRectI LIB "GDIPLUS.DLL" ALIAS "GdipDrawImageRectI" (BYVAL hgraphics AS DWORD, BYVAL nimage AS DWORD, BYVAL x AS LONG, BYVAL y AS LONG, _
                                 BYVAL nWidth AS LONG, BYVAL nWeight AS LONG) AS LONG
                
                DECLARE FUNCTION SetWindowPos LIB "USER32.DLL" ALIAS "SetWindowPos" (BYVAL hWnd AS DWORD, BYVAL hWndInsertAfter AS DWORD, BYVAL x AS LONG, BYVAL y AS LONG, _
                                 BYVAL cx AS LONG, BYVAL cy AS LONG, BYVAL wFlags AS DWORD) AS LONG
                
                GLOBAL hWin AS LONG
                GLOBAL W!,H!
                
                FUNCTION PBMAIN() AS LONG
                 LOCAL fnt1,CarH,CarW AS LONG
                 LOCAL X&,Y&,ARC$
                 GRAPHIC WINDOW " ",100,100,20,20 TO hWin
                 GRAPHIC ATTACH hWin,0,REDRAW
                 X&=50
                 Y&=50
                 GRAPHIC SET LOC X&,Y&
                 GRAPHIC SET CAPTION "Enter the graphic File Name"
                 FONT NEW "Courier New",12,0,0,0 TO fnt1
                 GRAPHIC SET FONT fnt1
                 GRAPHIC CHR SIZE TO CarW,CarH
                 GRAPHIC SET CLIENT 60*CarW,3*CarH
                 GRAPHIC WINDOW NORMALIZE
                 ARC$=REMOVE$(COMMAND$,CHR$(34))
                 GRAPHIC SET POS (CarW,CarH)
                 IF ARC$="" THEN GRAPHIC LINE INPUT "GRAPHIC FILE: ",ARC$
                 ARC$=UCASE$(ARC$)
                 IF ARC$="" THEN END
                 CALL XTOBMP(ARC$)
                 GRAPHIC SET CAPTION ARC$
                 GRAPHIC WAITKEY$
                END FUNCTION
                
                SUB XTOBMP(ARCH$)
                 LOCAL UARCH$$,GDIT???,IA???,pImage???,pGraphics???
                 LOCAL hBmp???,hDc???
                 LOCAL Inputbuf AS GdiplusStartupInput
                 UARCH$$=ARCH$
                 IA???=STRPTR(UARCH$$)
                 Inputbuf.GdiplusVersion = 1
                 IF GdiplusStartup(GDIT???,Inputbuf,$NUL) = 0 THEN
                  GdipLoadImagefromFile(IA???,pImage???)
                  GdipGetImageDimension(pImage???,W!,H!)
                  GRAPHIC BITMAP NEW W!,H! TO hBmp???
                  GRAPHIC ATTACH hBmp???,0
                  GRAPHIC GET DC TO hDC???
                  GdipCreateFromHDC(hDC???,pGraphics???)
                  GdipDrawImageRectI(pGraphics???,pImage???,0,0,W!,H!)
                  GRAPHIC ATTACH hWin,0,REDRAW
                  GRAPHIC SET CLIENT W!,H!
                  GRAPHIC COPY hBmp???,0
                  GRAPHIC REDRAW
                  GdipDisposeImage(pImage???)
                  GdiplusShutdown GDIT???
                  GRAPHIC BITMAP END
                 END IF
                END SUB

                Comment


                • #9
                  A modification to Manuel's code to isolate all the image stuff ( no globals required).
                  How you get the image file name is up to your specific requirements
                  '
                  Code:
                  #COMPILE EXE
                  #COMPILER PBCC 6, PBWIN 10
                  
                  #DIM ALL
                  FUNCTION PBMAIN() AS LONG
                      LOCAL wsFIlename AS WSTRING
                      LOCAL lRet AS LONG
                      DIM  errs(1 TO 3) AS STRING
                      PARSE CHR$$("File does not exist",$LF,"Not a valid image file extension",$LF,"Unable to initialise GDI+") _
                            ,Errs(),$LF
                      wsFilename = "MyImage.jpg"
                      lRet = SHowImage (wsFilename)
                      IF lRet <> 0 THEN
                          ? STR$(lRet) & ": " & Errs(lRet)
                      ELSE
                          #IF %DEF(%PB_CC32)
                              ? "...any key"
                              WAITKEY$
                          #ENDIF
                      END IF
                  END FUNCTION
                  
                  '=================All the image stuff=======================
                  TYPE GdiplusStartupInput
                      GdiplusVersion           AS DWORD
                      DebugEventCallback       AS DWORD
                      SuppressBackgroundThread AS LONG
                      SuppressExternalCodecs   AS LONG
                  END TYPE
                  
                  TYPE GdiplusStartupOutput
                      NotificationHook         AS DWORD
                      NotificationUnhook       AS DWORD
                  END TYPE
                  DECLARE FUNCTION GdiplusStartup LIB "GDIPLUS.DLL" ALIAS "GdiplusStartup" (token AS DWORD, inputbuf AS GdiplusStartupInput, outputbuf AS GdiplusStartupOutput) AS LONG
                  DECLARE SUB GdiplusShutdown LIB "GDIPLUS.DLL" ALIAS "GdiplusShutdown" (BYVAL token AS DWORD)'
                  DECLARE FUNCTION GdipLoadImageFromFile LIB "GDIPLUS.DLL" ALIAS "GdipLoadImageFromFile" (BYVAL flnameptr AS DWORD, lpImage AS DWORD) AS LONG
                  DECLARE FUNCTION GdipGetImageDimension LIB "GDIPLUS.DLL" ALIAS "GdipGetImageDimension" (BYVAL nImage AS LONG, nWidth AS SINGLE, Height AS SINGLE) AS LONG
                  DECLARE FUNCTION GdipCreateFromHDC LIB "GDIPLUS.DLL" ALIAS "GdipCreateFromHDC" (BYVAL hdc AS LONG, graphics AS LONG) AS LONG
                  DECLARE FUNCTION GdipDisposeImage LIB "GDIPLUS.DLL" ALIAS "GdipDisposeImage" (BYVAL lpImage AS DWORD) AS LONG
                  DECLARE FUNCTION GdipDrawImageRectI LIB "GDIPLUS.DLL" ALIAS "GdipDrawImageRectI" (BYVAL hgraphics AS DWORD, BYVAL nimage AS DWORD, BYVAL x AS LONG, BYVAL y AS LONG, _
                                   BYVAL nWidth AS LONG, BYVAL nWeight AS LONG) AS LONG
                  
                  FUNCTION SHowImage(wsFIlename AS WSTRING) AS LONG
                      LOCAL wsValidExts AS WSTRING
                      LOCAL dwGDIToken,pFilename, pImage,pBmp AS DWORD
                      LOCAL hWin, hBmp,hDc AS DWORD
                      LOCAL Inputbuf AS GdiplusStartupInput
                      LOCAL sngW,sngH AS SINGLE
                  
                      wsValidExts= ".BMP.JPG.TIFF.PNG.GIF"
                      IF NOT ISFILE(wsFIlename) THEN FUNCTION = 1:EXIT FUNCTION
                      IF INSTR(wsValidExts, UCASE$(PATHNAME$(EXTN,wsFilename))) = 0 THEN FUNCTION = 2:EXIT FUNCTION
                      pFIleName=STRPTR(wsFileName)
                    
                      Inputbuf.GdiplusVersion = 1
                      IF GdiplusStartup(dwGDIToken,Inputbuf,$NUL) <>  0 THEN FUNCTION = 3:EXIT FUNCTION
                      GdipLoadImagefromFile(pFIleName,pImage)
                      GdipGetImageDimension(pImage,sngW,sngH)
                  
                      GRAPHIC BITMAP NEW sngW,sngH TO hBmp
                      GRAPHIC ATTACH hBmp,0
                      GRAPHIC GET DC TO hDC
                      GdipCreateFromHDC(hDC,pBmp)
                      GdipDrawImageRectI(pBmp,pImage,0,0,sngW,sngH)
                      GdipDisposeImage(pImage)
                      GdiplusShutdown dwGDIToken
                  
                      GRAPHIC WINDOW " ",10,10,sngW,sngH TO hWin
                      GRAPHIC ATTACH hWin,0
                      GRAPHIC COPY hBmp,0
                      GRAPHIC ATTACH hBmp,0
                      GRAPHIC BITMAP END
                      GRAPHIC ATTACH hWin,0
                      GRAPHIC WAITKEY$
                  END FUNCTION
                  '

                  Comment


                  • #10
                    Further improvement avoiding the creation of an intermediate BMP, going instead straight to the Graphic Window. No GLOBALS

                    Code:
                    'PBWIN10/PBCC6 Program. Manuel Valdes - 2012
                    #COMPILE EXE
                    #IF %DEF(%PB_CC32)
                     #CONSOLE OFF
                    #ENDIF
                    #OPTIMIZE SPEED
                    #DIM ALL
                    
                    TYPE GdiplusStartupInput
                     GdiplusVersion           AS DWORD
                     DebugEventCallback       AS DWORD
                     SuppressBackgroundThread AS LONG
                     SuppressExternalCodecs   AS LONG
                    END TYPE
                    
                    TYPE GdiplusStartupOutput
                     NotificationHook         AS DWORD
                     NotificationUnhook       AS DWORD
                    END TYPE
                    
                    DECLARE FUNCTION GdiplusStartup LIB "GDIPLUS.DLL" ALIAS "GdiplusStartup" (token AS DWORD, inputbuf AS GdiplusStartupInput, outputbuf AS GdiplusStartupOutput) AS LONG
                    DECLARE SUB GdiplusShutdown LIB "GDIPLUS.DLL" ALIAS "GdiplusShutdown" (BYVAL token AS DWORD)
                    DECLARE FUNCTION GdipLoadImageFromFile LIB "GDIPLUS.DLL" ALIAS "GdipLoadImageFromFile" (BYVAL flnameptr AS DWORD, lpImage AS DWORD) AS LONG
                    DECLARE FUNCTION GdipGetImageDimension LIB "GDIPLUS.DLL" ALIAS "GdipGetImageDimension" (BYVAL nImage AS LONG, nWidth AS SINGLE, Height AS SINGLE) AS LONG
                    DECLARE FUNCTION GdipCreateFromHDC LIB "GDIPLUS.DLL" ALIAS "GdipCreateFromHDC" (BYVAL hdc AS LONG, graphics AS LONG) AS LONG
                    DECLARE FUNCTION GdipDisposeImage LIB "GDIPLUS.DLL" ALIAS "GdipDisposeImage" (BYVAL lpImage AS DWORD) AS LONG
                    DECLARE FUNCTION GdipDrawImageRectI LIB "GDIPLUS.DLL" ALIAS "GdipDrawImageRectI" (BYVAL hgraphics AS DWORD, BYVAL nimage AS DWORD, BYVAL x AS LONG, BYVAL y AS LONG, _
                                     BYVAL nWidth AS LONG, BYVAL nWeight AS LONG) AS LONG
                    
                    DECLARE FUNCTION SetWindowPos LIB "USER32.DLL" ALIAS "SetWindowPos" (BYVAL hWnd AS DWORD, BYVAL hWndInsertAfter AS DWORD, BYVAL x AS LONG, BYVAL y AS LONG, _
                                     BYVAL cx AS LONG, BYVAL cy AS LONG, BYVAL wFlags AS DWORD) AS LONG
                    
                    
                    FUNCTION PBMAIN() AS LONG
                     LOCAL hWin,fnt1,CarH,CarW AS LONG
                     LOCAL X&,Y&,ARCHI$,W!,H!
                     GRAPHIC WINDOW " ",100,100,20,20 TO hWin
                     GRAPHIC ATTACH hWin,0,REDRAW
                     X&=50
                     Y&=50
                     GRAPHIC SET LOC X&,Y&
                     GRAPHIC SET CAPTION "Enter the graphic File Name"
                     FONT NEW "Courier New",12,0,0,0 TO fnt1
                     GRAPHIC SET FONT fnt1
                     GRAPHIC CHR SIZE TO CarW,CarH
                     GRAPHIC SET CLIENT 60*CarW,3*CarH
                     ARCHI$=REMOVE$(COMMAND$,CHR$(34))
                     GRAPHIC SET POS (CarW,CarH)
                     IF ARCHI$="" THEN GRAPHIC LINE INPUT "GRAPHIC FILE: ",ARCHI$
                     ARCHI$=UCASE$(ARCHI$)
                     IF ARCHI$="" THEN END
                     CALL XTOBMP(ARCHI$,hWin,W!,H!)
                     GRAPHIC SET CAPTION ARCHI$
                     GRAPHIC WAITKEY$
                     GRAPHIC WINDOW END
                    END FUNCTION
                    
                    SUB XTOBMP(ARCH$,hWin AS LONG,W!,H!)
                     LOCAL UARCH$$,GDIT???,IA???,pImage???,pGraphics???,hDc???
                     LOCAL Inputbuf AS GdiplusStartupInput
                     UARCH$$=ARCH$
                     IA???=STRPTR(UARCH$$)
                     Inputbuf.GdiplusVersion = 1
                     IF GdiplusStartup(GDIT???,Inputbuf,$NUL) = 0 THEN
                      GdipLoadImagefromFile(IA???,pImage???)
                      GdipGetImageDimension(pImage???,W!,H!)
                      GRAPHIC SET CLIENT W!,H!
                      GRAPHIC GET DC TO hDC???
                      GdipCreateFromHDC(hDC???,pGraphics???)
                      GdipDrawImageRectI(pGraphics???,pImage???,0,0,W!,H!)
                      GRAPHIC REDRAW
                      GdipDisposeImage(pImage???)
                      GdiplusShutdown GDIT???
                     END IF
                    END SUB
                                  ​

                    Comment


                    • #11
                      Originally posted by Manuel Valdes View Post
                      Further improvement avoiding the creation of an intermediate BMP, going instead straight to the Graphic Window. No GLOBALS
                      Nice! I've updated my stand alone function above to do the same
                      '
                      Code:
                      FUNCTION ShowImage(wsFIlename AS WSTRING) AS LONG
                          LOCAL wsValidExts AS WSTRING
                          LOCAL dwGDIToken, pFilename, pImage, hGWnd, pGWnd, hBmp, hDc AS DWORD
                          LOCAL Inputbuf AS GdiplusStartupInput
                          LOCAL sngW,sngH AS SINGLE
                      
                          wsValidExts= ".BMP.JPG.TIFF.PNG.GIF"
                          IF NOT ISFILE(wsFIlename) THEN FUNCTION = 1:EXIT FUNCTION
                          IF INSTR(wsValidExts, UCASE$(PATHNAME$(EXTN,wsFilename))) = 0 THEN FUNCTION = 2:EXIT FUNCTION
                          pFIleName=STRPTR(wsFileName)
                      
                          Inputbuf.GdiplusVersion = 1
                          IF GdiplusStartup(dwGDIToken,Inputbuf,$NUL) <>  0 THEN FUNCTION = 3:EXIT FUNCTION
                          GdipLoadImagefromFile(pFIleName,pImage)
                          GdipGetImageDimension(pImage,sngW,sngH)
                          GRAPHIC WINDOW " ",10,10,sngW,sngH TO hGWnd
                          GRAPHIC GET DC TO hDC
                          GdipCreateFromHDC(hDC,pGWnd)
                          GdipDrawImageRectI(pGWnd,pImage,0,0,sngW,sngH)
                          GRAPHIC REDRAW
                          GRAPHIC WAITKEY$
                      END FUNCTION
                      '


                      Comment


                      • #12
                        I decided to add the ability to Display an image stored in the application as a resource as well as load one from a file.

                        '
                        Code:
                        #COMPILE EXE "ShowImageDemo.exe"
                        #COMPILER PBCC 6, PBWIN 10
                        #DIM ALL
                        #RESOURCE RCDATA, IMGRes1 "MyImage.jpg"
                        
                        FUNCTION PBMAIN() AS LONG
                            LOCAL wsFIlename AS WSTRING
                            LOCAL lRet AS LONG
                            DIM  errs(1 TO 3) AS STRING
                            PARSE CHR$$("File does not exist",$LF,"Not a valid image file extension",$LF,"Unable to initialise GDI+") _
                                  ,Errs(),$LF
                            wsFilename = "MyImage2.jpg"
                            lRet = SHowFileImage (wsFilename)
                            IF lRet <> 0 THEN
                                ? STR$(lRet) & ": " & Errs(lRet)
                            ELSE
                                #IF %DEF(%PB_CC32)
                                    ? "...any key"
                                    WAITKEY$
                                #ENDIF
                            END IF
                            lRet = ShowResourceImage("IMGRes1")
                            IF lRet <> 0 THEN
                                ? STR$(lRet) & ": " & Errs(lRet)
                            ELSE
                                #IF %DEF(%PB_CC32)
                                    ? "...any key"
                                    WAITKEY$
                                #ENDIF
                            END IF
                            wsFilename = "MyImage3.jpg"
                            lRet = SHowFileImage (wsFilename)
                            IF lRet <> 0 THEN
                                ? STR$(lRet) & ": " & Errs(lRet)
                            ELSE
                                #IF %DEF(%PB_CC32)
                                    ? "...any key"
                                    WAITKEY$
                                #ENDIF
                            END IF
                        END FUNCTION
                        
                        '=================All the image stuff=======================
                        TYPE GdiplusStartupInput
                            GdiplusVersion           AS DWORD
                            DebugEventCallback       AS DWORD
                            SuppressBackgroundThread AS LONG
                            SuppressExternalCodecs   AS LONG
                        END TYPE
                        
                        TYPE GdiplusStartupOutput
                            NotificationHook         AS DWORD
                            NotificationUnhook       AS DWORD
                        END TYPE
                        DECLARE FUNCTION GdiplusStartup LIB "GDIPLUS.DLL" ALIAS "GdiplusStartup" (token AS DWORD, inputbuf AS GdiplusStartupInput, outputbuf AS GdiplusStartupOutput) AS LONG
                        DECLARE SUB GdiplusShutdown LIB "GDIPLUS.DLL" ALIAS "GdiplusShutdown" (BYVAL token AS DWORD)'
                        DECLARE FUNCTION GdipLoadImageFromFile LIB "GDIPLUS.DLL" ALIAS "GdipLoadImageFromFile" (BYVAL flnameptr AS DWORD, lpImage AS DWORD) AS LONG
                        DECLARE FUNCTION GdipGetImageDimension LIB "GDIPLUS.DLL" ALIAS "GdipGetImageDimension" (BYVAL nImage AS LONG, nWidth AS SINGLE, Height AS SINGLE) AS LONG
                        DECLARE FUNCTION GdipCreateFromHDC LIB "GDIPLUS.DLL" ALIAS "GdipCreateFromHDC" (BYVAL hdc AS LONG, graphics AS LONG) AS LONG
                        DECLARE FUNCTION GdipDisposeImage LIB "GDIPLUS.DLL" ALIAS "GdipDisposeImage" (BYVAL lpImage AS DWORD) AS LONG
                        DECLARE FUNCTION GdipDrawImageRectI LIB "GDIPLUS.DLL" ALIAS "GdipDrawImageRectI" (BYVAL hgraphics AS DWORD, BYVAL nimage AS DWORD, BYVAL x AS LONG, BYVAL y AS LONG, _
                                         BYVAL nWidth AS LONG, BYVAL nWeight AS LONG) AS LONG
                        
                        'Only needed for SHowResourceImage:
                        DECLARE FUNCTION GdipCreateBitmapFromStream LIB "GDIPLUS.DLL" ALIAS "GdipCreateBitmapFromStream" (BYVAL pstream AS DWORD, m_pbitmap AS LONG) AS LONG
                        DECLARE FUNCTION CreateStreamOnHGlobal LIB "OLE32.DLL" ALIAS "CreateStreamOnHGlobal" (BYVAL hglobal AS DWORD, BYVAL fdeleteonrelease AS DWORD, pstm AS DWORD) AS LONG
                        
                        FUNCTION ShowFileImage(wsFIlename AS WSTRING) AS LONG
                            LOCAL wsValidExts AS WSTRING
                            LOCAL dwGDIToken, pFilename, pImage, hGWnd, pGWnd, hBmp, hDc AS DWORD
                            LOCAL Inputbuf AS GdiplusStartupInput
                            LOCAL sngW,sngH AS SINGLE
                        
                            wsValidExts= ".BMP.JPG.TIFF.PNG.GIF"
                            IF NOT ISFILE(wsFIlename) THEN FUNCTION = 1:EXIT FUNCTION
                            IF INSTR(wsValidExts, UCASE$(PATHNAME$(EXTN,wsFilename))) = 0 THEN FUNCTION = 2:EXIT FUNCTION
                            pFIleName=STRPTR(wsFileName)
                        
                            Inputbuf.GdiplusVersion = 1
                            IF GdiplusStartup(dwGDIToken,Inputbuf,$NUL) <>  0 THEN FUNCTION = 3:EXIT FUNCTION
                            GdipLoadImagefromFile(pFIleName,pImage)
                            GdipGetImageDimension(pImage,sngW,sngH)
                            GRAPHIC WINDOW "First Image ",10,10,sngW,sngH TO hGWnd
                            GRAPHIC ATTACH hGWnd,0
                            GRAPHIC GET DC TO hDC
                            GdipCreateFromHDC(hDC,pGWnd)
                            GdipDrawImageRectI(pGWnd,pImage,0,0,sngW,sngH)
                            GdipDisposeImage(pImage)
                            GdiplusShutdown dwGDIToken
                            GRAPHIC REDRAW
                            GRAPHIC WAITKEY$
                        END FUNCTION
                        
                        FUNCTION ShowResourceImage(ResID AS STRING) AS LONG
                            LOCAL sImage AS STRING
                            LOCAL lImageSize AS LONG
                            LOCAL dwGDIToken, pIMage,pMemBuffer,pBuffer,hGWnd,pGWnd,hDC AS DWORD
                            LOCAL Inputbuf AS GdiplusStartupInput
                            LOCAL sngW,sngH AS SINGLE
                            sImage = RESOURCE$(RCDATA, ResId)
                            lImageSize = LEN(sIMage)
                            pImage = STRPTR(sIMage)
                            Inputbuf.GdiplusVersion = 1
                            IF GdiplusStartup(dwGDIToken,Inputbuf,$NUL) <>  0 THEN FUNCTION = 3:EXIT FUNCTION
                            GLOBALMEM ALLOC lImageSize TO pMemBuffer
                            GLOBALMEM LOCK pMemBuffer TO pBuffer
                            MEMORY COPY pImage, pBuffer, lImageSize
                            CreateStreamOnHGlobal(pMemBuffer, 0, pBuffer)
                            GdipCreateBitmapFromStream(pBuffer, pImage)
                            GdipGetImageDimension(pImage,sngW,sngH)
                            GRAPHIC WINDOW " 2nd Image",100,100,sngW,sngH TO hGWnd
                            GRAPHIC ATTACH hGWnd,0
                            GRAPHIC GET DC TO hDC
                            GdipCreateFromHDC(hDC,pGWnd)
                            GdipDrawImageRectI(pGWnd,pImage,0,0,sngW,sngH)
                            GdipDisposeImage(pImage)
                            GLOBALMEM FREE pMemBuffer TO pBuffer
                            GdiplusShutdown dwGDIToken
                            GRAPHIC REDRAW
                            GRAPHIC WAITKEY$
                        END FUNCTION
                        '

                        Comment


                        • #13
                          That is old story too (2013),

                          https://forum.powerbasic.com/forum/j...ge-as-resource

                          Anyway it can be improved avoiding the intermediate bitmap.


                          Code:
                          #COMPILE EXE
                          #DIM ALL
                          #IF %DEF(%PB_CC32)
                           #CONSOLE OFF
                          #ENDIF
                          
                          #RESOURCE RCDATA Torres_del_Paine, "TP.JPG"
                          
                          $resourceName   = "Torres_del_Paine"
                          
                          %RT_RCDATA           = 10
                          %GMEM_MOVEABLE       = &H0002
                          %FALSE               = 0
                          
                          TYPE GdiplusStartupInput
                           GdiplusVersion           AS DWORD
                           DebugEventCallback       AS DWORD
                           SuppressBackgroundThread AS LONG
                           SuppressExternalCodecs   AS LONG
                          END TYPE
                          
                          TYPE GdiplusStartupOutput
                           NotificationHook         AS DWORD
                           NotificationUnhook       AS DWORD
                          END TYPE
                          
                          'API Declares
                          DECLARE FUNCTION FindResourceA LIB "Kernel32.dll" ALIAS "FindResourceA" (BYVAL hInstance AS DWORD, lpName AS ASCIIZ, lpType AS ASCIIZ) AS LONG
                          DECLARE FUNCTION SizeofResource LIB "Kernel32.dll" ALIAS "SizeofResource" (BYVAL hInstance AS DWORD, BYVAL hResInfo AS DWORD) AS DWORD
                          DECLARE FUNCTION FreeResource LIB "Kernel32.dll" ALIAS "FreeResource" (BYVAL hResData AS DWORD) AS LONG
                          DECLARE FUNCTION LockResource LIB "Kernel32.dll" ALIAS "LockResource" (BYVAL hResData AS DWORD) AS DWORD
                          DECLARE FUNCTION LoadResource LIB "Kernel32.dll" ALIAS "LoadResource" (BYVAL hInstance AS DWORD, BYVAL hResInfo AS DWORD) AS DWORD
                          DECLARE FUNCTION CreateStreamOnHGlobal LIB "OLE32.DLL" ALIAS "CreateStreamOnHGlobal" (BYVAL hglobal AS DWORD, BYVAL fdeleteonrelease AS DWORD, pstm AS DWORD) AS LONG
                          
                          DECLARE FUNCTION pStreamRelease(punk AS DWORD) AS LONG
                          
                          'GDI+ Declares
                          DECLARE FUNCTION GdiplusStartup LIB "GDIPLUS.DLL" ALIAS "GdiplusStartup" (token AS DWORD, inputbuf AS GdiplusStartupInput, outputbuf AS GdiplusStartupOutput) AS LONG
                          DECLARE SUB GdiplusShutdown LIB "GDIPLUS.DLL" ALIAS "GdiplusShutdown" (BYVAL token AS DWORD)
                          DECLARE FUNCTION GdipGetImageDimension LIB "GDIPLUS.DLL" ALIAS "GdipGetImageDimension" (BYVAL nImage AS LONG, nWidth AS SINGLE, Height AS SINGLE) AS LONG
                          DECLARE FUNCTION GdipCreateFromHDC LIB "GDIPLUS.DLL" ALIAS "GdipCreateFromHDC" (BYVAL hdc AS LONG, graphics AS LONG) AS LONG
                          DECLARE FUNCTION GdipDisposeImage LIB "GDIPLUS.DLL" ALIAS "GdipDisposeImage" (BYVAL lpImage AS DWORD) AS LONG
                          DECLARE FUNCTION GdipDrawImageRectI LIB "GDIPLUS.DLL" ALIAS "GdipDrawImageRectI" (BYVAL hgraphics AS DWORD, BYVAL nimage AS DWORD, BYVAL x AS LONG, BYVAL y AS LONG, _
                                           BYVAL nWidth AS LONG, BYVAL nWeight AS LONG) AS LONG
                          DECLARE FUNCTION GdipDeleteGraphics LIB "GDIPLUS.DLL" ALIAS "GdipDeleteGraphics" (BYVAL Graphics AS LONG) AS LONG
                          DECLARE FUNCTION GdipCreateBitmapFromStream LIB "GDIPLUS.DLL" ALIAS "GdipCreateBitmapFromStream" (BYVAL pstream AS DWORD, m_pbitmap AS LONG) AS LONG
                          
                          FUNCTION PBMAIN
                           LOCAL GDIT???,IA???,pImage???,pGraphics???,HDC???
                           LOCAL Inputbuf AS GdiplusStartupInput
                           LOCAL W!, H!,hWm???,hWin???
                           LOCAL GF$,DCX&,DCY&,I&
                           LOCAL hResource???, ImageSize???, pResourceData???, m_hBuffer???, pBuffer???
                           LOCAL pStream AS DWORD PTR
                           GF$=$ResourceName
                           Inputbuf.GdiplusVersion = 1
                           IF GdiplusStartup(GDIT???, Inputbuf, $NUL) = 0 THEN
                            GRAPHIC WINDOW NEW "XX",100,100,200,200 TO hWm???
                            GRAPHIC ATTACH hWm???, 0
                            hResource??? = FindResourceA(0, $ResourceName, BYVAL %rt_rcdata)
                            imageSize??? = SizeOfResource(0, hResource)
                            pResourceData??? = LockResource(LoadResource(0, hResource???))
                            GLOBALMEM ALLOC imageSize??? TO m_hBuffer???
                            GLOBALMEM LOCK m_hBuffer??? TO pBuffer???
                            MEMORY COPY pResourceData???, pBuffer???, imageSize???
                            CreateStreamOnHGlobal(m_hBuffer???, %false, pStream)
                            GdipCreateBitmapFromStream(pStream, pImage???)
                            GdipGetImageDimension(pImage???,W!,H!)
                            GRAPHIC SET CLIENT W!,H!
                            GLOBALMEM FREE @@pStream[2] TO pStream
                            GRAPHIC GET DC TO HDC???
                            GdipCreateFromHDC(HDC???, pGraphics???)
                            GdipDrawImageRectI(pGraphics???, pImage???, 0, 0, W!, H!)
                            GRAPHIC REDRAW
                            GdipDisposeImage(pImage???)
                            GdiplusShutdown GDIT???
                            GRAPHIC WAITKEY$
                            GRAPHIC WINDOW END
                           END IF
                          END FUNCTION              ​

                          Comment


                          • #14
                            Originally posted by Manuel Valdes View Post
                            That is old story too (2013),

                            https://forum.powerbasic.com/forum/j...ge-as-resource
                            Anyway it can be improved avoiding the intermediate bitmap.
                            Pretty much the same as my post with the exception of the use of the new PB intrinsic RESOURCE$:
                            Code:
                            ...
                            'API Declares
                            DECLARE FUNCTION FindResourceA LIB "Kernel32.dll" ALIAS "FindResourceA" (BYVAL hInstance AS DWORD, lpName AS ASCIIZ, lpType AS ASCIIZ) AS LONG
                            DECLARE FUNCTION SizeofResource LIB "Kernel32.dll" ALIAS "SizeofResource" (BYVAL hInstance AS DWORD, BYVAL hResInfo AS DWORD) AS DWORD
                            DECLARE FUNCTION FreeResource LIB "Kernel32.dll" ALIAS "FreeResource" (BYVAL hResData AS DWORD) AS LONG
                            DECLARE FUNCTION LockResource LIB "Kernel32.dll" ALIAS "LockResource" (BYVAL hResData AS DWORD) AS DWORD
                            DECLARE FUNCTION LoadResource LIB "Kernel32.dll" ALIAS "LoadResource" (BYVAL hInstance AS DWORD, BYVAL hResInfo AS DWORD) AS DWORD
                            ...
                            GF$=$ResourceName
                            ...
                            hResource??? = FindResourceA(0, $ResourceName, BYVAL %rt_rcdata)
                            imageSize??? = SizeOfResource(0, hResource)
                            pResourceData??? = LockResource(LoadResource(0, hResource???))
                            becomes:​
                            Code:
                            sImage = RESOURCE$(RCDATA, ResId)
                            lImageSize = LEN(sIMage)
                            pImage = STRPTR(sIMage)​

                            Comment

                            Working...
                            X