0

I am using ImageMagick C# tool to convert PDF to JPG by calling the executable from C#. I believe I set up the command correctly but it does not execute; it just passes through Process.Start(startInfo) without executing it. I do see the command prompt popping up but nothing happens.

string PNGPath = Path.ChangeExtension(Loan_list[f], ".png");
string PDFfile = '"' + Loan_list[f] + '"';
string PNGfile = '"' + PNGPath + '"';
string arguments = string.Format("{0} {1}", PDFfile, PNGfile);
ProcessStartInfo startInfo = new ProcessStartInfo(@"C:\Program Files\ImageMagick-6.9.2-Q16\convert.exe");
startInfo.Arguments = arguments;
Process.Start(startInfo);

I wasn't sure if it was because of the double quotes I added to each argument before hand but after commenting it out and running it again, it still skipped over. Any thoughts?

Edit: To add some clarity, I am expecting a JPG files from a PDF but I see no output file from this part of code. I ran the following in my command prompt to convert PDF to JPG

"C:\Program Files\ImageMagick-6.9.2-Q16\convert.exe" "C:\Users\rwong\Desktop\RoundPoint\1000965275\1000965275_157_Credit File_10.PDF" "C:\Users\rwong\Desktop\RoundPoint\1000965275\1000965275_157_Credit File_10.png"

I explicitly called the convert.exe for clarity sake in my code. The command works fine in command prompt but when coping the structure over to C# it doesn't do anything. I see the code step into it but it continues without an error.

Edit2: Upon request below is the code and output for a Process Exit code

string PNGPath = Path.ChangeExtension(Loan_list[f], ".png");
string PDFfile = '"' + Loan_list[f] + '"';
string PNGfile = '"' + PNGPath + '"';
try
{
    Process myprocess = null;
    string[] arguments = { PDFfile, PNGfile };
    myprocess=Process.Start(@"C:\ProgramFiles\ImageMagick6.9.2Q16\convert.exe", String.Join(" ", arguments));
    Console.WriteLine("Process exit code: {0}", myprocess.ExitCode);
}
catch (Exception ex)
{
    Console.WriteLine(ex);
}

Process exit code: 1

LampPost
  • 856
  • 11
  • 30
  • Are you running the program in your user context, as another user or in elevated context? – StingyJack Aug 21 '15 at 18:28
  • What do you mean user context and elevated user? – LampPost Aug 21 '15 at 18:31
  • Are you running it (or Visual Studio) "as administrator"? – StingyJack Aug 21 '15 at 18:55
  • try changing your process to `cmd.exe` and the arguments to `/c "C:\Program Files\ImageMagick-6.9.2-Q16\convert.exe" "C:\Users\rwong\Desktop\RoundPoint\1000965275\1000965275_157_Credit File_10.PDF" "C:\Users\rwong\Desktop\RoundPoint\1000965275\1000965275_157_Credit File_10.png"`. That will run the a new command window that will stay open so you can see the output of ImageMagick – shf301 Aug 21 '15 at 19:01
  • @StingyJack not sure but I will check – LampPost Aug 21 '15 at 21:58
  • 1
    Hope the [same answer](http://stackoverflow.com/a/32174035/3796048) can help you. – Mohit S Aug 24 '15 at 03:25

1 Answers1

1

Assuming that you are right and there was a problem (rather than the process just executed very quickly and exited), you can check the return code as follows:

if (Process.Start(startInfo) == null)
{
    int lastError = Marshal.GetLastWin32Error();
}

You then go here to look up the error code:

https://msdn.microsoft.com/en-us/library/windows/desktop/ms681381(v=vs.85).aspx

Hopefully, that vendor actually sets an error code on failure (they may or may not).

Jeff Prince
  • 658
  • 6
  • 13
  • Jeff, thanks for your input but the code does not go into that condition statement. I also edited the post to give more clarity of my problem. – LampPost Aug 21 '15 at 18:40
  • Process.Start does not set the last error, so calling Marshal.GetLastWin32Error() will give you nothing useful. If Process.Start() fails it will throw a Win32Exception. Marshal.GetLastWin32Error() can only be used with p/Invoke calls. – shf301 Aug 21 '15 at 18:47
  • The correct was to check a process's exist code is to call .ExitCode on the Process object that Process.Start returns. – shf301 Aug 21 '15 at 18:48
  • Yes, you are right. I had forgotten that. But the Win32Exception WILL contain the error code, right? – Jeff Prince Aug 21 '15 at 18:49
  • Yes it will. It has a NativeErrorCode property – shf301 Aug 21 '15 at 18:51
  • @shf301, You know, in theory, that is true, but the reality is that there are several types of exceptions that can be thrown by Process.Start for simple API call problems, so it would be unwise not to have a catch block to catch those exceptions. – Jeff Prince Aug 21 '15 at 18:59
  • Is there anything else I can provide? – LampPost Aug 21 '15 at 19:44
  • @Robert Wong, Have you run it with a try/catch around it to see it is throws Win32Exception.. If so, what was the error code. If not, what was the process ExitCode? – Jeff Prince Aug 21 '15 at 19:47
  • There was no error to be caught and what do you mean by ExitCode? – LampPost Aug 21 '15 at 19:50
  • The Process has a property named ExitCode. – Jeff Prince Aug 21 '15 at 19:57
  • By the way, you should wait for the process to exit before checking ExitCode. You can do this like this: – Jeff Prince Aug 21 '15 at 20:00
  • Oops, sorry, hit ENTER by accident using (Process exeProcess = Process.Start(startInfo)) { exeProcess.WaitForExit(); } – Jeff Prince Aug 21 '15 at 20:01
  • Then, check what is in exeProcess.ExitCode. – Jeff Prince Aug 21 '15 at 20:01
  • It returned a 1. I provided my steps above. Based on the process exit error chart it looks like a vauge error. – LampPost Aug 21 '15 at 20:21
  • Yes, I was afraid that would happen. Usually 1 just means that there was an error and 0 means there wasn't. I think you should back-track and take that other poster's suggestion that you change you command and arguments to invoke CMD.EXE so that you can see the output from the image program. – Jeff Prince Aug 21 '15 at 21:08
  • Hey, by the way, you need to call WaitForExit() before you check the ExitCode. I think in your case the program terminated immediately, but for the standard scenario the program might take some time to complete, and it is only then that ExitCode should be checked. – Jeff Prince Aug 21 '15 at 21:13