Date: 02-16-2022
Return to Index
created by gbSnippets
'Once a bitmap has been inserted into a resource file and compiled into the
'EXE it possible to determine the size of the bitmap. This information can
'be useful when sizing a graphic target where the resource bitmap is to be
'loaded.
'Primary Code:
'Here are two ways to get the image dimensions, a DDT approach and a GDI
'approach. Both require only a few lines of code.
'Credit: Dave Biggs
'1. DDT - put the bitmap into a memory bitmap and use Graphic Get Client
' to get the width/height of the image.
Local hBMP as DWord, w as Long, h as Long
Graphic Bitmap Load "update", 0, 0 to hBMP
Graphic Attach hBMP, 0
Graphic Get Client To w,h
Graphic Bitmap End
'2. GDI API - load the image and uset GetObject to extract information
' about the image, including it's width/height.
Local hbmp As DWord, w As Long, h As Long, Bmp As Bitmap
hBmp = LoadImage(GetModuleHandle(""), "cowgirl", %IMAGE_BITMAP, 0, 0, %LR_DEFAULTCOLOR)
GetObject hBmp, SizeOf(Bmp), Bmp
DeleteObject hBMP
MsgBox "BitMap width: "+Str$(Bmp.bmWidth)+$CRLF+ _
"BitMap height: "+Str$(Bmp.bmHeight)
'Compilable Example: (Jose Includes)
#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
Function PBMain() As Long
Dialog New Pixels, 0, "Test Code",300,300,200,200, %WS_OverlappedWindow To hDlg
Control Add Button, hDlg, 100,"DDT Method", 50,10,100,20
Control Add Button, hDlg, 101,"GDI API Method", 50,40,100,20
Dialog Show Modal hDlg Call DlgProc
End Function
CallBack Function DlgProc() As Long
Local hbmp As DWord, w As Long, h As Long, Bmp As Bitmap
Local hStatus as DWord, hInstance as DWord, strResourceName as String
Local pImage As DWord, dwImageWidth as Long, dwImageHeight as Long, hr as DWord
Local token as DWord, hDC as DWord
If CB.Msg = %WM_Command AND CB.Ctl = 100 AND CB.Ctlmsg = %BN_Clicked Then
Graphic Bitmap Load "cowgirl", 0, 0 to hBMP
Graphic Attach hBMP, 0
Graphic Get Canvas To w,h
Graphic Bitmap End
MsgBox Str$(w) + Str$(h)
ElseIf CB.Msg = %WM_Command AND CB.Ctl = 101 AND CB.Ctlmsg = %BN_Clicked Then
hBmp = LoadImage(GetModuleHandle(""), "cowgirl", %IMAGE_BITMAP, 0, 0, %LR_DEFAULTCOLOR)
GetObject hBmp, SizeOf(Bmp), Bmp
DeleteObject hBMP
MsgBox "BitMap width: "+Str$(Bmp.bmWidth)+$CRLF+"BitMap height: "+Str$(Bmp.bmHeight)
End If
End Function
'gbs_00474
'Date: 03-10-2012
http://www.garybeene.com/sw/gbsnippets.htm