Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/338.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 启动ImageMagick Exe进程并调整图像大小_C#_Imagemagick Convert - Fatal编程技术网

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

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";

我正在命令行中使用ImageMagick convert.exe(重新调整图像大小)。它工作得很好。但如果我在C#中也这样做,那么它就不起作用了。它没有显示任何错误,所有的行都运行正常。
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();
    }