简体   繁体   English

从C#代码执行Potrace命令以将位图图像转换为XAML

[英]Execute Potrace command from C# code to convert bitmap image to XAML

I am trying to convert my bitmap image to XAML format. 我正在尝试将位图图像转换为XAML格式。 Currently I am using Potrace in command Prompt. 目前,我在命令提示符中使用Potrace。 I will be passing the input file through command prompt only. 我将仅通过命令提示符传递输入文件。 My problem is that I have to do the same using C# code. 我的问题是我必须使用C#代码执行相同的操作。 I have to call the Potrace process externally and execute it. 我必须从外部调用Potrace进程并执行它。 I tried the following code 我尝试了以下代码

               Process potrace = new Process
           {
               StartInfo = new ProcessStartInfo
               {
                   FileName = "potrace.exe",
                   Arguments = "-s -u 1", //SVG
                   RedirectStandardInput = true,
                   RedirectStandardOutput = true,
                   RedirectStandardError = Program.IsDebug,
                   UseShellExecute = false,
                   CreateNoWindow = true,
                   WindowStyle = ProcessWindowStyle.Hidden
               },
               EnableRaisingEvents = false
           };
           StringBuilder svgBuilder = new StringBuilder();
           potrace.OutputDataReceived += (object sender2, DataReceivedEventArgs e2) =>
           {
               svgBuilder.AppendLine(e2.Data);
           };
           if (Program.IsDebug)
           {
               potrace.ErrorDataReceived += (object sender2, DataReceivedEventArgs e2) =>
               {
                   Console.WriteLine("Error: " + e2.Data);
               };
           }
           potrace.Start();
           potrace.BeginOutputReadLine();
           if (Program.IsDebug)
           {
               potrace.BeginErrorReadLine();
           }

           BinaryWriter writer = new BinaryWriter(potrace.StandardInput.BaseStream);

           Bitmap.Save(writer.BaseStream, ImageFormat.Bmp);
           potrace.StandardInput.WriteLine();
           potrace.WaitForExit();

This code gives me an error, the compiler says Program does not exist in this content.! 这段代码给我一个错误,编译器说该内容中不存在Program。 Help me out in solving this problem. 帮助我解决这个问题。 If you have any other suggestions, I'm open to it. 如果您还有其他建议,我愿意接受。

Try this! 尝试这个! Some parts of code that did not work are disabled by comments. 代码的某些无效部分被注释禁用。 This version takes source bmp image from property "Arguments". 此版本从属性“ Arguments”获取源bmp图像。

        Process potrace = new Process
        {
            StartInfo = new ProcessStartInfo
            {
                FileName = @"C:\potrace-1.11.win64\potrace.exe",
                Arguments = "C:\\potrace-1.11.win64\\circle.bmp --backend dxf", //DXF
                RedirectStandardInput = true,
                RedirectStandardOutput = true,
           //     RedirectStandardError = Program.IsDebug,
                UseShellExecute = false,
                CreateNoWindow = true,
                WindowStyle = ProcessWindowStyle.Hidden
            },
            EnableRaisingEvents = false
        };

        StringBuilder svgBuilder = new StringBuilder();
        potrace.OutputDataReceived += (object sender2, DataReceivedEventArgs e2) =>
        {
            svgBuilder.AppendLine(e2.Data);
        };
        /*
        if (Program.IsDebug)
        {
            potrace.ErrorDataReceived += (object sender2, DataReceivedEventArgs e2) =>
            {
                Console.WriteLine("Error: " + e2.Data);
            };
        }
         */

        potrace.Start();
         /*
          * 
        potrace.BeginOutputReadLine();
        if (Program.IsDebug)
        {
            potrace.BeginErrorReadLine();
        }
        */
        /*
        BinaryWriter writer = new BinaryWriter(potrace.StandardInput.BaseStream);
        // Construct a new image from the GIF file.
        Bitmap bmp2 = new Bitmap(@"C:\Users\Matus\Desktop\potrace-1.11.win64\kruz.bmp");
        bmp2.Save(writer.BaseStream, System.Drawing.Imaging.ImageFormat.Bmp);
        potrace.StandardInput.WriteLine(); //Without this line the input to Potrace won't go through.
         * **/
        potrace.WaitForExit();

Just use the C# version of Potrace see here . 只需使用PotraceC#版本,请参见此处

C# version of http://potrace.sourceforge.net/ (Peter Selinger) based on http://www.drawing3d.de/ who did the hard job. 基于http://www.drawing3d.de/的 C#版本的http://potrace.sourceforge.net/ (彼得·塞林格)(Peter Selinger),他做得很努力。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM