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

ImageMagickObject is a Windows COM+ interface to ImageMagick. COM+, Visual Basic, and Delphi users should post to this discussion group.
Post Reply
ipgimg
Posts: 4
Joined: 2011-07-27T14:55:01-07:00
Authentication code: 8675308

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

Post 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
Last edited by ipgimg on 2011-08-31T09:20:30-07:00, edited 1 time in total.
ipgimg
Posts: 4
Joined: 2011-07-27T14:55:01-07:00
Authentication code: 8675308

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

Post 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!
yhhuang
Posts: 3
Joined: 2012-01-08T20:51:37-07:00
Authentication code: 8675308

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

Post 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.
Alexvb6
Posts: 49
Joined: 2012-10-12T16:50:15-07:00
Authentication code: 67789

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

Post 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 !
Post Reply