If I accidentalliy save a photoshop picture (.PSD filetype) with a .JPG extension, when I use that file for GUI, ADD, PICTURE the picture does not appear. Totally understandable. And, it is documented that AHK does not support filetype .PSD for GUI, ADD, PICTURE.
The problem is this: how can I tell this is going to happen before I try to add the invalidly named file to a GUI? It would be awful nice if GUI, ADD, PICTURE threw an exception or set ERRORLEVEL or something.
Detect invalid filetype doing GUI ADD PICTURE Topic is solved
Re: Detect invalid filetype doing GUI ADD PICTURE
Code: Select all
image = d:\images\temp.jpg
If !imgSize(image).h
Throw "Um, that is exceptional work!"
imgSize(img) { ; https://www.autohotkey.com/boards/viewtopic.php?f=76&t=81665
; Returns an array indicating the image's width (w) and height (h), obtained from the file's properties
SplitPath, img, fn, dir
objShell := ComObjCreate("Shell.Application")
objFolder := objShell.NameSpace(dir), objFolderItem := objFolder.ParseName(fn)
scale := StrSplit(RegExReplace(objFolder.GetDetailsOf(objFolderItem, 31), ".(.+).", "$1"), " x ")
Return {w: scale.1, h: scale.2}
}
Re: Detect invalid filetype doing GUI ADD PICTURE
@mikeyww thank you , have always a solution / answer
maybe if open xy.psd can save as > jpg ( or other formats ) with irfanview
https://www.irfanview.com/main_formats.htm
maybe if open xy.psd can save as > jpg ( or other formats ) with irfanview
https://www.irfanview.com/main_formats.htm
Re: Detect invalid filetype doing GUI ADD PICTURE
Yes, if the conversion or export succeeds, then the process works, and an exception is not needed (or thrown).
Re: Detect invalid filetype doing GUI ADD PICTURE
@mikeyww : your code has an error. The THROW command fails. But, the rest is awesome. Thanks!!!
can I ask: where did you learn that kind of stuff you wrote? Another language?
can I ask: where did you learn that kind of stuff you wrote? Another language?
Re: Detect invalid filetype doing GUI ADD PICTURE
What should Throw do that it is not doing?
Addressing the second issue is a bit harder. Yes, some other languages, plus reading documentation and forum posts!
What the script does:
Addressing the second issue is a bit harder. Yes, some other languages, plus reading documentation and forum posts!
What the script does:
threw an exception or set ERRORLEVEL or something.
Re: Detect invalid filetype doing GUI ADD PICTURE
I tried to collect imagesize functions, here 3 examples
Code: Select all
P1:="c:\test.jpg"
;---------- 1st ----------------------
aa1:= imgSize1(P1).w,bb1:= imgSize1(P1).h
msgbox,Function-1`nW=%aa1%`nH=%bb1%
;---------- 2nd -----------------------
imgSize2(P1,imgw,imgh)
aa2:=imgw,bb2:=imgh
msgbox,Function-2`nW=%aa2%`nH=%bb2%
;---------- 3thd -----------------------
size := GetImageSize(P1)
aa3:=size.W , bb3:=size.H
msgbox,Function-3`nW=%aa3%`nH=%bb3%
return
;---------------------------------------
;-1st function
;----------------------
imgSize1(img) { ; https://www.autohotkey.com/boards/viewtopic.php?f=76&t=81665
; Returns an array indicating the image's width (w) and height (h), obtained from the file's properties
SplitPath, img, fn, dir
objShell := ComObjCreate("Shell.Application")
objFolder := objShell.NameSpace(dir), objFolderItem := objFolder.ParseName(fn)
scale := StrSplit(RegExReplace(objFolder.GetDetailsOf(objFolderItem, 31), ".(.+).", "$1"), " x ")
Return {w: scale.1, h: scale.2}
}
;---------------------
;- 2nd function
;---------------------
imgSize2(img, ByRef width , ByRef height) { ;-https://www.autohotkey.com/boards/viewtopic.php?p=355790#p355790 mikeyww
If FileExist(img) {
GUI, Add, Picture, hwndpic, %img%
ControlGetPos,,, width, height,, ahk_id %pic%
Gui, Destroy
} Else height := width := 0
}
;----------------------
;- 3thd function
;----------------------
GetImageSize(imageFilePath) { ;- https://www.autohotkey.com/boards/viewtopic.php?p=355794#p355794 teadrinker
if !hBitmap := LoadPicture(imageFilePath, "GDI+")
throw "Failed to load the image"
VarSetCapacity(BITMAP, size := 4*4 + A_PtrSize*2, 0)
DllCall("GetObject", "Ptr", hBitmap, "Int", size, "Ptr", &BITMAP)
DllCall("DeleteObject", "Ptr", hBitmap)
Return { W: NumGet(BITMAP, 4, "UInt"), H: NumGet(BITMAP, 8, "UInt") }
}
;============================================================================
Re: Detect invalid filetype doing GUI ADD PICTURE
Ohhhhh, garry, but we need to know which one is the best!
Re: Detect invalid filetype doing GUI ADD PICTURE
no idea, get always correct solution, I used your 2nd example which creates a new GUI to get imagesize
P1:="c:\test.psd" ; exists and get aa2=0 and bb2=0 ( this worked > msgbox ERROR )
P1:="c:\test.psd" ; exists and get aa2=0 and bb2=0 ( this worked > msgbox ERROR )
Code: Select all
;P1:="c:\test.jpgxx" ; not exists
;P1:="c:\test.jpg" ; exists
P1:="c:\test.psd" ; exists
SplitPath,P1, name, dir, ext, name_no_ext, drive
;---------- 2nd -----------------------
ifexist,%p1%
{
imgSize2(P1,imgw,imgh)
aa2:=imgw,bb2:=imgh
if (aa2=0 or bb2=0)
msgbox, ERROR > Extension=%ext% / AHK does not support filetype %ext%`nFunction-2`nW=%aa2%`nH=%bb2%
else
msgbox,OK > Extension=%ext%`nFunction-2`nW=%aa2%`nH=%bb2%
}
else
msgbox,File %p1% not exists
return
;---------------------
;- 2nd function
;---------------------
imgSize2(img, ByRef width , ByRef height) { ;-https://www.autohotkey.com/boards/viewtopic.php?p=355790#p355790 mikeyww
If FileExist(img) {
GUI, Add, Picture, hwndpic, %img%
ControlGetPos,,, width, height,, ahk_id %pic%
Gui, Destroy
} Else height := width := 0
}
;----------------------