简体   繁体   English

错误“无效参数”fom ImageMagick在Windows上转换

[英]Error “Invalid Parameter” fom ImageMagick convert on Windows

I am trying to convert a PDF document into a PNG file using ImageMagick command line tools from a ASP.NET website. 我正在尝试使用ASP.NET网站上的ImageMagick命令行工具将PDF文档转换为PNG文件。 I create a new shell process and ahve it execute the following command: 我创建一个新的shell进程,并执行以下命令:

convert -density 96x96 "[FileNameAndPath].pdf" "[FileNameAndPath].png"

This runs well when testing the website on my local machine with the ASP.NET Develeopment Server of VS and the command also works well when manually entered into the shell. 当使用VS的ASP.NET开发服务器在本地计算机上测试网站时,这很好用,并且在手动输入shell时该命令也可以正常工作。 When running from the programatically created shell in ASP.NET there is the following error message: 从ASP.NET中以编程方式创建的shell运行时,会出现以下错误消息:

Invalid Parameter - 96x96

Does anybody know why that happens and what to do? 有谁知道为什么会这样,怎么办?

I have tested the command while being logged in on the server via RDP with a different user account than the ASP.NET process. 我在使用与ASP.NET进程不同的用户帐户通过RDP登录服务器时测试了该命令。 I have used exactly the same ImageMagick and Ghostscript installation files as on my local machine and have activated adding the ImageMagick installation path to the enironment variables during installing. 我使用了与本地计算机完全相同的ImageMagick和Ghostscript安装文件,并在安装过程中激活了将ImageMagick安装路径添加到enironment变量。 The server has not been rebooted since than. 服务器从未重启过。

convert is also the name of a windows executable which converts FAT filesystem to NTFS. convert也是将FAT文件系统转换为NTFS的Windows可执行文件的名称。 When you do not specify the full path of an executable, quote: 如果未指定可执行文件的完整路径,请引用:

...the system first searches the current working directory and then searches the path environment variable , examining each directory from left to right, looking for an executable filename that matches the command name given. ...系统首先搜索当前工作目录,然后搜索路径环境变量 ,从左到右检查每个目录,查找与给定命令名称匹配的可执行文件名。

"C:\\Windows\\System32" is generally present in the beginning of %PATH% variable, causing the Windows convert utility to launch, which fails with "Invalid Parameter" error as expected. “C:\\ Windows \\ System32”通常出现在%PATH%变量的开头,导致Windows转换实用程序启动,失败并出现“无效参数”错误。

Try specifying the full path of the ImageMagick's convert.exe like so: 尝试指定ImageMagick的convert.exe的完整路径,如下所示:

"C:\Program Files\ImageMagick\convert.exe" -density 96x96 "path_and_filename.pdf" "path_and_filename.png"

As others have stated convert points to a different program in your PATH. 正如其他人已经说明convertconvert为PATH中的其他程序。 Instead preface your command with magick . 而是用magick你的命令作序。 So your command would instead be: 所以你的命令将改为:

magick convert -density 96x96 "[FileNameAndPath].pdf" "[FileNameAndPath].png"

在Window中,system32中实际存在“convert.exe” - 确保您的脚本没有启动那个(可能是开发机器上的环境路径设置不同)。

A couple more options for fixing this: 还有几个解决这个问题的方法:

  • Edit your Path system variable to contain the path to imagemagick as it's first content and then add the rest after it. 编辑Path系统变量以包含imagemagick的路径,因为它是第一个内容,然后在其后添加其余内容。 This will make windows always find the imagemagic convert first before it finds the other convert program. 这将使Windows在找到其他转换程序之前始终首先找到imagemagic convert So something like this: C:\\Program Files\\ImageMagick-6.9.2-Q16;C:\\Program Files\\Haskell Platform\\2014.2.0.0\\lib\\extralibs\\bin;... 所以像这样: C:\\Program Files\\ImageMagick-6.9.2-Q16;C:\\Program Files\\Haskell Platform\\2014.2.0.0\\lib\\extralibs\\bin;...

  • Another option is to create a dedicated folder somewhere on your machine where you will place shortcuts for some of these name clashes. 另一个选择是在您的计算机上的某个位置创建一个专用文件夹,您可以在其中放置一些名称冲突的快捷方式。 Then what you do is that you rename those shortcuts to meaningful names, for example convert_image_magick , then add the path to this folder to your system path. 然后,您要将这些快捷方式重命名为有意义的名称,例如convert_image_magick ,然后将此文件夹的路径添加到系统路径中。 So now as you hit tab more, you will finally find the right program you want to run 所以现在当你点击更多标签时,你最终会找到你想要运行的正确程序

yes! 是! if you launch an Administrator command window it defaults to C:\\windows\\sytem32\\ ... as long as you're not in that directory the command will pickup the ImageMagick convert.exe 如果你启动一个管理员命令窗口,它默认为C:\\ windows \\ sytem32 \\ ...只要你不在该目录中该命令将拾取ImageMagick convert.exe

My issue was I was using the "FORFILES" command which is tricky because it requires using "cmd /c" and passing the convert command with @path and @file parameters and it does some escaping of slashes... needless to say it's caused me hours and hours of headache. 我的问题是我使用的是“FORFILES”命令,这很棘手,因为它需要使用“cmd / c”并使用@path和@file参数传递convert命令,它会做一些斜线转义...不用说它是由于它造成的我头疼几个小时。 It even parses hex characters, like if your filepath has the combination 0x00 in it, it will think that's a hex value and mangle your path. 它甚至会解析十六进制字符,就好像你的文件路径中包含组合0x00一样,它会认为这是一个十六进制值并且会破坏你的路径。 I had a filepath named C:\\ImageRes3000x3000 and FORFILES interprets that literally and it caused a strange path issue. 我有一个名为C:\\ImageRes3000x3000的文件路径和FORFILES字面解释,它引起了一个奇怪的路径问题。 Sorry if this is a long useless post but it's meant to be FYI, if someone runs across this, maybe it will help them. 对不起,如果这是一个长期无用的帖子,但它的意思是FYI,如果有人遇到这个,也许它会帮助他们。 That being said, FORFILES and "convert.exe" are a powerful and simple image renaming line script combo. 话虽这么说,FORFILES和“convert.exe”是一个功能强大且简单的图像重命名行脚本组合。

here's my full 3 line image renaming script 这是我的完整3行图像重命名脚本

robocopy D:\SRC_DIR\ D:\DEST_DIR\_staging *.jpg /e /MAXAGE:2
FORFILES /P D:\DEST_DIR\_staging\ /S /M *.jpg /C "cmd /c convert.exe @path -quality 65 -resize 1500 D:\RESIZED_DIR\\@file"
DEL D:\DEST_DIR\_staging\*.* /S /Q

I am only answering this late because imagemagick was updated. 我只是回答这个问题因为imagemagick已经更新了。 Now, if you wish to use the "convert" command, you do it like this: 现在,如果你想使用“convert”命令,你可以这样做:

magick convert "image.png" "document.pdf"

or 要么

magick convert "image_00*.png" "document.pdf"

for multiple images. 用于多个图像。

Same syntax for command, just add magick before it 命令的语法相同,只需在它之前添加magick

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM