[英]Execute Potrace command from C# code to convert bitmap image to XAML
我正在嘗試將位圖圖像轉換為XAML格式。 目前,我在命令提示符中使用Potrace。 我將僅通過命令提示符傳遞輸入文件。 我的問題是我必須使用C#代碼執行相同的操作。 我必須從外部調用Potrace進程並執行它。 我嘗試了以下代碼
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();
這段代碼給我一個錯誤,編譯器說該內容中不存在Program。 幫助我解決這個問題。 如果您還有其他建議,我願意接受。
嘗試這個! 代碼的某些無效部分被注釋禁用。 此版本從屬性“ 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();
只需使用Potrace
的C#
版本,請參見此處 。
基於http://www.drawing3d.de/的 C#版本的http://potrace.sourceforge.net/ (彼得·塞林格)(Peter Selinger),他做得很努力。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.