 |
AutoHotkey Community Let's help each other out
|
| View previous topic :: View next topic |
| Author |
Message |
Albireo
Joined: 01 Feb 2006 Posts: 240
|
Posted: Tue Mar 02, 2010 12:01 am Post subject: Right proportions - Images GUI |
|
|
Hello!
When to display images, using a GUI window
How to control the size?
Assume that the reserved area of the GUI window is 300 x 300pixels
The command is:
| Code: | | Gui, Add, Picture, w300 h-1, C:\My Pictures\Company Logo.gif |
This works, if the images is more wide than high
But, It doesn't work if the images it is more high then whide.
How todo? |
|
| Back to top |
|
 |
closed
Joined: 07 Feb 2008 Posts: 509
|
Posted: Tue Mar 02, 2010 1:09 pm Post subject: |
|
|
calculate a bit?
| Code: | FileSelectFile, image_file
gosub show_picture
return
show_picture:
Gui, destroy
gui,add,picture,hwndpic vmypic,%image_file%
ControlGetPos, ,,width,height,,ahk_id%pic%
ratio:=(width/height)
if ratio>=1
delta:=(width/300),w=300,h:=floor(height/delta)
if ratio<1
delta:=(height/300),h=300,w:=floor(width/delta)
GuiControl,,mypic,*w%w% *h%h% %image_file%
Gui, show,w330 h330
return
guiclose:
ExitApp,
esc::
ExitApp, |
|
|
| Back to top |
|
 |
jaco0646
Joined: 07 Oct 2006 Posts: 3113 Location: MN, USA
|
Posted: Tue Mar 02, 2010 4:10 pm Post subject: |
|
|
| AHK Help File wrote: | | To shrink or enlarge the image while preserving its aspect ratio, specify -1 for one of the dimensions and a positive number for the other. | h300 w-1 |
|
| Back to top |
|
 |
Leef_me
Joined: 08 Apr 2009 Posts: 5333 Location: San Diego, California
|
Posted: Tue Mar 02, 2010 9:01 pm Post subject: |
|
|
| yume wrote: | | calculate a bit? | Why not let AHk do most of the work? Thanks to "yume" for the framework.
| Code: | FileSelectFile, image_file
gosub show_picture
return
show_picture:
Gui, destroy
gui,add,picture,hwndpic vmypic,%image_file%
ControlGetPos, ,,width,height,,ahk_id%pic%
ratio:=(width/height)
if ratio>=1
GuiControl,,mypic,*w300 *h-1 %image_file%
else
GuiControl,,mypic,*h300 *w-1 %image_file%
Gui, show,w330 h330
return
guiclose:
ExitApp,
esc::
ExitApp, |
|
|
| Back to top |
|
 |
Albireo
Joined: 01 Feb 2006 Posts: 240
|
Posted: Sat Mar 20, 2010 4:23 pm Post subject: |
|
|
Thank's!
The solution is quite good
Would like to add two "features" in the example above from "yume" and "leef_me"
(I don´t think it is difficult, but don't know where I put the Offset in the program code)
Offset:
If the input image is 1600x1200 pixels and the "image window" is 300 x 300 pixels
the result image is 300 x 225 pixels.
I want the following Offset: (the image = landscape)
xOffset = 0
yOffset = 300 - 225 = 75
75 / 2 = 37,5
yOffset = 37 or 38
or the result image is 225 x 300 pixels
I want the following Offset: (the image = portrait)
xOffset = 37 or 38
yOffset = 0
____________________________________________________________
The second thing is if the image is smaller than 300 x 300 pixels (240 x 240)
I don't want "magnify" the picture to 300x300 pixels.
//Jan |
|
| Back to top |
|
 |
Leef_me
Joined: 08 Apr 2009 Posts: 5333 Location: San Diego, California
|
Posted: Sat Mar 20, 2010 11:04 pm Post subject: |
|
|
It's bee 18 days. What have you tried?
You really do need to at least attempt to read and understand the documentation. |
|
| Back to top |
|
 |
Albireo
Joined: 01 Feb 2006 Posts: 240
|
Posted: Sun Mar 21, 2010 5:54 pm Post subject: |
|
|
| Leef_me wrote: | | It's bee 18 days. What have you tried?. |
During this time, I have not only tried to solve this
It isn't easy to understand "GUI".
And the documentation not have many examples
I have tried to understand the program you gave me earlier.
But failed to fix the "offset" problem
- - - - - - - - - - - - - - - - -
There are some commands I don't understand in your code.
| Code: | | gui,add,picture,hwndpic vmypic,%image_file% |
GUI - Is that not a graphical command?
What is hwndpic? or vmypic in this case?
( I want to know the image size )
| Code: | | ControlGetPos, ,,width,height,,ahk_id%pic% |
"ControlGetPos - Retrieves the position and size of a control" from manual
Here I get apparently the size of the image
(This means that the hwndpic from the last command points to the image)
| Code: | GuiControl ,,mypic, *w300 *h-1 %image_file%
; and
GuiControl ,,mypic, *w-1 *h300 %image_file% |
These commands determine the size of the image.
- - - - - - - - - - - - - - - - -
I have tried to do something i can handle / understand
But it doesn't work
Here is the code
| Code: | #SingleInstance force
#NoEnv
SetBatchLines -1
; ImageFile = D:\Temp\Pic\orig\hatching_orig.jpg
; ImageFile = D:\Temp\Pic\orig\rot90.jpg
FileSelectFile ImageFile
Gosub ShowPicture
Return
ShowPicture:
; Gui Destroy ; Removes the window
Gui 1:Show, x130 y90 h570 w1000, Test Image
Gui 1:Add, Text, x300 y5 w310 h1 0x7 ;Horizontal Line 1 > Black
Gui 1:Add, Text, x300 y315 w310 h1 0x7 ;Horizontal Line 2 > Black
Gui 1:Add, Text, x300 y5 w1 h310 0x7 ;Vertical Line 1 > Black
Gui 1:Add, Text, x610 y5 w1 h310 0x7 ;Vertical Line 2 > Black
Gui 1:Add, Picture, hwndpic vmypic, %ImageFile%
ControlGetPos ,,, Width, Height,, ahk_id%pic%
Ratio:=(Width/Height)
if Ratio>=1
GuiControl ,,mypic, x305 y10 *w300 *h-1 %ImageFile%
; Gui 1:Add, Picture, x305 y10 w-1 h300 , %ImageFile%
else
GuiControl ,,mypic, x305 y10 *w-1 *h300 %ImageFile%
; Gui 1:Add, Picture, x305 y10 w300 h-1 , %ImageFile%
Gui 1:Show
Return
GuiClose1:
ExitApp
ESC::
ExitApp
|
//Jan |
|
| Back to top |
|
 |
Albireo
Joined: 01 Feb 2006 Posts: 240
|
Posted: Sun Mar 21, 2010 10:13 pm Post subject: |
|
|
Now I have try this script
It works only for "landscape" images (not for portrait)
(and not for images smaller than 300 x 300 pixels)
| Code: | #SingleInstance force
#NoEnv
SetBatchLines -1
; ImageFile = D:\Temp\Pic\orig\hatching_orig.jpg
; ImageFile = D:\Temp\Pic\orig\rot90.jpg
FileSelectFile ImageFile
Gosub ShowPicture
Return
ShowPicture:
; Gui Destroy ; Removes the window
Gui 1:Show, x130 y90 h570 w1000, Test Image
Gui 1:Add, Text, x300 y5 w310 h1 0x7 ;Horizontal Line 1 > Black
Gui 1:Add, Text, x300 y315 w310 h1 0x7 ;Horizontal Line 2 > Black
Gui 1:Add, Text, x300 y5 w1 h310 0x7 ;Vertical Line 1 > Black
Gui 1:Add, Text, x610 y5 w1 h310 0x7 ;Vertical Line 2 > Black
Gui Add, Picture, hwndPic vMyPic, %ImageFile%
ControlGetPos ,,, Width, Height,, ahk_id%pic%
; MsgBox Width = %Width% Height = %Height%
Ratio:=(Width/Height)
if Ratio>=1
{ ; Landscape Image
yOff := Round((( 300 - (Height / Width*300)) / 2 ))+10
MsgBox Width = %Width% Height = %Height% `nRatio = %Ratio% Pos = %yOff%
GuiControl ,, MyPic, *h-1 *w300 %ImageFile%
; GuiControl , Move, MyPic, x305 y%yOff% *h-1 *w300 %ImageFile%
GuiControl Move, MyPic, x305 y%yOff%
}
else
{ ; Portrait Image
xOff := Round((( 300 - (Width / Height *300)) / 2 ))+305
MsgBox Width = %Width% Height = %Height% `nRatio = %Ratio% Pos = %xOff%
GuiControl ,, MyPic, *h300 *w-1 %ImageFile%
GuiControl Move, MyPic, x%xOff% y10
; GuiControl Move, MyPic, x305 y10
}
; Gui 1:Add, Picture, x305 y10 w300 h-1 , %ImageFile%
; Gui 1:Add, Picture, x305 y10 w-1 h300 , %ImageFile%
; Gui Show, w330 h330
; Gui 1:Show, AutoSise
Return
GuiClose1:
ExitApp
ESC::
ExitApp |
|
|
| Back to top |
|
 |
Leef_me
Joined: 08 Apr 2009 Posts: 5333 Location: San Diego, California
|
Posted: Sun Mar 21, 2010 11:15 pm Post subject: |
|
|
| Albireo wrote: | There are some commands I don't understand in your code.
| Code: | | gui,add,picture,hwndpic vmypic,%image_file% |
GUI - Is that not a graphical command?
What is hwndpic? or vmypic in this case?
( I want to know the image size ) |
I actually don't know what that is, I think we need to ask yume.
Ok. So I see you are trying. My apologies for being as pain, just wanted you to learn to do it yourself.
Here is my updated code with the 2 requests you made. It is just an extension of the prior version of code I posted.
edit: this is a correction of the code originally posted here
This code has been more thoroughly tested with several image sizes
| Code: | FileSelectFile, image_file
gosub show_picture
return
show_picture:
Gui, destroy
gui,add,picture,hwndpic vmypic,%image_file%
;gui,add,picture,hwnd vmypic,%image_file%
ControlGetPos, ,,width,height,,ahk_id%pic%
ratio:=(width/height)
;listvars
;msgbox
if (width<300) && (height<300) ; #2 request
{
GuiControl,,mypic,*w *h0 %image_file%
;msgbox less than 300
}
else
{
;msgbox more 300
if ratio>=1
GuiControl,,mypic,*w300 *h-1 %image_file%
else
GuiControl,,mypic,*h300 *w-1 %image_file%
}
ControlGetPos,x,y,width,height,,ahk_id%pic% ; #1 request
xOffset:= (300 - width)/2
yOffset:= (300 - height)/2
xOffset:= 30+(300 - width)/2
yOffset:= 30+(300 - height)/2
;listvars
;msgbox revised dims
ControlMove,, XOffset, YOffset, Width, Height, ahk_id%pic%
Gui, show,w330 h330
return
guiclose:
ExitApp,
esc::
ExitApp,
;f12::reload ; used during code development
|
Last edited by Leef_me on Tue Mar 23, 2010 5:08 pm; edited 1 time in total |
|
| Back to top |
|
 |
Albireo
Joined: 01 Feb 2006 Posts: 240
|
Posted: Tue Mar 23, 2010 12:50 pm Post subject: |
|
|
Thank´s! (But your script doesn't work)
I have written a new script "from scratch"
The script works, but I can see a new "problem"
When I draw the lines for the rectangle (row 13-16)- the positions is OK!
But when I place the image in the rectangle, I must adjust the value for the window frames (Row 26,27).
Where is "the problem"?
- I'm thinking wrong?
- I'm doing wrong?
(excuse for all swedish comments)
| Code: | #SingleInstance force
#NoEnv
SetBatchLines -1
; ImageFile = D:\Temp\Pic\hatching_240.png
; ImageFile = D:\Temp\Pic\hatching_300x300.png
; ImageFile = D:\Temp\Pic\orig\hatching_orig.jpg
; ImageFile = D:\Temp\Pic\orig\rot90.jpg
FileSelectFile ImageFile
Gui 1:Show, x130 y90 h570 w1000, Test Image
Gui 1:Add, Text, x300 y10 w300 h1 0x7 ;Hor Line 1 > Black
Gui 1:Add, Text, x300 y310 w300 h1 0x7 ;Hor Line 2 > Black
Gui 1:Add, Text, x300 y10 w1 h300 0x7 ;Vert Line 1 > Black
Gui 1:Add, Text, x600 y10 w1 h300 0x7 ;Vert Line 2 > Black
Gosub ShowPicture
Return
ShowPicture:
xSize = 300 ; Bredd på den ruta där bilden ska rymmas
ySize = 300 ; Höjd på den ruta där bilden ska hamna
xPos = 304 ; xPos på bildens övre vänstra hörn
yPos = 40 ; yPos på bildens övre vänstra hörn
; Gui Destroy ; Removes the window
Gui 1:Add, Picture, hwndPic vMyPic, %ImageFile%
ControlGetPos ,,, WidthPic, HeightPic,, ahk_id%pic% ; Hämta original storlek på bilden
; Beräkna ny storlek samt positionen för bilden
if ( WidthPic > xSize ) or ( HeightPic > ySize )
{ ; Kolla proportionerna på bilden ( <1 porträtt, >1 lanskap)
If ( WidthPic / HeightPic ) > 1
{ ; Liggande format (xPos har redan ett värde)
xPic = %xSize%
yPic := Round( HeightPic / WidthPic * ySize )
EnvAdd yPos, Round(( ySize - yPic ) / 2 )
GuiControl ,, MyPic, *w%xSize% *h-1 %ImageFile% ; Bredden begränsas till Max pixels - xSize
}
else
{ ; Stående format (yPos har redan ett värde)
xPic := Round( WidthPic / HeightPic * xSize )
yPic = %ySize%
EnvAdd xPos, Round(( xSize - xPic ) / 2 )
GuiControl ,, MyPic, *w-1 *h%ySize% %ImageFile% ; Höjden begränsas till Max pixels - ySize
}
}
else
{
xPic := WidthPic
EnvAdd xPos, Round(( xSize - WidthPic ) / 2 )
yPic := HeightPic
EnvAdd yPos, Round(( ySize - HeightPic ) / 2 )
GuiControl ,, MyPic,*w *h0 %ImageFile%
}
; Resultat Pos för den nya bilden samt storlek
; MsgBox WidthPic = %WidthPic% `tHeightPic = %HeightPic%`n`nxPic `t= %xPic% `tyPic `t= %yPic%`nxPos `t= %xPos% `tyPos `t= %yPos%
; Listvars
ControlMove ,, xPos, yPos, xPic, yPic, ahk_id%pic%
Return
GuiClose1:
ExitApp
ESC::
ExitApp
|
//Jan[/code] |
|
| Back to top |
|
 |
Leef_me
Joined: 08 Apr 2009 Posts: 5333 Location: San Diego, California
|
Posted: Tue Mar 23, 2010 5:10 pm Post subject: |
|
|
Albireo, I have made corrections to my prior code listing and tested it more properly.
please download and try the code again. |
|
| Back to top |
|
 |
flyingDman
Joined: 27 Feb 2009 Posts: 690 Location: Burbank, California
|
Posted: Tue Mar 23, 2010 8:06 pm Post subject: |
|
|
I am working on a script that creates html pages, each with a jpg and text. I can determine the appropriate size that I give to each image in the html code (<IMG SRC="../images/img-%counter%.jpg" width="%imgw%" BORDER="0">) but only after opening the image in a GUI using ControlGetPos in order to get the width and height of the image first.
Is there a way (dllcall?) to get the dimensions of a jpg (or other image) without opening the file. I.e a "FileGetDimensions" function similar to FileGetSize? _________________ "Data is not information, information is not knowledge, knowledge is not understanding, understanding is not wisdom" but let's start to get the data... |
|
| Back to top |
|
 |
Murx Guest
|
Posted: Tue Mar 23, 2010 8:24 pm Post subject: |
|
|
| Interestingly that info is displayed within a tooltip if you hoover over the image file within explorer. I guess its some EXIF stuff ... |
|
| Back to top |
|
 |
closed
Joined: 07 Feb 2008 Posts: 509
|
Posted: Tue Mar 23, 2010 9:05 pm Post subject: |
|
|
| Code: | fileselectfile,myfile
GDIPToken := Gdip_Startup()
pBitM := Gdip_CreateBitmapFromFile( myfile )
WxH := Gdip_GetImageWidth( pBitM ) "x" Gdip_GetImageHeight( pBitM )
Gdip_DisposeImage( pBitM )
Gdip_Shutdown( GDIPToken )
MsgBox, %WxH%
#Include GDIP.ahk |
| Quote: | There are some commands I don't understand in your code.
Code:
gui,add,picture,hwndpic vmypic,%image_file%
|
from the helpfile:
| Quote: | | HwndOutputVar [v1.0.46.01+]: When used with Gui Add, this option stores the window handle (HWND) of the newly created control in OutputVar. For example: Gui, Add, Edit, vMyEdit HwndMyEditHwnd |
| Quote: | | If the target control has an associated variable, specify the variable's name as the ControlID (this method takes precedence over the ones described next). For this reason, it is usually best to assign a variable to any control that will later be accessed via GuiControl or GuiControlGet, even if that control is not an input-capable type (such as GroupBox or Text). |
|
|
| Back to top |
|
 |
flyingDman
Joined: 27 Feb 2009 Posts: 690 Location: Burbank, California
|
Posted: Tue Mar 23, 2010 9:58 pm Post subject: |
|
|
Thanks Yume! This will work I think. Although not very fast (got the size of 6500 images in about 3 minutes on my machine; see below), it beats opening each file in a AHK window!
| Code: | FileSelectFolder, myfolder
counter = 0
StartTime := A_TickCount
GDIPToken := Gdip_Startup()
Loop, %myfolder%\*.jpg
{
pBitM := Gdip_CreateBitmapFromFile( A_LoopFileFullPath )
WxH := Gdip_GetImageWidth( pBitM ) "x" Gdip_GetImageHeight( pBitM )
Gdip_DisposeImage( pBitM )
++counter
}
Gdip_Shutdown( GDIPToken )
ElapsedTime := A_TickCount - StartTime . "`n" . ElapsedTime
MsgBox, % counter . "files done in" . ElapsedTime
|
_________________ "Data is not information, information is not knowledge, knowledge is not understanding, understanding is not wisdom" but let's start to get the data... |
|
| Back to top |
|
 |
|
|
You can post new topics in this forum You can reply to topics in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|