Detect invalid filetype doing GUI ADD PICTURE Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
joefiesta
Posts: 502
Joined: 24 Jan 2016, 13:54
Location: Pa., USA

Detect invalid filetype doing GUI ADD PICTURE  Topic is solved

10 Oct 2022, 09:40

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.
User avatar
mikeyww
Posts: 27372
Joined: 09 Sep 2014, 18:38

Re: Detect invalid filetype doing GUI ADD PICTURE

10 Oct 2022, 10:16

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}
}
garry
Posts: 3795
Joined: 22 Dec 2013, 12:50

Re: Detect invalid filetype doing GUI ADD PICTURE

10 Oct 2022, 10:20

@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
User avatar
mikeyww
Posts: 27372
Joined: 09 Sep 2014, 18:38

Re: Detect invalid filetype doing GUI ADD PICTURE

10 Oct 2022, 10:22

Yes, if the conversion or export succeeds, then the process works, and an exception is not needed (or thrown).
joefiesta
Posts: 502
Joined: 24 Jan 2016, 13:54
Location: Pa., USA

Re: Detect invalid filetype doing GUI ADD PICTURE

10 Oct 2022, 10:31

@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?
User avatar
mikeyww
Posts: 27372
Joined: 09 Sep 2014, 18:38

Re: Detect invalid filetype doing GUI ADD PICTURE

10 Oct 2022, 11:32

What should :arrow: 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:
threw an exception or set ERRORLEVEL or something.
image221010-1234-001.png
Programmed output
image221010-1234-001.png (32.89 KiB) Viewed 473 times
garry
Posts: 3795
Joined: 22 Dec 2013, 12:50

Re: Detect invalid filetype doing GUI ADD PICTURE

10 Oct 2022, 12:12

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") }
}
;============================================================================

User avatar
mikeyww
Posts: 27372
Joined: 09 Sep 2014, 18:38

Re: Detect invalid filetype doing GUI ADD PICTURE

10 Oct 2022, 12:17

Ohhhhh, garry, but we need to know which one is the best! :)
garry
Posts: 3795
Joined: 22 Dec 2013, 12:50

Re: Detect invalid filetype doing GUI ADD PICTURE

10 Oct 2022, 13:31

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 )

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
}
;----------------------

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Bing [Bot], Mateusz53, mikeyww and 280 guests