Date: 02-16-2022
Return to Index
created by gbSnippets
'Compilable Example: (Jose Includes)
#Compiler PBWin 10
#Compile Exe
#Dim All
%Unicode = 1
#Include "Win32API.inc"
'=============================
Type GdiplusStartupInput Dword
GdiplusVersion As Dword
DebugEventCallback As Dword
SuppressBackgroundThread As Long
SuppressExternalCodecs As Long
End Type
Type GdiplusStartupOutput Dword
NotificationHook As Dword
NotificationUnhook As Dword
End Type
Declare Function GdiplusStartup Lib "gdiplus.dll" Alias "GdiplusStartup" (ByRef token As Dword, ByRef Input As GdiplusStartupInput, ByRef Output 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 filename As Dword, ByRef Image As Dword) As Long
Declare Function GdipGetImageWidth Lib "GDIPLUS.DLL" Alias "GdipGetImageWidth" (ByVal Image As Dword, ByRef Width As Dword) As Long
Declare Function GdipGetImageHeight Lib "GDIPLUS.DLL" Alias "GdipGetImageHeight" (ByVal Image As Dword, ByRef height As Dword) As Long
Declare Function GdipCreateFromHDC Lib "gdiplus.dll" Alias "GdipCreateFromHDC" (ByVal hdc As Dword, ByRef graphics As Dword) As Long
Declare Function GdipDrawImage Lib "gdiplus.dll" Alias "GdipDrawImage"(ByVal graphics As Dword, ByVal Image As Dword, ByVal x As Single, ByVal y As Single) As Long ' GpStatus
Declare Function GdipDisposeImage Lib "gdiplus.dll" Alias "GdipDisposeImage" (ByVal Image As Dword) As Long
Declare Function GdipDeleteGraphics Lib "gdiplus.dll" Alias "GdipDeleteGraphics" (ByVal graphics As Dword) As Long
'=============================
Enum Equates Singular
IDC_Button = 500
IDC_TextBox
IDC_Graphic
End Enum
Global hDlg, hBMP, token As Dword
Function PBMain() As Long
Dialog New Pixels, 0, "PowerBASIC",300,300,220,280, %WS_OverlappedWindow To hDlg
Control Add Button, hDlg, %IDC_Button,"Load Image", 10,10,100,20
Control Add TextBox, hDlg, %IDC_TextBox,"garyface.gif", 10,35,180,20
Control Add Graphic, hDlg, %IDC_Graphic,"", 10,60,200,200, %WS_Border
Dialog Show Modal hDlg Call DlgProc
End Function
CallBack Function DlgProc() As Long
Local FileName As WString, StartupInput As GdiplusStartupInput
Select Case Cb.Msg
Case %WM_InitDialog
StartupInput.GdiplusVersion = 1 ' Initialize GDI+
GdiplusStartup(Token, StartupInput, ByVal %NULL) ' Initialize GDI+
Case %WM_Command
Select Case Cb.Ctl
Case %IDC_Button
Control Get Text hDlg, %IDC_TextBox To FileName
FileName = Exe.Path$ + FileName 'FileName needs full filepath
LoadTheImage FileName 'use GDIP to load the image from a file to a bitmap (Global handle hBMP)
Graphic Attach hDlg, %IDC_Graphic
Graphic Copy hBMP, 0 'copy image from memory bitmap to graphic control
End Select
Case %WM_Destroy
GdiPlusShutDown token
End Select
End Function
Sub LoadTheImage(wFileName As WString)
'load JPG/GIF/PNG image to memory bitmap from file
Local pImage,pGraphics,hDC As Dword, w,h As Long
GdipLoadImageFromFile(StrPtr(wFileName), pImage) 'pImage - image object
GdipGetImageWidth(pImage,w) 'get width
GdipGetImageHeight(pImage,h) 'get height
Graphic Bitmap New w,h To hBmp 'hBMP is Global
Graphic Attach hBmp, 0
Graphic Get DC To hDC 'hDC is for memory bitmap
GdipCreateFromHDC(hDC, pGraphics) 'create graphic object
GdipDrawImage(pGraphics, pImage, 0,0) 'draw image at 0,0
If pImage Then GdipDisposeImage(pImage) 'GDIP cleanup
If pGraphics Then GdipDeleteGraphics(pGraphics) 'GDIP cleanup
End Sub
'gbs_01296
'Date: 05-11-2013
http://www.garybeene.com/sw/gbsnippets.htm