Page 1 of 1

Solved: VB: How to use dynamic/variable convert parameters?

Posted: 2011-08-26T12:55:54-07:00
by ipgimg
Hi,

How can I use a variable to feed parameters to my convert command? For example instead of:

Code: Select all

myImg.Convert(inputfile, "-resize","1500x1500", outputfile)
use something like a list or array:

Code: Select all

Dim params As New List(Of String)
params.add("-resize")
params.add("1500x1500")
myImg.Convert(inputfile, params, outputfile)
or

Code: Select all

Dim params As String
params = " """-resize""","""1500x1500""" "
myImg.Convert(inputfile, params, outputfile)
I'm looking for some way not to have to hard code the conversion into the program, perhaps read command lines from a text file.

Thanks

Re: VB: How to use dynamic/variable convert parameters?

Posted: 2011-08-31T09:05:53-07:00
by ipgimg
Hi,

It's hard to believe this software is seldom used in the VB/.NET world, but there are almost no examples or documentation anywhere to be found. I'm putting this here for anyone else to find in future searches. As you'll see I'm not an accomplished VB coder but it works.

I got the basic concept of a parameter array from this post,viewtopic.php?f=8&t=18140,but could not get any output when trying to feed in file names and parameters from a text file containing the commands I wanted to execute.

Here is how I was able to use variable parameters with the ImageMagick COM object image.convert()

I put all the ImageMagick parameters in a single line string (read from a text file) including the input and output file names as the first and last values, e.g.

Code: Select all

Dim sCommand As String
sCommand = "c:\inputfile.tif,-strip,-density,96,-profile,USWebCoatedSWOP.icc,-profile,sRGB_v4_ICC_preference.icc,-quality,100,-resize,680x680^,c:\outputfile.jpg"
Split the string into an array,

Code: Select all

Dim iParams(1) As Object
iParams = sCommand.Split(",")
At this point I though that

Code: Select all

retval = myImg.convert(iParams)
would work but it didn't, and eventually I saw the error "specified array was not of the expected type". Seems the split command messed up the array, so I copied the values into a new array:

Code: Select all

Dim sParams(1) As Object
Dim cNum As Integer
Dim i As Integer

cNum = iParams.Length

ReDim sParams(cNum)
For i = 0 To cNum - 1
	sParams(i) = iParams(i)
Next

myImg.convert(sParams)
WORKS!

Re: Solved: VB: How to use dynamic/variable convert paramete

Posted: 2012-01-09T08:34:42-07:00
by yhhuang
Using TypeLib Information Library is good implementation but solution. Before, I'd written a program using TLI Application with perfect call-by-name mechanism that it helps to dynamically feed parameters to the MagickImage.Convert method, and then I found it's hard to deploy because TLI library is supplied by VB6 IDE.

Then, I used VB6 built-in command `CallByName`. If a conversion job needs five arguments, I have a code feeding five arguments to Convert method. If it needs three arguments, I have a code feeding three. The whole code looks like that,

Sub Convert1(ParamArray args() As Variant)
......
N = UBound(args) - LBound(args) + 1
Select Case N
......
Case 5
imobj.Convert "-density", "150", "-alpha", "off", args(1), args(2), args(3), args(4), args(5), target
Case 6
imobj.Convert "-density", "150", "-alpha", "off", args(1), args(2), args(3), args(4), args(5), args(6), target
......
Case Else
RaiseError "No feasible method-call for these arguments."
End Select
......

And, I have another code to generate those dummy codez.

Re: Solved: VB: How to use dynamic/variable convert parameters?

Posted: 2018-03-27T21:03:28-07:00
by Alexvb6
In the case you are not in VB, but using VBScript or ASP, you cannot use an "Object" (it does not exists in VbScript or ASP).
Here is how to proceed, after having spent HOURS on this topic !

You have to list ALL the arguments (all the items of your Array), but set the ones you do not need as "Empty" !

Code: Select all

'Declare Array of optional arguments
Dim optionalArg(3)
optionalArg(0) = "-rotate"
optionalArg(1) = "67"
optionalArg(2) = Empty
optionalArg(3) = "-negate"

'Execute
Set ImgObj = CreateObject("ImageMagickObject.MagickImage.1")
	ImgObj.Convert "c:\Source.jpg", optionalArg(0), optionalArg(1), optionalArg(2), optionalArg(3), "c:\Destination.jpg"
Set ImgObj = Nothing
Hope this will HELP !