C# 启动ImageMagick Exe进程并调整图像大小
我正在命令行中使用ImageMagick convert.exe(重新调整图像大小)。它工作得很好。但如果我在C#中也这样做,那么它就不起作用了。它没有显示任何错误,所有的行都运行正常。C# 启动ImageMagick Exe进程并调整图像大小,c#,imagemagick-convert,C#,Imagemagick Convert,我正在命令行中使用ImageMagick convert.exe(重新调整图像大小)。它工作得很好。但如果我在C#中也这样做,那么它就不起作用了。它没有显示任何错误,所有的行都运行正常。standerErrorOutput也为空。有什么想法吗?这是我的密码 var myProcess = new Process(); myProcess.StartInfo.FileName = @"C:\Users\user\Desktop\ImageMagick-6.8.6-Q16\convert.exe";
standerErrorOutput
也为空。有什么想法吗?这是我的密码
var myProcess = new Process();
myProcess.StartInfo.FileName = @"C:\Users\user\Desktop\ImageMagick-6.8.6-Q16\convert.exe";
myProcess.StartInfo.Arguments = @"icon.png -resize 64x64 icon1.png";
myProcess.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
myProcess.StartInfo.CreateNoWindow = true;
myProcess.Start();
myProcess.WaitForExit();
Console.Read();
这是我用来运行进程的,除了调用StandardError.ReadToEnd()的那一行之外,它基本上与这里的相同
我通过创建一个临时批处理文件找到了解决方案
static void Main(string[] args)
{
var guid = Guid.NewGuid().ToString();
var root = AppDomain.CurrentDomain.BaseDirectory;
var batchFilePath = root + guid + ".bat";
var cmd = @"cd C:\Users\user\Desktop\ImageMagick-6.8.6-Q16" + Environment.NewLine
+ "convert icon.png -resize 64x64 icon1.png";
CreateBatchFile(cmd, batchFilePath);// Temporary Batch file
RunBatchFile(batchFilePath);
DeleteBatchFile(batchFilePath);
}
private static void RunBatchFile(string batFilePath)
{
var myProcess = new Process();
myProcess.StartInfo.FileName = batFilePath;
myProcess.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
myProcess.StartInfo.CreateNoWindow = true;
myProcess.Start();
myProcess.WaitForExit();
}
private static void DeleteBatchFile(string file)
{
File.Delete(file);
}
private static void CreateBatchFile(string input, string filePath)
{
FileStream fs = new FileStream(filePath, FileMode.Create);
StreamWriter writer = new StreamWriter(fs);
writer.WriteLine(input);
writer.Close();
fs.Close();
}
我在Mac上尝试运行Convert时遇到了类似的问题,即使使用了另一张海报建议的ReadToEnd() 我通过添加 -调试“全部” 选项使它工作 我不知道为什么
static void Main(string[] args)
{
var guid = Guid.NewGuid().ToString();
var root = AppDomain.CurrentDomain.BaseDirectory;
var batchFilePath = root + guid + ".bat";
var cmd = @"cd C:\Users\user\Desktop\ImageMagick-6.8.6-Q16" + Environment.NewLine
+ "convert icon.png -resize 64x64 icon1.png";
CreateBatchFile(cmd, batchFilePath);// Temporary Batch file
RunBatchFile(batchFilePath);
DeleteBatchFile(batchFilePath);
}
private static void RunBatchFile(string batFilePath)
{
var myProcess = new Process();
myProcess.StartInfo.FileName = batFilePath;
myProcess.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
myProcess.StartInfo.CreateNoWindow = true;
myProcess.Start();
myProcess.WaitForExit();
}
private static void DeleteBatchFile(string file)
{
File.Delete(file);
}
private static void CreateBatchFile(string input, string filePath)
{
FileStream fs = new FileStream(filePath, FileMode.Create);
StreamWriter writer = new StreamWriter(fs);
writer.WriteLine(input);
writer.Close();
fs.Close();
}