Load JPG/GIF Images to Memory Bitmap

Category: Graphics - GDI+

Date: 02-16-2022

Return to Index


 
'Compilable Example:  (Jose Includes)
#Compiler PBWin 10
#Compile Exe
#Dim All
%Unicode = 1
#Include "Win32API.inc"
#Include "CGDIPLUS.inc"
%IDC_GraphicA = 500
%IDC_GraphicB = 501
Global hDlg,hWinA,hWinB As Dword
 
Function PBMain() As Long
   Dialog New Pixels, 0, "PowerBASIC",300,300,320,320, %WS_OverlappedWindow To hDlg
   Control Add Graphic, hDlg, %IDC_GraphicA,"", 10,10,150,150
   Control Add Graphic, hDlg, %IDC_GraphicB,"", 170,10,150,150
   Dialog Show Modal hDlg Call DlgProc
End Function
 
CallBack Function DlgProc() As Long
   Local w,h As Long
   Select Case Cb.Msg
      Case %WM_InitDialog
         LoadImageToMemoryBitmap "baby.jpg", hWinA, w,h   'w,h are return values
         Graphic Attach hDlg, %IDC_GraphicA
         Graphic Copy hWinA,0
 
         LoadImageToMemoryBitmap "cowgirl_good.bmp", hWinB, w,h   'w,h are return values
         Graphic Attach hDlg, %IDC_GraphicB
         Graphic Copy hWinB,0
   End Select
End Function
 
Sub LoadImageToMemoryBitmap (ImgName As String, hWin As Dword, w As Long, h As Long)
   Local pGraphics,pImage,hDC,token As Dword, StartupInput As GdiplusStartupInput
   Local w,h As Long
   'initialize GDI
   StartupInput.GdiplusVersion = 1                    'initialize GDIPlus
   GdiplusStartup(token, StartupInput, ByVal %NULL)   'initialize GDIPlus
 
   GdipLoadImageFromFile((ImgName), pImage)           'load image
   GdipGetImageWidth(pImage,w)                        'get width
   GdipGetImageHeight(pImage, h)                      'get height
 
   Graphic Bitmap New w,h To hWin
   Graphic Attach hWin, 0
   Graphic Get DC To hDC
 
   GdipCreateFromHDC(hDC, pGraphics)                  ' Create the Graphic object
   GdipDrawImage(pGraphics, pImage, 0, 0)             ' Draw the image
 
   GdipShutDown
   If pImage Then GdipDisposeImage(pImage)            'cleanup
   If pGraphics Then GdipDeleteGraphics(pGraphics)    'cleanup
   GdiplusShutdown token                              'shutdown GDI+
End Sub 
 
'gbs_01317
'Date: 05-11-2013


created by gbSnippets
http://www.garybeene.com/sw/gbsnippets.htm