Date: 02-16-2022
Return to Index
created by gbSnippets
'PowerBASIC GRAPHIC commands offer two ways to load an image from a file:
'1. Graphic Render - load file into target graphic
'2. Graphic Bitmap Load - put file into memory bitmap
'Here are the various routes to loading images into graphic targets - the graphic
'control, graphic window an dmemory bitmap. You can see from the listing that
'there are two paths to loading an image from a disk - Graphic Render and
'Graphic Bitmap Load
pbr -------------------------------> Render ---------------> x3 'all/resize
disk -------------------------------> Render ---------------> x3 'all/resize
x3 --------> Copy ----------------> x3 'partial/noresize
x3 --------> Stretch -------------> x3 'partial/resize
pbr ----> ImageList----------------> Graphic ImageList ---> x3 'all/noresize
pbr ----> Graphic Bitmap Load -----------------------------> Memory Bitmap 'all/noresize
disk ----> Graphic Bitmap Load -----------------------------> Memory Bitmap 'all/noresize
'Primary Code:
'Use this function to get the bitmap dimensions
Sub GetBitMapDimensions(sFileName$, x as long, y as long)
Open sFileName$ For BINARY AS #1
Get #1, 19, w
Get #1, 23, h
Close #1
End Sub
'Example#1 - Use Graphic Render to load a file
GetBitMapDimensions(sFileName$, w,h)
Control Add Graphic, hDlg, 300,"", 20, 70, w, h, %ws_border
Graphic Attach hDlg, 300
Graphic Render sFileName$, (0,0) - (99,99) 'Image from resource file
'Example#2 - Use Graphic Bitmap Load to load a file
GetBitMapDimensions(sFileName$, w,h)
Control Add Graphic, hDlg, 300,"", 20, 180, w, h, %ws_border
Graphic Attach hDlg, 300
Graphic Bitmap Load sFileName$, w, h, TO hBMP 'file is loaded, but not visible
Graphic Copy hBMP, 0
'Compilable Example: (Jose Includes)
'A image is loaded from a file.
#Compiler PBWin 9, PBWin 10
#Compile EXE
#Dim All
%Unicode=1
#Include "Win32API.inc"
#Resource "gbsnippets.pbr"
Global hDlg as DWord
Function PBMain() As Long
Dialog New Pixels, 0, "Test Code",300,300,200,300, %WS_OverlappedWindow To hDlg
Control Add Button, hDlg, 100,"Load File (Render)", 20,10,160,20
Control Add Button, hDlg, 200,"Load File (Memory Bitmap)", 20,40,160,20
Dialog Show Modal hDlg Call DlgProc
End Function
CallBack Function DlgProc() As Long
Local w as Long, h as Long, sFileName$, hBMP as DWord
If CB.Msg = %WM_Command AND CB.Ctl = 100 AND CB.Ctlmsg = %BN_Clicked Then
'use GRAPHIC RENDER to load image file
sFileName$ = "cowgirl.bmp"
GetBitMapDimensions(sFileName$, w,h)
Control Add Graphic, hDlg, 300,"", 20, 70, w, h, %ws_border
Graphic Attach hDlg, 300
Graphic Render sFileName$, (0,0) - (w-1,h-1) 'Image from resource file
End If
If CB.Msg = %WM_Command AND CB.Ctl = 200 AND CB.Ctlmsg = %BN_Clicked Then
'use GRAPHIC BITMAP LOAD to load image file, then copy to Graphic Control
sFileName$ = "cowgirl.bmp"
GetBitMapDimensions(sFileName$, w,h)
Control Add Graphic, hDlg, 300,"", 20, 180, w, h, %ws_border
Graphic Attach hDlg, 300
Graphic Bitmap Load sFileName$, w, h TO hBMP 'file is loaded, but not visible
Graphic Copy hBMP, 0
End If
End Function
Sub GetBitMapDimensions(sFileName$, w as long, h as long)
Open sFileName$ For BINARY AS #1
Get #1, 19, w
Get #1, 23, h
Close #1
End Sub
'gbs_00172
'Date: 03-10-2012
http://www.garybeene.com/sw/gbsnippets.htm