Picture control aspect ratio

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Bunny1
Posts: 66
Joined: 18 Nov 2014, 17:01

Picture control aspect ratio

13 May 2015, 15:37

Is their a way to keep a picture's aspect ratio in a picture control?

For example.

Code: Select all

Gui,Add,Picture,w475 h375,
If I make the W or H -1. the picture will stretch in that direction to it's original size. I want the control to be that size and the picture to keep it's width aspect ratio.
toralf
Posts: 868
Joined: 27 Apr 2014, 21:08
Location: Germany

Re: Picture control aspect ratio

13 May 2015, 22:44

You have to specify one dimension and set the other to -1
ciao
toralf
Bunny1
Posts: 66
Joined: 18 Nov 2014, 17:01

Re: Picture control aspect ratio

14 May 2015, 10:40

toralf wrote:You have to specify one dimension and set the other to -1
I know. I've read the documentation several times.
I was hoping their was a way to keep the aspect ratio within a set control size.
kon
Posts: 1756
Joined: 29 Sep 2013, 17:11

Re: Picture control aspect ratio

14 May 2015, 13:44

Maybe use WIA. The example (WIASample.ahk) even shows scaling an image for a picture control.
toralf
Posts: 868
Joined: 27 Apr 2014, 21:08
Location: Germany

Re: Picture control aspect ratio

14 May 2015, 20:18

Bunny1 wrote: I was hoping their was a way to keep the aspect ratio within a set control size.
I do not understand. What do you mean by 'within a a set control size'? The aspect ratio of the picture is what it is. If you want to keep it the control has to have the same aspect ratio or it will adjust to that ratio.
ciao
toralf
kon
Posts: 1756
Joined: 29 Sep 2013, 17:11

Re: Picture control aspect ratio

14 May 2015, 23:29

Is there an easier way to get the original width and height? I just wanted to try justme's excellent new WIA lib. :)

Code: Select all

; Requires: WIA.ahk (ahkscript.org/boards/viewtopic.php?&t=7254) in your Lib folder or #Included

#NoEnv
GBW := 800 ; Groupbox width
GBH := 800 ; Groupbox height
FileSelectFile, ImgPath,,, Select an image file.
Gui, Add, GroupBox, x10 y10 w%GBW% h%GBH%
; Load the image
If !(ImgObj := WIA_LoadImage(ImgPath)) {
   MsgBox, 16, Error, Couldn't load %ImgPath%!`n`nThe program will exit.
   ExitApp
}
FileW := ImgObj.Width
FileH := ImgObj.Height
ImgObj := ""
W := 700	; Maximum width
H := 300	; Maximum height
if ((Ratio1 := FileW / W) > (Ratio2 := FileH / H))
	H := Round(FileH / Ratio1)
else
	W := Round(FileW / Ratio2)
X := Round(GBW//2 - W//2 + 10)
Y := Round(GBH//2 - H//2 + 10)
Gui, Add, Pic, % "x" . X . " y" . Y . " w" . W . " H" . H, %ImgPath%
Gui, Add, Text, % "x10 y" (GBH+20), % "FileW: " FileW 
									. "`nFileH: " FileH 
									. "`nX: " X 
									. "`nY: " Y 
									. "`nW: " W 
									. "`nH: " H
Gui, Show
return

GuiClose:
GuiEscape:
ExitApp
Bunny1
Posts: 66
Joined: 18 Nov 2014, 17:01

Re: Picture control aspect ratio

15 May 2015, 11:29

toralf wrote:
Bunny1 wrote: I was hoping their was a way to keep the aspect ratio within a set control size.
I do not understand. What do you mean by 'within a a set control size'? The aspect ratio of the picture is what it is. If you want to keep it the control has to have the same aspect ratio or it will adjust to that ratio.
I have a picture control that is h375 by w475. it can't be any bigger than that or it will start hitting other controls.
I haven't seen the height have any problem, but the width is stretched across the control and the picture is distorted.
Bunny1
Posts: 66
Joined: 18 Nov 2014, 17:01

Re: Picture control aspect ratio

15 May 2015, 11:39

kon wrote:Is there an easier way to get the original width and height? I just wanted to try justme's excellent new WIA lib. :)

Code: Select all

; Requires: WIA.ahk (ahkscript.org/boards/viewtopic.php?&t=7254) in your Lib folder or #Included

#NoEnv
GBW := 800 ; Groupbox width
GBH := 800 ; Groupbox height
FileSelectFile, ImgPath,,, Select an image file.
Gui, Add, GroupBox, x10 y10 w%GBW% h%GBH%
; Load the image
If !(ImgObj := WIA_LoadImage(ImgPath)) {
   MsgBox, 16, Error, Couldn't load %ImgPath%!`n`nThe program will exit.
   ExitApp
}
FileW := ImgObj.Width
FileH := ImgObj.Height
ImgObj := ""
W := 700	; Maximum width
H := 300	; Maximum height
if ((Ratio1 := FileW / W) > (Ratio2 := FileH / H))
	H := Round(FileH / Ratio1)
else
	W := Round(FileW / Ratio2)
X := Round(GBW//2 - W//2 + 10)
Y := Round(GBH//2 - H//2 + 10)
Gui, Add, Pic, % "x" . X . " y" . Y . " w" . W . " H" . H, %ImgPath%
Gui, Add, Text, % "x10 y" (GBH+20), % "FileW: " FileW 
									. "`nFileH: " FileH 
									. "`nX: " X 
									. "`nY: " Y 
									. "`nW: " W 
									. "`nH: " H
Gui, Show
return

GuiClose:
GuiEscape:
ExitApp
I'll have another look at it. I spent about an hour and half yesterday trying to get it to work. I couldn't figure out how to get the filepath into it. the filepath is stored in %Successfuldownload% after a picture is downloaded with urldownloadtofile. the filepath changes each time a picture is download based on the title of the picture and username of the person that uploaded it.
kon
Posts: 1756
Joined: 29 Sep 2013, 17:11

Re: Picture control aspect ratio

15 May 2015, 13:45

The example script I posted prompts the user for an image file useing FileSelectFile. The file path is stored in the ImgPath variable. This: ImgObj := WIA_LoadImage(ImgPath) loads the image and saves a reference to the image object into the ImgObj var. Note that there are no %% around the var since it is used in a function, and functions use expression syntax.

A bit of explanation about the example: It loads an image file into a WIA image object. Then the file's width/height are retrieved from the object. With a bit of math it calculates the x,y,w,h to use for the picture control. The picture will be displayed centered within the groupbox.
Bunny1
Posts: 66
Joined: 18 Nov 2014, 17:01

Re: Picture control aspect ratio

15 May 2015, 17:55

kon wrote:The example script I posted prompts the user for an image file useing FileSelectFile. The file path is stored in the ImgPath variable. This: ImgObj := WIA_LoadImage(ImgPath) loads the image and saves a reference to the image object into the ImgObj var. Note that there are no %% around the var since it is used in a function, and functions use expression syntax.

A bit of explanation about the example: It loads an image file into a WIA image object. Then the file's width/height are retrieved from the object. With a bit of math it calculates the x,y,w,h to use for the picture control. The picture will be displayed centered within the groupbox.
Thank you. that does look like what I want.
Maybe I'm being really stupid, but I can't seem to get it to show the picture.

This is the code I'm using.

Code: Select all

SetBatchLines -1
ListLines Off
#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.
#SingleInstance force
#Include WIA.ahk

;INI READ=======================
Iniread, pathtofolderSF, folder.ini, Filepath Sofurry, folder, ERROR ;read ini for downlaod location
Ifinstring, pathtofolderSF, ERROR
	Msgbox Please select a folder to create the Sofurry artist folder in.
Else

GBW := 485 ; Groupbox width
GBH := 385 ; Groupbox height
Gui, Add, GroupBox, x600 y5 w%GBW% h%GBH%
; Load the image
ImgPath = %Successfuldownload%
ImgObj := WIA_LoadImage(ImgPath)
FileW := ImgObj.Width
FileH := ImgObj.Height
ImgObj := ""
W := 475    ; Maximum width
H := 375    ; Maximum height
if ((Ratio1 := FileW / W) > (Ratio2 := FileH / H))
    H := Round(FileH / Ratio1)
else
    W := Round(FileW / Ratio2)
X := Round(GBW//2 - W//2 + 10)
Y := Round(GBH//2 - H//2 + 10)

;ButtonS========================
x := 0
FileList := {}
gui,font,s15
;SOFURRY======a========================
Gui,Add,GroupBox, x20 y20 w250 h80 section, Setup
Gui Add,Button, xs5 ys30 0x1, Filepath ;Button
Gui Add,Button, x+10 0x1, Open folder ;Button
;OTHER ButtonS=========================
Gui,Add,GroupBox, x20 y125 w250 h190 section, Other
Gui Add,Button, xs10 ys30 gExit 0x1, Close control panel ;Button
Gui Add,Button, xs10 y+10 0x1, Reload app ;Button
Gui Add,Button, xs10 y+10 gShutdown, Shutdown app ;Button

Gui, Add, Pic, % "x" . X . " y" . Y . " w" . W . " H" . H, %ImgPath%

Gui,Add,Listview, x5 y400 r12 w1550 gAddtolist 0x1 NoSort, Title|Format|Site|Location|Creation Date|Megabytes|Kilobytes
return

;CONTROL PANEL SUBROUTINES=============
#v::
Gui,Show,w1700 autosize,Control panel
return

;SOFURRY=========================
ButtonFilepathSofurry:
FileSelectFolder, pathtofolderSF,,
IniWrite, %pathtofolderSF%, folder.ini, Filepath Sofurry, folder
return

ButtonOpenfolderSofurry:
Run %pathtofolderSF%,
return

;OTHER ButtonS=========================
Exit:
gui,cancel
return

ButtonReloadapp:
Reload
return

Shutdown:
Exitapp
return

;DOWNLOADER============================
#IfWinActive ahk_class MozillaWindowClass
#a::
clipsaved = %clipboard%
Send, {F6} ;select url in Address bar.
Send ^c ;copy url to clipboard.
sleep 400
Clipboard2 = %Clipboard%
AlreadyExists = 
AlreadyExists++
;==========================================
;==========================================
;==========================================
;SOFURRY===================================
;==========================================
;==========================================
;==========================================
Website1 = Sofurry.com
IfInString, clipboard, %Website1%
{
;SOURCEPAGE================================
	Send != ;Keyconfig shortcut to use easy copy to copy source page to clipboard.
	sleep 200
	If clipboard = %clipsaved%
		Msgbox Source page not found
	Else

;USERNAME==========================
	RegExMatch(clipboard, "sfTextMedium"">(.*?)</span>", SubPat) ;find username and place in variable.
	If subpat1 =
		Msgbox Username not found
	Else
		Username = %subpat1%

;TITLE========================
	RegExMatch(clipboard, "<title>(.*?) SoFurry</title>", SubPat2) ;find title and place in variable.
	If subpat21 =
		Msgbox Title not found
	Else
		String = %subpat21%
		StringTrimRight, titlea, String, 10
		StringReplace, OutputVar1, SubPat21, :, , ReplaceAll?
		StringReplace, OutputVar2, OutputVar1, /, , ReplaceAll?
		StringReplace, OutputVar3, OutputVar2, ?, , ReplaceAll?
		StringReplace, OutputVar4, OutputVar3, !, , ReplaceAll?
		StringReplace, OutputVar5, OutputVar4, `", , ReplaceAll?
		StringReplace, OutputVar6, OutputVar5, `%, , ReplaceAll?
		StringReplace, OutputVar7, OutputVar6, *, , ReplaceAll?
		StringReplace, OutputVar, OutputVar7, |, , ReplaceAll?
		Titlea = %OutputVar%
;URL===========================
	Ifinstring, clipboard, sfContentFlash
	{
		StringReplace, URL, Clipboard2, https://www.sofurry.com/view/, https://www.sofurryfiles.com/std/content?page=, ReplaceAll?
		Format = swf
	}
	Else
	{
		StringReplace, URL, Clipboard2, https://www.sofurry.com/view/, https://www.sofurryfiles.com/std/content?page=, ReplaceAll?
		Format = png
	}

;DOWNLOAD==================================
	FileCreateDir, %pathtofolderSF%\%Username%\ ;The folder where artist's folders will be created.
	sleep 400

Title = %Titlea% by %Username%
;If it doesn't exist do this============================
	Ifnotexist %pathtofolderSF%\%Username%\%Title%.%Format%
	{
		urldownloadtofile, %URL%, %pathtofolderSF%\%Username%\%Title%.%Format%
		Ifexist %pathtofolderSF%\%Username%\%Title%.%Format%
		{
			Successfuldownload = %pathtofolderSF%\%Username%\%Title%.%Format%
			GuiControl,,Picturea,%Successfuldownload%
			SelectedFile = %pathtofolderSF%\%Username%\%Title%.%Format%
			FileGetTime, Creation, %pathtofolderSF%\%Username%\%Title%.%Format%, C
			FormatTime, Creation,Creation
			FileGetSize, Kilobytes, %pathtofolderSF%\%Username%\%Title%.%Format%, K
			FileGetSize, Megabytes, %pathtofolderSF%\%Username%\%Title%.%Format%, M
			SplitPath, SelectedFile,,,, FileName         ; Split the file name from the full path
			LV_Insert(1, "", FileName ,Format,Website1,SelectedFile,Creation,Megabytes,Kilobytes)              ; Add the file name to the Listview
			LV_ModIfyCol()
			FileList[FileName] := SelectedFile      ; Store the file's path in the array, using the file name as the key
			Gosub, Addtolist
			clipboard = %clipsaved%
			Msgbox,0,Download succesful, %Title% downloaded. `n` `n` Don't forget to fave and rate what you just downloaded.,3
;Add TO LIST===========================================================
			return
		}
		Else
		{
			Msgbox Picture not downloaded.
			Msgbox %username% `n`n` %title% `n`n` %url% `n`n` %Format% `n`n` %pathtofolderSF%\%Username%\%Title%.%Format%
			clipboard = %clipsaved%
			return
		}
	}
;If it already exists do this============================
	Else Ifexist %pathtofolderSF%\%Username%\%Title%.%Format%
	{
		Msgbox, 262182,,File arleady exists, The picture you are trying to download already exists. `n` Would you like to `n` Cancel and stop the download. `n` Try again to rename the file to be downloaded to include _1 at the end. `n` Continue to overwrite the file that already exists.
		IfMsgbox, Cancel
		{
			clipboard = %clipsaved%
			return
		}
		IfMsgbox, TryAgain
		{
			urldownloadtofile, %URL%, %pathtofolderSF%\%Username%\%Title%_%AlreadyExists%.%Format%
			Ifexist %pathtofolderSF%\%Username%\%Title%_%AlreadyExists%.%Format%
			{
				Successfuldownload = %pathtofolderSF%\%Username%\%Title%_%AlreadyExists%.%Format%
				GuiControl,,Picturea,%Successfuldownload%
				SelectedFile = %pathtofolderSF%\%Username%\%Title%_%AlreadyExists%.%Format%
				FileGetTime, Creation, %pathtofolderSF%\%Username%\%Title%_%AlreadyExists%.%Format%, C
				FormatTime, Creation,Creation
				FileGetSize, Kilobytes, %pathtofolderSF%\%Username%\%Title%_%AlreadyExists%.%Format%, K
				FileGetSize, Megabytes, %pathtofolderSF%\%Username%\%Title%_%AlreadyExists%.%Format%, M
				SplitPath, SelectedFile,,,, FileName         ; Split the file name from the full path
				LV_Insert(1, "", FileName ,Format,Website1,SelectedFile,Creation,Megabytes,Kilobytes)               ; Add the file name to the Listview
				LV_ModIfyCol()
				FileList[FileName] := SelectedFile      ; Store the file's path in the array, using the file name as the key
				Gosub, Addtolist
				clipboard = %clipsaved%
				Msgbox,0,%Title%_%AlreadyExists% downloaded.,3 `n` `n` Don't forget to fave and rate what you just downloaded.,3
;Add TO LIST===========================================================
				return
			}
			Else
			{
				Msgbox Picture not downloaded.
				Msgbox %username% `n`n` %title% `n`n` %url% `n`n` %Format% `n`n` %pathtofolderSF%\%Username%\%Title%.%Format%
				clipboard = %clipsaved%
				return
			}
		}
		IfMsgbox, Continue
		{
			urldownloadtofile, %URL%, %pathtofolderSF%\%Username%\%Title%.%Format%
			Ifexist %pathtofolderSF%\%Username%\%Title%.%Format%
			{
				Successfuldownload = %pathtofolderSF%\%Username%\%Title%.%Format%
				GuiControl,,Picturea,%Successfuldownload%
				SelectedFile = %pathtofolderSF%\%Username%\%Title%.%Format%
				FileGetTime, Creation, %pathtofolderSF%\%Username%\%Title%.%Format%, C
				FormatTime, Creation,Creation
				FileGetSize, Kilobytes, %pathtofolderSF%\%Username%\%Title%.%Format%, K
				FileGetSize, Megabytes, %pathtofolderSF%\%Username%\%Title%.%Format%, M
				SplitPath, SelectedFile,,,, FileName         ; Split the file name from the full path
				LV_Insert(1, "", FileName ,Format,Website1,SelectedFile,Creation,Megabytes,Kilobytes)               ; Add the file name to the Listview
				LV_ModIfyCol()
				FileList[FileName] := SelectedFile      ; Store the file's path in the array, using the file name as the key
				Gosub, Addtolist
				clipboard = %clipsaved%
				Msgbox,0,Download succesful, %Title% by %Username% downloaded. `n` `n` Don't forget to fave and rate what you just downloaded.,3
;Add TO LIST===========================================================
				return
			}
			Else
			{
				Msgbox Picture not downloaded.
				Msgbox %username% `n`n` %title% `n`n` %url% `n`n` %Format% `n`n` %pathtofolderSF%\%Username%\%Title%.%Format%
				clipboard = %clipsaved%
				return
			}
		}
	}
}
return

Addtolist:
If (A_GuiEvent = "DoubleClick")
{
	RowNumber  := LV_GetNext()
	LV_GetText(RowItem, RowNumber)
	Run % FileList[RowItem]
	return
}
return

Pictures:
run,%Successfuldownload%
return
kon
Posts: 1756
Joined: 29 Sep 2013, 17:11

Re: Picture control aspect ratio

15 May 2015, 18:07

I apologize for not mentioning this sooner, but I think WIA is for Windows Vista and later (probably won't work with XP?). Sorry if that is the problem and I have wasted your time.
Does my example script work for you?

Also, before the image is loaded, have you checked if Successfuldownload contains the path?

Code: Select all

...
;INI READ=======================
Iniread, pathtofolderSF, folder.ini, Filepath Sofurry, folder, ERROR ;read ini for downlaod location
Ifinstring, pathtofolderSF, ERROR
    Msgbox Please select a folder to create the Sofurry artist folder in.
;Else		;<------- remove this probably

GBW := 485 ; Groupbox width
GBH := 385 ; Groupbox height
Gui, Add, GroupBox, x600 y5 w%GBW% h%GBH%
; Load the image
MsgBox, %Successfuldownload%		;<------- does this contain the path?
ImgObj := WIA_LoadImage(Successfuldownload)
...
kon
Posts: 1756
Joined: 29 Sep 2013, 17:11

Re: Picture control aspect ratio

15 May 2015, 18:13

Another thing. I noticed you changed the coordinates on this line: Gui, Add, GroupBox, x600 y5 w%GBW% h%GBH%.
In my example the groupbox is at x10 y10. This is important when calculating the x and y of the picture control:

Code: Select all

X := Round(GBW//2 - W//2 + 600)
Y := Round(GBH//2 - H//2 + 5)
Bunny1
Posts: 66
Joined: 18 Nov 2014, 17:01

Re: Picture control aspect ratio

15 May 2015, 18:25

kon wrote:Another thing. I noticed you changed the coordinates on this line: Gui, Add, GroupBox, x600 y5 w%GBW% h%GBH%.
In my example the groupbox is at x10 y10. This is important when calculating the x and y of the picture control:

Code: Select all

X := Round(GBW//2 - W//2 + 600)
Y := Round(GBH//2 - H//2 + 5)
I'm running windows 7 64bit
Yes, the example works.
It doesn't contain the path at that point. I used a message box to check and it was blank.
if I move the shortcut above the buttons the picture shows up, but I can only use the shortcut once and then I get a variables can only be used once error.
The else is if the pathtofolderSF variable does not contain error it will continue on to setup the buttons for the control panel. it may not be needed, but it worked, so I kept it. I've been improving the code as I learn more about autohotkey.
kon
Posts: 1756
Joined: 29 Sep 2013, 17:11

Re: Picture control aspect ratio

15 May 2015, 19:03

Here's another example.
Only use Gui, Add, Pic, x%GBX% y%GBY% vPic1 once to create the picture control.
To change the picture use GuiControl.
To adjust the picture location and dimensions use GuiControl, Move
I wrote a function to make calculating the image coordinates easier. Give the function the image path, the max W and H, and the X,Y,W,H of the area in which to display the image. It will return the X,Y,W,H to use with GuiControl, Move.

Code: Select all

; Requires: WIA.ahk (ahkscript.org/boards/viewtopic.php?&t=7254) in your Lib folder or #Included

#NoEnv
GBX := 10   ; Groupbox x
GBY := 10   ; Groupbox y
GBW := 800  ; Groupbox width
GBH := 800  ; Groupbox height
Gui, Add, GroupBox, x%GBX% y%GBY% w%GBW% h%GBH%
Gui, Add, Button, % "gAddImage x10 y" (GBH+20), Add Image
Gui, Add, Pic, x%GBX% y%GBY% vPic1
Gui, Show
return  ; End of Auto-execute / Gui creation

AddImage:       ; Changes the image when the button is pressed
FileSelectFile, ImgPath,,, Select an image file.
Coords := GetScaledDimensions(ImgPath, 700, 400, GBX, GBY, GBW, GBH)    ; get image coordinates of image scaled to max 700x400
GuiControl, Move, Pic1, % "x" . Coords.X . " y" . Coords.Y . " w" . Coords.W . " h" . Coords.H		; Move the picture control to the new coordinates
GuiControl,, Pic1, %ImgPath%	; Put the new image into the picture control
return

GuiClose:
GuiEscape:
ExitApp

; Gets the x,y,w,h of a scaled image. Preserves aspect ration
GetScaledDimensions(ImgPath, MaxW, MaxH, AreaX, AreaY, AreaW, AreaH) {
   If !(ImgObj := WIA_LoadImage(ImgPath)) {
      MsgBox, 16, Error, Couldn't load %ImgPath%!
      return
   }
   FileW := ImgObj.Width    ; Get original image dimensions
   FileH := ImgObj.Height
   if ((Ratio1 := FileW / MaxW) > (Ratio2 := FileH / MaxH))     ; Determine whether the width or height governs
       MaxH := Round(FileH / Ratio1)
   else
       MaxW := Round(FileW / Ratio2)
   X := Round(AreaW//2 - MaxW//2 + AreaX)
   Y := Round(AreaH//2 - MaxH//2 + AreaY)
   return {X: X, Y: Y, W: MaxW, H: MaxH}
}
Bunny1
Posts: 66
Joined: 18 Nov 2014, 17:01

Re: Picture control aspect ratio

15 May 2015, 19:16

kon wrote:Here's another example.
Only use Gui, Add, Pic, x%GBX% y%GBY% vPic1 once to create the picture control.
To change the picture use GuiControl.
To adjust the picture location and dimensions use GuiControl, Move
I wrote a function to make calculating the image coordinates easier. Give the function the image path, the max W and H, and the X,Y,W,H of the area in which to display the image. It will return the X,Y,W,H to use with GuiControl, Move.

Code: Select all

; Requires: WIA.ahk (ahkscript.org/boards/viewtopic.php?&t=7254) in your Lib folder or #Included

#NoEnv
GBX := 10   ; Groupbox x
GBY := 10   ; Groupbox y
GBW := 800  ; Groupbox width
GBH := 800  ; Groupbox height
Gui, Add, GroupBox, x%GBX% y%GBY% w%GBW% h%GBH%
Gui, Add, Button, % "gAddImage x10 y" (GBH+20), Add Image
Gui, Add, Pic, x%GBX% y%GBY% vPic1
Gui, Show
return  ; End of Auto-execute / Gui creation

AddImage:       ; Changes the image when the button is pressed
FileSelectFile, ImgPath,,, Select an image file.
Coords := GetScaledDimensions(ImgPath, 700, 400, GBX, GBY, GBW, GBH)    ; get image coordinates of image scaled to max 700x400
GuiControl, Move, Pic1, % "x" . Coords.X . " y" . Coords.Y . " w" . Coords.W . " h" . Coords.H		; Move the picture control to the new coordinates
GuiControl,, Pic1, %ImgPath%	; Put the new image into the picture control
return

GuiClose:
GuiEscape:
ExitApp

; Gets the x,y,w,h of a scaled image. Preserves aspect ration
GetScaledDimensions(ImgPath, MaxW, MaxH, AreaX, AreaY, AreaW, AreaH) {
   If !(ImgObj := WIA_LoadImage(ImgPath)) {
      MsgBox, 16, Error, Couldn't load %ImgPath%!
      return
   }
   FileW := ImgObj.Width    ; Get original image dimensions
   FileH := ImgObj.Height
   if ((Ratio1 := FileW / MaxW) > (Ratio2 := FileH / MaxH))     ; Determine whether the width or height governs
       MaxH := Round(FileH / Ratio1)
   else
       MaxW := Round(FileW / Ratio2)
   X := Round(AreaW//2 - MaxW//2 + AreaX)
   Y := Round(AreaH//2 - MaxH//2 + AreaY)
   return {X: X, Y: Y, W: MaxW, H: MaxH}
}
Yeah. that works.
Bunny1
Posts: 66
Joined: 18 Nov 2014, 17:01

Re: Picture control aspect ratio

15 May 2015, 19:52

kon wrote:Here's another example.
Only use Gui, Add, Pic, x%GBX% y%GBY% vPic1 once to create the picture control.
To change the picture use GuiControl.
To adjust the picture location and dimensions use GuiControl, Move
I wrote a function to make calculating the image coordinates easier. Give the function the image path, the max W and H, and the X,Y,W,H of the area in which to display the image. It will return the X,Y,W,H to use with GuiControl, Move.

Code: Select all

; Requires: WIA.ahk (ahkscript.org/boards/viewtopic.php?&t=7254) in your Lib folder or #Included

#NoEnv
GBX := 10   ; Groupbox x
GBY := 10   ; Groupbox y
GBW := 800  ; Groupbox width
GBH := 800  ; Groupbox height
Gui, Add, GroupBox, x%GBX% y%GBY% w%GBW% h%GBH%
Gui, Add, Button, % "gAddImage x10 y" (GBH+20), Add Image
Gui, Add, Pic, x%GBX% y%GBY% vPic1
Gui, Show
return  ; End of Auto-execute / Gui creation

AddImage:       ; Changes the image when the button is pressed
FileSelectFile, ImgPath,,, Select an image file.
Coords := GetScaledDimensions(ImgPath, 700, 400, GBX, GBY, GBW, GBH)    ; get image coordinates of image scaled to max 700x400
GuiControl, Move, Pic1, % "x" . Coords.X . " y" . Coords.Y . " w" . Coords.W . " h" . Coords.H		; Move the picture control to the new coordinates
GuiControl,, Pic1, %ImgPath%	; Put the new image into the picture control
return

GuiClose:
GuiEscape:
ExitApp

; Gets the x,y,w,h of a scaled image. Preserves aspect ration
GetScaledDimensions(ImgPath, MaxW, MaxH, AreaX, AreaY, AreaW, AreaH) {
   If !(ImgObj := WIA_LoadImage(ImgPath)) {
      MsgBox, 16, Error, Couldn't load %ImgPath%!
      return
   }
   FileW := ImgObj.Width    ; Get original image dimensions
   FileH := ImgObj.Height
   if ((Ratio1 := FileW / MaxW) > (Ratio2 := FileH / MaxH))     ; Determine whether the width or height governs
       MaxH := Round(FileH / Ratio1)
   else
       MaxW := Round(FileW / Ratio2)
   X := Round(AreaW//2 - MaxW//2 + AreaX)
   Y := Round(AreaH//2 - MaxH//2 + AreaY)
   return {X: X, Y: Y, W: MaxW, H: MaxH}
}
I got it working. thank you. I've got to tweak the groupbox size a little, but it's doing what I was looking for.

I changed the button setup to this.

Code: Select all

GBX := 600   ; Groupbox x
GBY := 5  ; Groupbox y
GBW := 476  ; Groupbox width
GBH := 376  ; Groupbox height

;ButtonS========================
x := 0
FileList := {}
gui,font,s15
;SOFURRY======a========================
Gui,Add,GroupBox, x20 y20 w250 h80 section, Setup
Gui Add,Button, xs5 ys30 0x1, Filepath ;Button
Gui Add,Button, x+10 0x1, Open folder ;Button
;OTHER ButtonS=========================
Gui,Add,GroupBox, x20 y125 w250 h190 section, Other
Gui Add,Button, xs10 ys30 gExit 0x1, Close control panel ;Button
Gui Add,Button, xs10 y+10 0x1, Reload app ;Button
Gui Add,Button, xs10 y+10 gShutdown, Shutdown app ;Button

Gui, Add, GroupBox, x%GBX% y%GBY% w%GBW% h%GBH%
Gui, Add, Pic, x%GBX% y%GBY% vPic1
I added this to the successful download section.

Code: Select all

Coords := GetScaledDimensions(Successfuldownload, 475, 375, GBX, GBY, GBW, GBH)    ; get image coordinates of image scaled to max 700x400
GuiControl, Move, Pic1, % "x" . Coords.X . " y" . Coords.Y . " w" . Coords.W . " h" . Coords.H      ; Move the picture control to the new coordinates
GuiControl,, Pic1, %Successfuldownload%    ; Put the new image into the picture control
just me
Posts: 9442
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: Picture control aspect ratio

16 May 2015, 01:37

If you want to use the WIA lib, there might be another way:

Code: Select all

#NoEnv
SetBatchLines, -1

Successfuldownload := "Test.jpg" ; you need to put in a valid image path here !!!

PicW := 475
PicH := 375
Gui, Margin, 20, 20
Gui, Add, Picture, w%PicW% h%PicH% hwndHPIC Border +0x020E ; important, set SS_CENTERIMAGE (0x0200) | SS_BITMAP (0x0E)
Gui, Add, Button, gLoadPic, Click here to load the picture!
Gui, Show, , WIA Test
Return

GuiClose:
Gui, Destroy
ExitApp

LoadPic:
Gui, +OwnDialogs
; Load the image
If !(ImgObj := WIA_LoadImage(Successfuldownload)) {
   MsgBox, 16, Error, Couldn't load %Successfuldownload%!
   Return
}
; Determine how to scale
If (ImgObj.Width - PicW) > (ImgObj.Height - PicH) {
   ScaleW := PicW
   ScaleH := 0
}
Else {
   ScaleW := 0
   ScaleH := PicH
}
; Scale the image
Scaled := WIA_ScaleImage(ImgObj, ScaleW, ScaleH)
; Get the bitmap data
PicObj := WIA_GetImageBitmap(Scaled)
; STM_SETIMAGE message: Set the control's image.
SendMessage, 0x172, 0, % PicObj.Handle, , ahk_id %HPIC%
; Delete the previous bitmap, if any
If (Errorevel) 
   DllCall("DeleteObject", "Ptr", ErrorLevel)
Return

#Include WIA.ahk
But it will center the image within the control.
kon
Posts: 1756
Joined: 29 Sep 2013, 17:11

Re: Picture control aspect ratio

16 May 2015, 13:23

Thanks just me! I tried doing it that way at first too. I was surprised that despite what the documentation says about ScaleImage preserving the aspect ratio, we still need to calculate whether height or width governs.
I didn't think to use +0x020E or DllCall("DeleteObject", "Ptr", ErrorLevel) when I attempted it. Thanks for demonstrating!

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: OrangeCat and 241 guests