Gh05t Posted June 19, 2014 Share Posted June 19, 2014 First off I would like to express my gratitude to the members of the Autoit community. I learned to program using Autoit over two years ago and thanks to the excellent help files and knowledge on these forums I've never had to post a question until today. Normally hunting around the forums is enough to find a solution to any problem I've had, but I'm at a loss on how to even describe what I'm seeing on this one. Essentially what I'm trying to do is have the user select an image and then resize the image according to its current proportions to make it display correctly on my GUI. I'll admit I'm still pretty green at programming so I may be going at this all wrong but below is a small sample from my code to show the issue and what I'm trying to do. The function to get the image size is 99% something M23 posted on the forums at some point, the rest is all my doing. expandcollapse popupUpdateLogo() Func UpdateLogo() $LogoFile=FileOpenDialog("Logo",@ScriptDir,"Images (*.jpg;*.bmp)") $Size=ImageSize($LogoFile) $Size=StringSplit($Size,"x") If $Size[1]>209 or $Size[2]>113 Then Do MsgBox(0,"Debug",$Size[1]&"x"&$Size[2],1) $Size[1]-=1 $Size[2]-=1 MsgBox(0,"Debug",$Size[1]&"x"&$Size[2],1) ;Debug catch to kill program on error If $Size[1]<-2 Or $Size[2]<-2 Then Exit EndIf Until $Size[1]<209 And $Size[2]<113 EndIf MsgBox(0,"Debug","The correct display size would be: "&$Size[1]&"x"&$Size[2]) EndFunc Func ImageSize($Image) Local $sPassed_File_Name=$Image Local $sDir_Name = StringRegExpReplace($sPassed_File_Name, "(^.*\\)(.*)", "\1") Local $sFile_Name = StringRegExpReplace($sPassed_File_Name, "^.*\\", "") Local $sDOS_Dir = FileGetShortName($sDir_Name, 1) Local $oShellApp = ObjCreate("shell.application") If IsObj($oShellApp) Then Local $oDir = $oShellApp.NameSpace($sDOS_Dir) If IsObj($oDir) Then Local $oFile = $oDir.Parsename($sFile_Name) If IsObj($oFile) Then $Details=$oDir.GetDetailsOf($oFile, 31) Return $Details Else $iError = 3 EndIf Else $iError = 2 EndIf Else $iError = 1 EndIf EndFunc The issue is that $Size[1] (the width) seems to convert to a 0 anytime I try to do a math function against it. If I had to make an educated guess I'd think Autoit thinks it's a string instead of an integer. What I find odd if that $Size[2] (the height) always works as expected. Link to comment Share on other sites More sharing options...
Solution BrewManNH Posted June 19, 2014 Solution Share Posted June 19, 2014 It is a string, StringRegExpReplace returns a string, that's why it's called STRINGRegExp, but I don't believe that is your problem. You're looking for the dimensions of the image in $Details = $oDir.GetDetailsOf($oFile, 31), the problem is that number may only work on Windows 7, file properties change every version of Windows so what is property 31 in 7 may be 20 in Vista. Use the UDF in my signature _FileGetProperty to get the proper information. You will probably have to code it to specify the OS you're looking at because not only the numbers change, but the names do as well. If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag GudeHow to ask questions the smart way! I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from. Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays. - ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script. - Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label. - _FileGetProperty - Retrieve the properties of a file - SciTE Toolbar - A toolbar demo for use with the SciTE editor - GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI. - Latin Square password generator Link to comment Share on other sites More sharing options...
Gh05t Posted June 19, 2014 Author Share Posted June 19, 2014 BrewManNH: Thank you very much for the advice. Unfortunately, I'm having what appears to be the same problem when I implemented your UDF. I should mention that I am developing on Win 7 so I am getting the correct values from the functions for the image size. I'm just unable to perform logic using these values once I have them. At this point I'm not even triggering the if statement for resizing. New code using your UDF follows: expandcollapse popupUpdateLogo() Exit Func UpdateLogo() $LogoFile=FileOpenDialog("Logo",@ScriptDir,"Images (*.jpg;*.bmp)") $Width=_FileGetProperty($LogoFile,"Width") $Height=_FileGetProperty($LogoFile,"Height") MsgBox(0,"Debug",$Width&"x"&$Height) $Width=StringTrimRight($Width,7) $Height=StringTrimRight($Height,7) MsgBox(0,"Debug",$Width&"x"&$Height) If $Width>209 or $Height>113 Then Do MsgBox(0,"Debug",$Width&"x"&$Height,1) $Width-=1 $Height-=1 MsgBox(0,"Debug",$Width&"x"&$Height,1) ;Debug catch to kill program on error If $Width<-2 Or $Height<-2 Then Exit EndIf Until $Width<209 And $Height<113 EndIf MsgBox(0,"Debug","The correct display size would be: "&$Width&"x"&$Height) EndFunc ;=============================================================================== ; Function Name.....: _FileGetProperty ; Description.......: Returns a property or all properties for a file. ; Version...........: 1.0.2 ; Change Date.......: 05-16-2012 ; AutoIt Version....: 3.2.12.1+ ; Parameter(s)......: $FGP_Path - String containing the file path to return the property from. ; $FGP_PROPERTY - [optional] String containing the name of the property to return. (default = "") ; $iPropertyCount - [optional] The number of properties to search through for $FGP_PROPERTY, or the number of items ; returned in the array if $FGP_PROPERTY is blank. (default = 300) ; Requirements(s)...: None ; Return Value(s)...: Success: Returns a string containing the property value. ; If $FGP_PROPERTY is blank, a two-dimensional array is returned: ; $av_array[0][0] = Number of properties. ; $av_array[1][0] = 1st property name. ; $as_array[1][1] = 1st property value. ; $av_array[n][0] = nth property name. ; $as_array[n][1] = nth property value. ; Failure: Returns an empty string and sets @error to: ; 1 = The folder $FGP_Path does not exist. ; 2 = The property $FGP_PROPERTY does not exist or the array could not be created. ; 3 = Unable to create the "Shell.Application" object $objShell. ; Author(s).........: - Simucal <Simucal@gmail.com> ; - Modified by: Sean Hart <autoit@hartmail.ca> ; - Modified by: teh_hahn <sPiTsHiT@gmx.de> ; - Modified by: BrewManNH ; URL...............: http://www.autoitscript.com/forum/topic/34732-udf-getfileproperty/page__view__findpost__p__557571 ; Note(s)...........: Modified the script that teh_hahn posted at the above link to include the properties that ; Vista and Win 7 include that Windows XP doesn't. Also removed the ReDims for the $av_ret array and ; replaced it with a single ReDim after it has found all the properties, this should speed things up. ; I further updated the code so there's a single point of return except for any errors encountered. ; $iPropertyCount is now a function parameter instead of being hardcoded in the function itself. ;=============================================================================== Func _FileGetProperty($FGP_Path, $FGP_PROPERTY = "", $iPropertyCount = 300) If $FGP_PROPERTY = Default Then $FGP_PROPERTY = "" $FGP_Path = StringRegExpReplace($FGP_Path, '["'']', "") ; strip the quotes, if any from the incoming string If Not FileExists($FGP_Path) Then Return SetError(1, 0, "") ; path not found Local Const $objShell = ObjCreate("Shell.Application") If @error Then Return SetError(3, 0, "") Local Const $FGP_File = StringTrimLeft($FGP_Path, StringInStr($FGP_Path, "\", 0, -1)) Local Const $FGP_Dir = StringTrimRight($FGP_Path, StringLen($FGP_File) + 1) Local Const $objFolder = $objShell.NameSpace($FGP_Dir) Local Const $objFolderItem = $objFolder.Parsename($FGP_File) Local $Return = "", $iError = 0 If $FGP_PROPERTY Then For $I = 0 To $iPropertyCount If $objFolder.GetDetailsOf($objFolder.Items, $I) = $FGP_PROPERTY Then $Return = $objFolder.GetDetailsOf($objFolderItem, $I) EndIf Next If $Return = "" Then $iError = 2 EndIf Else Local $av_ret[$iPropertyCount + 1][2] = [[0]] For $I = 1 To $iPropertyCount If $objFolder.GetDetailsOf($objFolder.Items, $I) Then $av_ret[$I][0] = $objFolder.GetDetailsOf($objFolder.Items, $I - 1) $av_ret[$I][1] = $objFolder.GetDetailsOf($objFolderItem, $I - 1) $av_ret[0][0] += 1 EndIf Next ReDim $av_ret[$av_ret[0][0] + 1][2] If Not $av_ret[1][0] Then $iError = 2 $av_ret = $Return Else $Return = $av_ret EndIf EndIf Return SetError($iError, 0, $Return) EndFunc ;==>_FileGetProperty Link to comment Share on other sites More sharing options...
Gh05t Posted June 19, 2014 Author Share Posted June 19, 2014 Correction, I now have it working with your UDF. Sometimes writing data to a file makes all the difference when it comes to debugging. It appears there was some character before the data in the variables that autoit was ignoring when it was displaying to me, but showed up in a text editor when I wrote it to a file. I added a stringtrim to remove the character and it seems to be working as I would expect. I've got some issue in my resizing logic, but I should be able to work that out on my own now. Thank you again for your help! Link to comment Share on other sites More sharing options...
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