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.
'Primary Code:
'Credit: Jose Roca
'This Sub reads a file and draws it on the hDC of the graphic target. At
'that point, an application can then use any of the PowerBASIC tools to
'manipulate the image.
Sub GDIP_DrawImage (ByVal hdc AS DWord, strFileName As String)
Local hStatus As Long, pGraphics AS DWord, pImage AS DWord
hStatus = GdipCreateFromHDC(hdc, pGraphics)
strFileName = UCode$(strFileName)
hStatus = GdipLoadImageFromFile(strFileName, pImage) 'create image object
hStatus = GdipDrawImage(pGraphics, pImage, 0, 0) 'draw image at 0,0
If pImage Then GdipDisposeImage(pImage)
If pGraphics Then GdipDeleteGraphics(pGraphics)
End Sub
'Compilable Example: (Jose Includes)
'In this case, the graphic target is a Graphic Control. The code will work
'equally well for Memory Bitmap and a Graphic Window graphic targets.
#Compiler PBWin 9, PBWin 10
#Compile EXE
#Dim All
%Unicode=1
#Include "Win32API.inc"
#Include "GDIPLUS_Simple.INC"
#Resource "gbsnippets.pbr"
Global hDlg as DWord
%IDC_GRAPHIC = 101
Function PBMain() As Long
Local hr As Long, hdc AS DWord, token AS DWord
Local StartupInput AS GdiplusStartupInput
StartupInput.GdiplusVersion = 1 ' Initialize GDI+
hr = GdiplusStartup(token, StartupInput, ByVal %NULL)
Dialog New Pixels, 0, "GDI+ Load Image Test",300,300,250,260, %WS_OverlappedWindow To hDlg
Control Add Button, hDlg, 110,"Load GIF", 10,10,60,20
Control Add Graphic, hDlg, %IDC_GRAPHIC, "", 20, 40, 200, 200 ' Add a graphic control
Graphic Attach hDlg, %IDC_GRAPHIC
Graphic Color %Black, %White : Graphic Clear
Dialog Show Modal hDlg Call DlgProc
GDIPlusShutDown token
End Function
CallBack Function DlgProc() As Long
If CB.Msg = %WM_Command AND CB.Ctl = 110 Then
Local hDC as DWord
Graphic Get DC TO hdc
GDIP_DrawImage hDC, "garyface.gif"
Graphic Redraw
End If
End Function
Sub GDIP_DrawImage (ByVal hdc AS DWord, strFileName As String)
Local hStatus As Long, pGraphics AS DWord, pImage AS DWord
hStatus = GdipCreateFromHDC(hdc, pGraphics)
strFileName = UCode$(strFileName)
hStatus = GdipLoadImageFromFile(StrPTR(strFileName), pImage) 'create image object
hStatus = GdipDrawImage(pGraphics, pImage, 0, 0) 'draw image at 0,0
If pImage Then GdipDisposeImage(pImage)
If pGraphics Then GdipDeleteGraphics(pGraphics)
End Sub
'gbs_00176
'Date: 03-10-2012
http://www.garybeene.com/sw/gbsnippets.htm