Date: 02-16-2022
Return to Index
created by gbSnippets
'This code shows how to resize a Graphic control as the dialog
'is resized. Also, the image is redrawn to fill the graphic control
'while maintaining the original image aspect ratio.
'Compiler Comments:
'This code compiles only in PBWin10. It uses a resizable Graphic Control,
'which is not available in PBWin9. However, this link, http://gbl_00528,
'shows an approach to use in PBWin9.
'Primary Code:
'This Sub generates the new image size
Sub GetNewImageSize(wCont As Long, hCont As Long, wImg As Long, hImg As Long, wNew As Long, hNew As Long)
'wCont,hCont = container size : hImg,wImg = original image size : wNew,hNew = image size to fit in container
wNew = wImg / Max(wImg / wCont, hImg / hCont)
hNew = hImg / Max(wImg / wCont, hImg / hCont)
End Sub
'Compilable Example: (Jose Includes)
#Compiler PBWin 10
#Compile EXE
#Dim All
%Unicode=1
#Include "Win32API.inc"
#Resource Bitmap girl "cowgirl.bmp"
%IDC_Graphic = 600
Global hDlg,hBMP,wImg,hImg As Dword
Function PBMain() As Long
Dialog New Pixels, 0, "Image Resize",300,300,300,200, %WS_OverlappedWindow To hDlg
Control Add Graphic, hDlg, %IDC_Graphic,"",10,10,200,150, %WS_Border
Graphic Attach hDlg, %IDC_Graphic
Dialog Show Modal hDlg Call DlgProc
End Function
CallBack Function DlgProc() As Long
Local x,y,wNew,hNew,w,h As Long
Select Case Cb.Msg
Case %WM_InitDialog
Graphic Bitmap Load "girl", 0, 0 To hBMP 'put image in memory bitmap
Graphic Attach hBMP,0
Graphic Get Canvas To wImg,hImg
Graphic Attach hDlg, %IDC_Graphic
Graphic Copy hBMP,0 'copy from memory bitmap to graphic control, put in upper/left
Case %WM_Size
Dialog Get Client hDlg To w,h
Control Set Size hDlg, %IDC_Graphic, w-20, h-20
RedrawImage
End Select
End Function
Sub GetNewImageSize(wCont As Long, hCont As Long, wImg As Long, hImg As Long, wNew As Long, hNew As Long)
'wCont,hCont = container size : hImg,wImg = original image size : wNew,hNew = image size to fit in container
wNew = wImg / Max(wImg / wCont, hImg / hCont)
hNew = hImg / Max(wImg / wCont, hImg / hCont)
End Sub
Sub RedrawImage
Local w,h,x,y,wNew,hNew As Long
Graphic Clear
Graphic Get Client To w,h
GetNewImageSize(w,h,wImg,hImg,wNew,hNew) 'get resized image dimensions
x = (w-wNew)/2 : y = (h-hNew)/2 'upper/left position so resized image is centered
Graphic Stretch hBMP, 0, (0,0)-(wImg-1,hImg-1) To (x,y)-(x+wNew-1,y+hNew-1) 'copy (resized) from memory bitmap to visible image
End Sub
'gbs_01136
'Date: 03-03-2012
http://www.garybeene.com/sw/gbsnippets.htm