0

I am using ImageMagick convert.exe(to re-size a image) in command line. It works great. But if I do the same in C# then It doesn't work. It does not show any error and all the lines run just fine. The StanderdErrorOutput is also empty. Any idea? Here is my code.

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();
svick
  • 236,525
  • 50
  • 385
  • 514
Imran Qadir Baksh - Baloch
  • 32,612
  • 68
  • 179
  • 322

3 Answers3

0

This is what I use to run processes, it's basically the same as what you have there except for the line that calls StandardError.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();

        }
Lee Bailey
  • 3,594
  • 2
  • 18
  • 17
0

I found the solution by creating a temporary batch file,

    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();
    }
Imran Qadir Baksh - Baloch
  • 32,612
  • 68
  • 179
  • 322
0

I was having a similar problem on Mac when trying to run Convert, even with the ReadToEnd() suggested by the other poster.

I found that by adding

-debug 'All'

option caused it to work.

I have not ideas why!

Ben
  • 10,931
  • 9
  • 38
  • 47