gpt4 book ai didi

c# - 将 Canvas 保存为 SVG

转载 作者:行者123 更新时间:2023-11-30 21:50:53 25 4
gpt4 key购买 nike

给大家的一个简单问题(希望如此)。我用 C# 做了一个 Canvas ,这个 Canvas 里面有形状。现在我想将这个带有形状的 Canvas 保存到 .svg 中以用于其他用途。那么,如何将此 Canvas 保存为 svg 文件?

希望你能帮到你。

最佳答案

您不能直接使用 Microsoft 类执行此操作。你应该为此创建你自己的类。

这只是一个示例,对您没有用,但对您的示例很有用:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Windows;
using System.IO;
using System.Windows.Media;

namespace Render
{
public class SVGRenderer : IRenderContext
{
public bool Absolute { get { return true; } }
public double Width { get; private set; }
public double Height { get; private set; }

public MemoryStream SVGDocument { get; private set; }
private XmlTextWriter Writer { get { return m_writer; } }

private XmlTextWriter m_writer;

/// <summary>
/// Creates a new SVGRenderer, which will produce an output SVG with the specified width and height.
/// </summary>
/// <param name="width">Width of the output SVG.</param>
/// <param name="height">Height of the output SVG.</param>
public SVGRenderer(double width, double height)
{
SVGDocument = new MemoryStream();
m_writer = new XmlTextWriter(SVGDocument, Encoding.UTF8);
m_writer.Formatting = Formatting.Indented;
this.Width = width;
this.Height = height;
}

public void Begin()
{
string cdlibraryVersion = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString();

Writer.WriteStartDocument();
Writer.WriteComment(" Generator: " + (IO.ApplicationInfo.FullName != null ? IO.ApplicationInfo.FullName : "") + ", cdlibrary.dll " + cdlibraryVersion + " ");
Writer.WriteDocType("svg", "-//W3C//DTD SVG 1.1//EN", "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd", null);
Writer.WriteStartElement("svg", "http://www.w3.org/2000/svg");
Writer.WriteAttributeString("version", "1.1");
Writer.WriteAttributeString("width", this.Width.ToString());
Writer.WriteAttributeString("height", this.Height.ToString());
}

public void End()
{
Writer.WriteEndDocument();
Writer.Flush();
}

public void StartSection(object tag)
{
// Do nothing.
}

public void DrawLine(Point start, Point end, double thickness)
{
m_writer.WriteStartElement("line");
m_writer.WriteAttributeString("x1", start.X.ToString());
m_writer.WriteAttributeString("y1", start.Y.ToString());
m_writer.WriteAttributeString("x2", end.X.ToString());
m_writer.WriteAttributeString("y2", end.Y.ToString());
m_writer.WriteAttributeString("style", "stroke:rgb(0,0,0);stroke-linecap:square;stroke-width:" + thickness.ToString());
m_writer.WriteEndElement();
}

public void DrawRectangle(Point start, Size size, double thickness, bool fill = false)
{
m_writer.WriteStartElement("rect");
m_writer.WriteAttributeString("x", start.X.ToString());
m_writer.WriteAttributeString("y", start.Y.ToString());
m_writer.WriteAttributeString("width", size.Width.ToString());
m_writer.WriteAttributeString("height", size.Height.ToString());
m_writer.WriteAttributeString("style", "fill-opacity:0;stroke:rgb(0,0,0);stroke-width:" + thickness.ToString());
m_writer.WriteEndElement();
}

public void DrawEllipse(Point centre, double radiusX, double radiusY, double thickness, bool fill = false)
{
string fillOpacity = ((fill ? 255f : 0f) / 255f).ToString();

m_writer.WriteStartElement("ellipse");
m_writer.WriteAttributeString("cx", centre.X.ToString());
m_writer.WriteAttributeString("cy", centre.Y.ToString());
m_writer.WriteAttributeString("rx", radiusX.ToString());
m_writer.WriteAttributeString("ry", radiusY.ToString());
m_writer.WriteAttributeString("style", String.Format("fill-opacity:" + fillOpacity + ";fill:rgb({0},{1},{2});stroke:rgb(0,0,0);stroke-width:" + thickness.ToString(), 0, 0, 0));
m_writer.WriteEndElement();
}

public void DrawPath(Point start, IList<IPathCommand> commands, double thickness, bool fill = false)
{
string data = "M " + start.X.ToString() + "," + start.Y.ToString();
Point last = new Point(0, 0);
foreach (IPathCommand pathCommand in commands)
{
data += " " + pathCommand.Shorthand(start, last);
last = new Point(pathCommand.End.X, pathCommand.End.Y);
}

string fillOpacity = ((fill ? 255f : 0f) / 255f).ToString();

m_writer.WriteStartElement("path");
m_writer.WriteAttributeString("d", data);
m_writer.WriteAttributeString("style", "fill-opacity:" + fillOpacity + ";fill:rgb(0,0,0);stroke:rgb(0,0,0);stroke-width:" + thickness.ToString());
m_writer.WriteEndElement();
}

public void DrawText(Point anchor, TextAlignment alignment, IEnumerable<TextRun> textRuns, Brush bru = null)
{
m_writer.WriteStartElement("text");
m_writer.WriteAttributeString("x", anchor.X.ToString());
m_writer.WriteAttributeString("y", anchor.Y.ToString());

string textAnchor = "start";
if (alignment == TextAlignment.BottomCentre || alignment == TextAlignment.CentreCentre || alignment == Render.TextAlignment.TopCentre)
textAnchor = "middle";
else if (alignment == TextAlignment.BottomRight || alignment == TextAlignment.CentreRight || alignment == Render.TextAlignment.TopRight)
textAnchor = "end";

string dy = "-0.3em";
if (alignment == TextAlignment.CentreCentre || alignment == TextAlignment.CentreLeft || alignment == TextAlignment.CentreRight)
dy = ".3em";
else if (alignment == TextAlignment.TopCentre || alignment == TextAlignment.TopLeft || alignment == TextAlignment.TopRight)
dy = "1em";

m_writer.WriteAttributeString("style", "font-family:Arial;font-size:" + textRuns.FirstOrDefault().Formatting.Size.ToString() + ";text-anchor:" + textAnchor);
m_writer.WriteAttributeString("dy", dy);

foreach (TextRun run in textRuns)
{
if (run.Formatting.FormattingType != TextRunFormattingType.Normal)
m_writer.WriteStartElement("tspan");
if (run.Formatting.FormattingType == TextRunFormattingType.Subscript)
{
m_writer.WriteAttributeString("baseline-shift", "sub");
m_writer.WriteAttributeString("style", "font-size:0.8em");
}
else if (run.Formatting.FormattingType == TextRunFormattingType.Superscript)
{
m_writer.WriteAttributeString("baseline-shift", "super");
m_writer.WriteAttributeString("style", "font-size:0.8em");
}
m_writer.WriteString(run.Text);
if (run.Formatting.FormattingType != TextRunFormattingType.Normal)
m_writer.WriteEndElement();
}

m_writer.WriteEndElement();
}
}
}

希望对你有帮助

关于c# - 将 Canvas 保存为 SVG,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36055001/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com