If you use the C# Process class as shown in the code below, with process.WaitForExit() called after each process is started, then you can run the two commands separately as shown. I think the correct calls are in fact magick convert image.png image.pnm
and then potrace image.pnm -b svg
. This works for me and is what the code is doing.
The biggest problem here was getting C# to find the files it needs. To get this to work I put potrace in a subfolder of the project and copied it to the output directory, so you don't need to install it. I couldn't get this to work for ImageMagick. It appears to need to be installed to work, so in the end I just installed it. The files to be converted are similarly in a Files subfolder of the project with 'Copy to Output Directory' set on their project properties.
As some of the commenters have suggested it may be better to try one of the libraries that can be called directly from C# rather than starting separate processes, which are clearly more inefficient. However, my experience is that often this sort of wrapper project is not well-maintained versus the main project, so if the solution below works it may be fine.
I can upload the full project to GitHub if that would be helpful.
using System.Diagnostics;
using System.Reflection;
#nullable disable
namespace ImageMagicPotrace
{
internal class Program
{
static void Main(string[] args)
{
string path = Path.GetDirectoryName(Assembly.GetAssembly(typeof(Program)).Location);
string filesPath = Path.Combine(path, "Files");
string fileName = "image";
string imageMagick = "magick.exe";
string arg = $"convert \"{Path.Combine(filesPath, fileName + ".png")}\" \"{Path.Combine(filesPath, fileName + ".pnm")}\"";
RunProcess(imageMagick, arg);
string potrace = Path.Combine(path, @"potrace\potrace.exe");
string arg2 = $"\"{Path.Combine(filesPath, fileName + ".pnm")}\" -b svg";
RunProcess(potrace, arg2);
Console.WriteLine("Done");
}
private static void RunProcess(string executable, string arg)
{
Console.WriteLine(executable);
Console.WriteLine(arg);
ProcessStartInfo start = new ProcessStartInfo(executable)
{
WindowStyle = ProcessWindowStyle.Hidden,
Arguments = arg,
UseShellExecute = false,
CreateNoWindow = true
};
Process process = Process.Start(start);
process.WaitForExit();
}
}
}