【问题标题】:Calling ImageMagick COM from VBScript从 VBScript 调用 ImageMagick COM
【发布时间】:2012-01-14 08:55:55
【问题描述】:

我已经成功使用以下命令行创建了一个带有一些简单文本的新 GIF 图片

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

我想使用 VBScript 的 COM 接口来实现相同的结果,但没有成功传入相同的参数。这是我的代码...

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

我得到的输出是这样的,并且没有创建任何 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")

【问题讨论】:

    标签: vbscript imagemagick


    【解决方案1】:

    最后,我放弃了在使用 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")
    

    【讨论】:

      【解决方案2】:

      在文件夹[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 测试。

      【讨论】:

      • 我已经从命令行确认了参数,正如我的问题中的第一个代码示例所解释的那样。如果我将相同的参数作为字符串(而不是数组)复制到我的 vbscript 中,它将不起作用并且似乎需要输入图像。如果我创建输入图像并将其添加为参数,则 label:blah 不会应用于输出图像。这是我的问题。
      【解决方案3】:

      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

      【讨论】:

      • 您知道TLViewer 是否可以在Windows 7 上运行吗?我没有 Visual Studio,也无法使用 regsvr32 注册 Tlbinf32.dll。它抱怨文件丢失(它没有)或者它有其他依赖的DLL。我找不到任何文档告诉我任何相关的 DLL。 BNut 一个提到 Windows 7 的寻呼机将不支持 Tlbinf32.dll
      • 我似乎记得让它在 Windows 7 上以兼容模式运行。
      猜你喜欢
      • 1970-01-01
      • 2011-06-20
      • 2011-08-04
      • 2012-04-08
      • 2011-09-25
      • 2012-09-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多