TimRude Posted October 20, 2022 Posted October 20, 2022 I found a snippet for loading a PNG file into a Pic control that also works well for JPG, GIF, BMP (and maybe others?). But I've discovered that after the Pic control is loaded with the image, if I resize the GUI window at all, it sets the Pic control's size to 150 x 150 and scales it according, which totally messes up the aspect ratio of the image if it wasn't already an exact square. Here's the snippet (which I've modified) demonstrating this. If you click on the image before resizing the GUI it will show the GUI client size and the Pic size and the aspect ratio of each. Then resize the GUI and notice the change in the Pic. What's going on? (I'm using AutoIt 3.3.16.1 if that matters.) expandcollapse popup; based on snippet coded by UEZ 2011 ; https://www.autoitscript.com/forum/topic/135989-png-in-gui/?do=findComment&comment=950214 #include <GUIConstantsEx.au3> #include <GDIPlus.au3> #include <WindowsConstants.au3> #include <StaticConstants.au3> Global $msg Global Const $hGUI = GUICreate("Display PNG Image in picture control", 600, 250, -1, -1, BitOR($GUI_SS_DEFAULT_GUI, $WS_MAXIMIZEBOX, $WS_SIZEBOX)) Global Const $idPic = GUICtrlCreatePic("", 215, 20) _GDIPlus_Startup() Global Const $png = StringReplace(@AutoItExe, "autoit3.exe", "Examples\GUI\Torus.png") Global Const $hImage = _GDIPlus_ImageLoadFromFile($png) Global Const $Bmp = _GDIPlus_BitmapCreateHBITMAPFromBitmap($hImage) _WinAPI_DeleteObject(GUICtrlSendMsg($idPic, $STM_SETIMAGE, $IMAGE_BITMAP, $Bmp)) GUISetState() While True $msg = GUIGetMsg() Switch $msg Case $idPic ; image was clicked, show some info Local $aSize = WinGetClientSize($hGUI) ; get client size of GUI window in $aSize[0] (width) x $aSize[1] (height) Local $hPic = GUICtrlGetHandle($idPic) ; get hWnd of picture control Local $aPos = WinGetPos($hPic) ; get current size of image in $aPos[2] (width) x $aPos[3] (height) Local $fAspectGUI = $aSize[0] / $aSize[1] Local $fAspectPic = $aPos[2] / $aPos[3] MsgBox(0, "Info", _ "GUI: " & $aSize[0] & " W x " & $aSize[1] & " H, Aspect Ratio = " & $fAspectGUI & @CRLF & @CRLF & _ "Pic: " & $aPos[2] & " W x " & $aPos[3] & " H, Aspect Ratio = " & $fAspectPic) Case $GUI_EVENT_CLOSE _WinAPI_DeleteObject($Bmp) _GDIPlus_ImageDispose($hImage) _GDIPlus_Shutdown() GUIDelete($hGUI) Exit EndSwitch WEnd
pixelsearch Posted October 20, 2022 Posted October 20, 2022 (edited) I thought this annoying 150x150 pixels issue was history but apparently it's not. @TimRudeif you indicate the pic size during the control creation, then it should be ok : ; Global Const $idPic = GUICtrlCreatePic("", 215, 20) Global Const $idPic = GUICtrlCreatePic("", 215, 20, 193, 184) ; 193, 184 = size of Torus.png Edit: Also, we can read this in AutoIt help file (topic GUICtrlCreatePic) : To set the picture control to the same size as the file content set width and height to 0. Which means, in your script : Global Const $idPic = GUICtrlCreatePic("", 215, 20, 0, 0) But when I do that (width & height to 0) then : * With AutoIt 3.3.16.1 : the pic disappears completely as soon as I start to resize the GUI ! * With AutoIt 3.3.14.5 : the pic is resized to 150x150 as soon as I start to resize the GUI Edited October 20, 2022 by pixelsearch
TimRude Posted October 21, 2022 Author Posted October 21, 2022 1 hour ago, pixelsearch said: I thought this annoying 150x150 pixels issue was history but apparently it's not. @TimRudeif you indicate the pic size during the control creation, then it should be ok : Unfortunately, I won't know the pic size before I load it into the control. I mean, for the Torus.png file I know, but I'm writing an app to view a folder full of unknown image files. 2 hours ago, pixelsearch said: Global Const $idPic = GUICtrlCreatePic("", 215, 20, 0, 0) But when I do that (width & height to 0) then : * With AutoIt 3.3.16.1 : the pic disappears completely as soon as I start to resize the GUI ! * With AutoIt 3.3.14.5 : the pic is resized to 150x150 as soon as I start to resize the GUI On mine, with AutoIt 3.3.16.1, as long as I don't specify anything for the size, it initially loads at the correct size. It doesn't disappear when I resize the GUI, it just resizes to 150x150. After some tinkering, I've come up with the following workaround that seems to fix it: Global $msg Global Const $hGUI = GUICreate("Display PNG Image in picture control", 600, 250, -1, -1, BitOR($GUI_SS_DEFAULT_GUI, $WS_MAXIMIZEBOX, $WS_SIZEBOX)) Global Const $idPic = GUICtrlCreatePic("", 215, 20) _GDIPlus_Startup() Global Const $png = StringReplace(@AutoItExe, "autoit3.exe", "Examples\GUI\Torus.png") Global Const $hImage = _GDIPlus_ImageLoadFromFile($png) Global Const $Bmp = _GDIPlus_BitmapCreateHBITMAPFromBitmap($hImage) _WinAPI_DeleteObject(GUICtrlSendMsg($idPic, $STM_SETIMAGE, $IMAGE_BITMAP, $Bmp)) Global $hPic = GUICtrlGetHandle($idPic) ; get hWnd of picture control Local $aPos = WinGetPos($hPic) ; get current size of image in $aPos[2] (width) x $aPos[3] (height) GUICtrlSetPos($idPic, Default, Default, $aPos[2], $aPos[3]) ; force control to correct size to make it stick GUISetState() Essentially, right after loading the Pic control, I use WinGetPos to determine the size of the control. Then I turn around and set the control to that same size using GUICtrlSetPos. Once that's done, the pic control maintains the proper size and aspect ratio when the GUI is resized.
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now