Date: 02-16-2022
Return to Index
created by gbSnippets
'Since JPG/GIF images are not supported in PowerBASIC, programmers need
'a way to get them into an application. Once there, PowerBASIC has the
'tools to work with the image. GDI+, a part of the Windows distribution,
'can be used to load the images.
'GDI+ is considered by many to be somewhat complicated. Fortunately, the
'code to simply load JPG/GIF images is somewhat short. This snippet does
'require the gdiplus.inc include file. Two versions are available, one from
'Jose Roca and another from Patrice Terrier. Neither is included in the
'gbSnippets distribution.
'In this version of the JPG/GIF GDI+ loader (see snippet http://gbl_00176
'for another version) handling of the file loading is treated as a complete
'Function, with no GDI+ code required in the main program.
'Primary Code:
'Credit: Jose Roca
'Compilable Example: (Jose Includes)
'In this example, Graphic Controls are used as the graphic targets.
#Compiler PBWin 9, PBWin 10
#Compile EXE
#Dim All
%Unicode=1
#Include "Win32API.inc"
#Include "GDIPLUS_Simple.INC"
Global hDlg as DWord
%IDC_GRAPHIC1 = 101
%IDC_GRAPHIC2 = 102
%IDC_GRAPHIC3 = 103
Function PBMain() As Long
Dialog New Pixels, 0, "GDI+ Load Image Test",300,300,500,200, %WS_OverlappedWindow To hDlg
Control Add Button, hDlg, 110,"GIF", 10,10,60,20
Control Add Button, hDlg, 120,"BMP", 100,10,60,20
Control Add Button, hDlg, 130,"JPG", 190,10,60,20
Control Add Graphic, hDlg, %IDC_GRAPHIC1, "", 20, 40, 125, 125 ' Add a graphic control
Control Add Graphic, hDlg, %IDC_GRAPHIC2, "", 170, 40, 125, 125 ' Add a graphic control
Control Add Graphic, hDlg, %IDC_GRAPHIC3, "", 320, 40, 125, 125 ' Add a graphic control
Dialog Show Modal hDlg Call DlgProc
End Function
CallBack Function DlgProc() As Long
If CB.Msg = %WM_Command Then
Select Case CB.Ctl
Case 110
LoadImage_GDIP "garyface.gif", hDlg, %IDC_Graphic1
Graphic Redraw
Case 120
LoadImage_GDIP "cowgirl.bmp", hDlg, %IDC_Graphic2
Graphic Redraw
Case 130
LoadImage_GDIP "daniel.jpg", hDlg, %IDC_Graphic3
Graphic Redraw
End Select
End If
End Function
Function LoadImage_GDIP(sFileName As String, targetHandle As DWord, targetID as Long) As Long
'with Graphic Control targetHandle is hDlg and %IDC_Graphic1
'with Memory Bitmap targetHandle is hBMP and 0
'with Graphic Window targetHandle is hWin and 0
Local hr As Long, token AS DWord, hDC As DWord
Local hStatus As Long, pGraphics AS DWord, pImage As DWord
Local StartupInput AS GdiplusStartupInput
' Initialize GDI+
StartupInput.GdiplusVersion = 1
hr = GdiplusStartup(token, StartupInput, ByVal %NULL)
If hr Then Function = 0 : Exit Function 'return 0 if fails
Graphic Attach targetHandle, targetID ' Select the drawing target
Graphic Get DC TO hdc ' Retrieve the handle of the device context
Graphic Color %BLACK, %WHITE ' Set the foreground and background color
Graphic Clear ' Clear the entire selected graphic window
sFileName = UCode$(sFileName)
hStatus = GdipLoadImageFromFile(StrPTR(sFileName), pImage) ' Create the Image object
hStatus = GdipCreateFromHDC(hDC, pGraphics) ' Create the Graphic object
hStatus = GdipDrawImage(pGraphics, pImage, 0, 0) ' Draw the image
If pImage Then GdipDisposeImage(pImage) ' Cleanup
If pGraphics Then GdipDeleteGraphics(pGraphics) ' Cleanup
GdiplusShutdown token ' Shutdown GDI+
Function = 1 ' Success
End Function
'gbs_00413
'Date: 03-10-2012
http://www.garybeene.com/sw/gbsnippets.htm