我有一个如下所示的批处理文件:
@echo off
REM Create the folder to put the converted files in
md PngConverted
REM Iterate all bitmap files
for /r %%F in (*.bmp) do (
REM Convert the files to PNG. Resize them so they are no bigger than 1300x1300
REM Also limit to 250 colors.
convert -resize 1300x1300 -colors 250 "%%~nF%%~xF" ".\PngConverted\%%~nF.png"
REM Output to the user that we completed this file.
echo converted %%~nF%%~xF to png
)
它使用ImageMagick转换并调整大量图像的大小。
如果我只是双击它,它的效果很好。运行没有错误并输出我转换后的图像。
但是,我试图把它放到这样的控制台应用程序中:
static void Main(string[] args)
{
// Run the batch file that will convert the files.
ExecuteCommand("ConvertImagesInCurrentFolder.bat");
// Pause so the user can see what happened.
Console.ReadLine();
}
static void ExecuteCommand(string command)
{
int exitCode;
ProcessStartInfo processInfo;
Process process;
processInfo = new ProcessStartInfo("cmd.exe", "/c " + command);
processInfo.CreateNoWindow = true;
processInfo.UseShellExecute = false;
// *** Redirect the output ***
processInfo.RedirectStandardError = true;
processInfo.RedirectStandardOutput = true;
process = Process.Start(processInfo);
process.WaitForExit();
// *** Read the streams ***
string output = process.StandardOutput.ReadToEnd();
string error = process.StandardError.ReadToEnd();
exitCode = process.ExitCode;
Console.WriteLine("output>>" + (String.IsNullOrEmpty(output) ? "(none)" : output));
Console.WriteLine("error>>" + (String.IsNullOrEmpty(error) ? "(none)" : error));
Console.WriteLine("ExitCode: " + exitCode.ToString(), "ExecuteCommand");
process.Close();
}
我收到错误:
无效参数 - 1300x1300
为什么从我的控制台应用程序运行时会出现此错误,但是当我从命令行运行时却没有?
答案 0 :(得分:2)
还有一个名为convert
的Windows CMD命令。确保ImageMagick通过环境变量PATH可以访问您的应用程序,或者直接在批处理文件中指定ImageMagick / convert可执行文件路径。
所以总之我怀疑你的应用程序正在调用CMD convert
(转换文件系统)而不是ImageMagick / convert。