Date: 02-16-2022
Return to Index
created by gbSnippets
'Compilable Example: (Jose Includes)
'In this example, Memory Bitmaps 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, hBMP1 as DWord, hBMP2 as DWord, hBMP3 as DWord
%IDC_Graphic = 101
Function PBMain() As Long
Dialog New Pixels, 0, "GDI+ Load Image Test",300,300,275,220, %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_GRAPHIC, "", 60, 50, 150, 150 ' Add a graphic control
Graphic Attach hDlg, %IDC_Graphic
Graphic Color %Black, %White : Graphic Clear
Graphic Bitmap New 400,400 To hBMP1
Graphic Bitmap New 400,400 To hBMP2
Graphic Bitmap New 400,400 To hBMP3
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", hBMP1, 0
Graphic Attach hDlg, %IDC_Graphic
Graphic Copy hBMP1, 0
Graphic Redraw
Case 120
LoadImage_GDIP "cowgirl.bmp", hBMP2, 0
Graphic Attach hDlg, %IDC_Graphic
Graphic Copy hBMP2, 0
Graphic Redraw
Case 130
LoadImage_GDIP "daniel.jpg", hBMP3, 0
Graphic Attach hDlg, %IDC_Graphic
Graphic Copy hBMP3, 0
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 (pImage)
hStatus = GdipCreateFromHDC(hDC, pGraphics) ' Create the Graphic object (pGraphics)
hStatus = GdipDrawImage(pGraphics, pImage, 0, 0) ' Draw the image
'***
' At this point the image is available to both GDI+ and DDT manipulations
' Here's an attempt to save the loaded image to a JPG file.
Local s As String, EncoderClsid As GUID, OutFileName As String
s = GdiPlusGetEncoderClsid("image/jpeg")
EncoderClsid = Guid$(s)
OutFileName = UCode$("convert.jpg") + $Nul
GdipSaveImageToFile pImage, StrPTR(OutFileName), EncoderClsid, ByVal %NUll
'***
If pImage Then GdipDisposeImage(pImage) ' Cleanup
If pGraphics Then GdipDeleteGraphics(pGraphics) ' Cleanup
GdiplusShutdown token ' Shutdown GDI+
Function = 1 ' Success
End Function
'gbs_00429
'Date: 03-10-2012
http://www.garybeene.com/sw/gbsnippets.htm