如何通过SVG C#库扩展SVG节点

问题描述 投票:0回答:1

我正在尝试将SVG缩放为PNG,以便将其以不同的“缩放”级别调用时,图像不会被像素化。我正在尝试使用此库https://github.com/vvvv/SVG,但由于没有文档,我尝试使用使用常规xpath选择的HTMLAgilityPack对其进行补充,然后将转换样式添加到节点本身。似乎SVG C#库在处理阅读,绘图和保存文件时并未考虑缩放比例,因此画布无法正确缩放。

除了打开和保存文件之类的基本操作外,我不知道如何真正使用SVG C#。对于如何选择节点或应用转换,我无能为力。有人可以协助吗?

        public void svgToPng(string svgFileContents,string fileName)
    {
            var svgDocument = SvgDocument.Open(svgFileContents);
            string workingPath = svgFileContents.Replace(".svg", "_working.svg");
            if (System.IO.File.Exists(workingPath))
            {
                System.IO.File.Delete(workingPath);
            }
            var workingSvgDocument = SvgDocument.Open(svgFileContents).GetXML();
            HtmlDocument theDocument = new HtmlDocument();
            theDocument.LoadHtml(workingSvgDocument);
            HtmlNodeCollection theNodes = theDocument.DocumentNode.SelectNodes("//svg");
            foreach (var node in theNodes)
            {

                decimal decimalWidth = Convert.ToDecimal(sizeOfSprites - (sizeOfSprites*.1));
                int oldWidth = Int32.Parse(node.GetAttributeValue("width",null).Replace("px",""));
                int oldHeight = Int32.Parse(node.GetAttributeValue("height",null).Replace("px",""));
                decimal percentChange = decimalWidth / oldWidth;
                int newWidth = Convert.ToInt32(decimalWidth);
                int newHeight = Convert.ToInt32(oldHeight * percentChange);
                node.SetAttributeValue("width", newWidth.ToString() + "px");
                node.SetAttributeValue("height", newHeight.ToString() + "px");
                node.SetAttributeValue("viewbox", "0 0 " + newWidth.ToString() + " " + newHeight.ToString());
                node.SetAttributeValue("enable-background", "new 0 0 " + newWidth.ToString() + " " + newHeight.ToString());
                node.SetAttributeValue("preserveAspectRatio", "xMinyMin meet");
                //node.SelectSingleNode("g").SetAttributeValue("style", "fill:purple;transform:scale("+Convert.ToDouble(percentChange)+")");
                HtmlNode svgRoot = node.ParentNode;
                HtmlNode newSvgParent = theDocument.CreateElement("svg");
                newSvgParent.SetAttributeValue("baseprofile", "full");
                newSvgParent.SetAttributeValue("width", "100%");
                newSvgParent.SetAttributeValue("height", "100%");
                newSvgParent.SetAttributeValue("xmlns", "http://www.w3.org/2000/svg");
                newSvgParent.SetAttributeValue("xmlns:xlink", "http://www.w3.org/1999/xlink");
                newSvgParent.SetAttributeValue("xmlns:ev", "http://www.w3.org/2001/xml-events");
                newSvgParent.AppendChild(node);
                svgRoot.ReplaceChild(newSvgParent, node);
                node.Attributes.Remove("xmlns");
                node.Attributes.Remove("xmlns:xlink");
                break;
            }
            theDocument.Save(workingPath);
            var output = SvgDocument.Open(workingPath);
            //output = SvgDocument.Open.Nodes.Select(Func < noclue, nodocumentation > stuck).Transform.Scale.?
            if (System.IO.File.Exists(svgFileContents.Replace(".svg",".png")))
            {
                System.IO.File.Delete(svgFileContents.Replace(".svg",".png"));
            }
            var bitmap = output.Draw();
            bitmap.Save(svgFileContents.Replace(".svg",".png"), System.Drawing.Imaging.ImageFormat.Png);
    }
c# xpath svg png transformation
1个回答
0
投票

简单的方法是:

var svgDocument = SvgDocument.Open(f);
using (var bitmap = svgDocument.Draw(500,0))
{                      
   bitmap.Save(output500pixWide);
}

绘制需要一定的宽度和高度。如果宽度或高度之一为0,则输出将使用宽高比进行缩放。

© www.soinside.com 2019 - 2024. All rights reserved.