启动ImageMagick Exe进程并调整图像大小



我在命令行中使用ImageMagick convert.exe(重新调整图像大小)。效果很好。但是如果我在c#中做同样的事情,那么它就不起作用了。它没有显示任何错误,所有行都运行良好。StanderdErrorOutput也是空的。任何想法?这是我的代码。

var myProcess = new Process();
myProcess.StartInfo.FileName = @"C:UsersuserDesktopImageMagick-6.8.6-Q16convert.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();

这是我用来运行进程的,除了调用standardror。readtoend()的那行,它基本上和你在那里看到的一样

        // create process start info
        ProcessStartInfo startInfo = new ProcessStartInfo(fileName, arguments);
        startInfo.UseShellExecute = false;
        startInfo.CreateNoWindow = true;
        startInfo.RedirectStandardOutput = true;
        startInfo.RedirectStandardError = true;
        // run the process
        using (Process proc = System.Diagnostics.Process.Start(startInfo))
        {
            // This needs to be before WaitForExit() to prevent deadlocks, for details: 
            // http://msdn.microsoft.com/en-us/library/system.diagnostics.process.standardoutput%28v=VS.80%29.aspx
            proc.StandardError.ReadToEnd();
            // Wait for exit
            proc.WaitForExit();
        }

我通过创建一个临时批处理文件

找到了解决方案
    static void Main(string[] args)
    {
        var guid = Guid.NewGuid().ToString();
        var root = AppDomain.CurrentDomain.BaseDirectory;
        var batchFilePath = root + guid + ".bat";
        var cmd = @"cd C:UsersuserDesktopImageMagick-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()。

我发现通过添加

调试所有的

选项导致它工作。

我不知道为什么!

最新更新