2012-01-13 50 views
0

调用ImageMagick的COM我已经成功地使用下面的命令行用一些简单的文字从VBScript

convert -size 100x100 -font arial label:blah output.gif 

我想利用VBScript中的COM接口来实现相同的结果创建一个新的GIF图像,但有在通过在相同的参数没有成功。这是我的代码...

Dim img 
Set img = CreateObject("ImageMagickObject.MagickImage.1") 

Dim fnm(6) 
fnm(0) = "-size" 
fnm(1) = "100x100" 
fnm(2) = "-font" 
fnm(3) = "arial" 
fnm(4) = "-label" 
fnm(5) = "blah" 
fnm(6) = "C:\temp\example.gif" 

retval = img.convert(fnm) 

wscript.echo("retval " & retval) 

我如下

cscript example.vbs 
0执行该代码

和输出我得到的是这样的,并没有GIF文件被创建...

Microsoft (R) Windows Script Host Version 5.8 
Copyright (C) Microsoft Corporation. All rights reserved. 

Version: ImageMagick 6.7.4-3 2011-12-24 Q16 http://www.imagemagick.org 
Copyright: Copyright (C) 1999-2012 ImageMagick Studio LLC 
Features: OpenMP 

Usage: cscript.exe [options ...] file [ [options ...] file ...] [options ...] file 

我对Windows7的使用ImageMagick-6.7.4-Q16

有谁知道我在做什么错误?

[编辑]

响应于Ekkehard的回答我也尝试不使用的阵列。此代码将创建一个输出图像,但label未应用。我还必须通过一个白色的图像作为输入,因为它不会没有一个。

msgs = img.convert("C:\temp\blank.gif", "-size", "100x100", "-font", "arial", "label:", "blah", "C:\temp\example.gif") 

回答

2

最后,我放弃了在使用COM + API时试图让label工作,并且使用-draw代替解决方案。 Ekkehard关于不使用数组是正确的,而是使用如下所示的单个参数。

msgs = img.convert("c:\temp\blank.gif", "-font", "arial", "-pointsize", "36", "-gravity", "center", "-fill", "blue", "-draw", "text 0,0 'blah'", "c:\temp\example.gif") 
0

COM接口将公开一系列用于控制对象的属性和方法。你不会通过传递一个值的数组来实例化它。在大多数情况下,您会分别设置每个属性。它可能会是这样(纯粹是一个例子,我不知道这些是此对象的不动产):

Dim img 
Set img = CreateObject("ImageMagickObject.MagickImage.1") 

img.sizeX = 100 
img.sizeY = 100 
img.font = "Arial" 
img.label = "blah" 
retval = img.convert("C:\temp\example.gif") 

WScript.echo("retval " & retval) 

对象浏览器或类型库浏览器可以用来检查COM对象的属性和方法。我的最爱是TLViewer

+0

你知道TLViewer是否可以在Windows 7上运行?我没有Visual Studio,并且无法使用regsvr32注册Tlbinf32.dll。它抱怨文件丢失(不)或者它有其他依赖的DLL。我找不到任何文档告诉我任何依赖的DLL。 BNut一个提到Windows 7的寻呼机将不支持Tlbinf32.dll – Brad 2012-01-13 23:06:05

+0

我似乎记得在兼容模式下让它在Windows 7上运行。 – Nilpo 2012-01-16 16:35:26

1

在文件夹[WhereEver]\ImageMagick-6.7.4-Q16\ImageMagickObject\Tests中,您会看到两个示例脚本(ArrayTest.vbs,SimpleTest.vbs)。从第二个最重要的信息:

' The argument list is exactly the same as the utility programs 
' as a list of strings. In fact you should just be able to 
' copy and past - do simple editing and it will work. See the 
' other samples for more elaborate command sequences and the 
' documentation for the utility programs for more details. 
' 
msgs = img.Convert("logo:","-format","%m,%h,%w","info:") 

因此,传递一个数组中的所有参数是错误的尝试设置属性。我用

Dim sSrcFSpec : sSrcFSpec = "..\data\logo.jpg" 
    Dim sDstFSpec : sDstFSpec = "..\data\logo.png" 
    If goFS.FileExists(sDstFSpec) Then goFS.DeleteFile sDstFSpec 
    CreateObject("ImageMagickObject.MagickImage.1").convert "-verbose", sSrcFSpec, sDstFSpec 
    If Not goFS.FileExists(sDstFSpec) Then WScript.Echo "Failure!" 

输出:

..\data\logo.jpg JPEG 123x118 123x118+0+0 8-bit DirectClass 16.2KB 0.000u 0:00.006 
..\data\logo.jpg=>..\data\logo.png JPEG 123x118 123x118+0+0 8-bit DirectClass 0.030u 0:00. 
083 

这证明,如果你通过合理的参数,你会得到结果。我想类似的测试代码的东西 -

convert -verbose -size 100x100 -font arial -label blah ..\data\example.gif 

,并得到:

convert.exe: missing an image filename `..\data\example.gif' @ error/convert.c/ConvertImag 
eCommand/3016. 

知道任何关于ImageMagick的,我只能假设,这种假设ARGS现有的输入文件。

因此,当您从命令行验证参数时,请进行COM测试。

+0

我已经从命令行中确认了参数,正如我的问题中的第一个代码示例所解释的那样。如果我复制相同的参数到我的VBScript作为一个字符串(而不是数组),它不起作用,似乎需要一个输入图像。如果我创建一个输入图像并将其添加为参数,则'label:blah'不会应用于输出图像。这是我的问题。 – Brad 2012-01-13 22:58:54