VBA get transparency
Posted: 2016-10-04T05:35:44-07:00
I’ve been struggling for several days with an issue. I’ve read a lot of forum posts but didn’t find any solution for the moment.
I’m programming on VBA. I want to know wether an image has transparency (i.e. alpha channel present and transparent pixels). I use the IM command proposed here viewtopic.php?t=18596 :
This code gives the mean of the alpha pixels.
I call this command from VBA, usint "WScript.Shell" :
I use shellCommand.Run and not shellCommand.Exec, because I want the shell window not to be visible and it seems only feasible with Run.
To get the result of the shell command, I forward it to a text file (I would have preferred to get the result directly, but didn’ find the way to do it).
The problem with the above code is that the tempfile.txt is created, but it is empty. Do you have any idea why ?
By the way I’ve tested the command directly from the DOS command prompt, and it works : the tempfile.txt contains the result.
I’m programming on VBA. I want to know wether an image has transparency (i.e. alpha channel present and transparent pixels). I use the IM command proposed here viewtopic.php?t=18596 :
Code: Select all
convert image.png -alpha on -alpha extract -format "%[fx:mean]" info:
I call this command from VBA, usint "WScript.Shell" :
Code: Select all
Set shellCommand = VBA.CreateObject("WScript.Shell")
cmdLine = “convert image.png -alpha on -alpha extract -format "%[fx:mean]" info: > C:\tempfile.txt”
windowStyle = 0
waitOnReturn = True
returnCode = shellCommand.Run("%comspec% /c " & cmdLine, intWindowStyle, bWaitOnReturn)
I use shellCommand.Run and not shellCommand.Exec, because I want the shell window not to be visible and it seems only feasible with Run.
To get the result of the shell command, I forward it to a text file (I would have preferred to get the result directly, but didn’ find the way to do it).
The problem with the above code is that the tempfile.txt is created, but it is empty. Do you have any idea why ?
By the way I’ve tested the command directly from the DOS command prompt, and it works : the tempfile.txt contains the result.