Open szpebble opened 4 years ago
自己解决了:
` public class SvgUtils {
public static string AbsPath2SVGText(IEnumerable<IEnumerable
var maxWidth = 0d;
var maxHeight = 0d;
Strokes.ForEach(p => p.ForEach(pt => {
maxWidth = Math.Max(pt.X, maxWidth);
maxHeight = Math.Max(pt.Y, maxHeight);
}));
var doc = new StringBuilder();
doc.Append(@"<?xml version=""1.0"" encoding=""UTF-8"" standalone=""no""?>")
.Append(@"<!DOCTYPE svg PUBLIC ""-//W3C//DTD SVG 1.1//EN"" ""http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd"">")
.Append(@"<svg xmlns=""http://www.w3.org/2000/svg"" version=""1.1"" ")
.Append($@"width=""{Convert.ToInt32(maxWidth)}"" height=""{Convert.ToInt32(maxHeight)}"" >");
foreach (var ps in Strokes) {
doc.Append($"<path stroke-linejoin=\"round\" stroke-linecap=\"round\" stroke-width=\"2\" stroke=\"#0000ff\" fill=\"none\" d=\"");
Point prev = Point.Zero;
for (var i = 0; i < ps.Count(); i++) {
var it = ps.ElementAt(i);
if (i == 0)
doc.AppendFormat("M {0} {1} ", it.X, it.Y);
else {
var x = it.X - prev.X;
var y = it.Y - prev.Y;
doc.AppendFormat("l {0} {1} ", x, y);
}
prev = it;
}
doc.Append("\"/>");
}
doc.Append("</svg>");
return doc.ToString();
//System.IO.File.WriteAllText("d:\\test.svg", doc.ToString(), Encoding.UTF8);
}
public static string AbsPath2SVGDateImage(IEnumerable<IEnumerable<Point>> Strokes) {
var doc = AbsPath2SVGText(Strokes);
if (string.IsNullOrEmpty(doc))
return null;
var b64 = Convert.ToBase64String(Encoding.UTF8.GetBytes(doc));
return $"image/svg+xml;base64,{b64}";
}
}`
Strokes能否转化成SVG?或GetImage获取SVG