【问题标题】:How to Scale an SVG Node Via SVG C# Library如何通过 SVG C# 库缩放 SVG 节点
【发布时间】:2016-01-07 22:47:37
【问题描述】:

我试图在将 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);
}

【问题讨论】:

  • 将转换设置为属性而不是样式更有可能起作用。 FWIW xMinyMin 是错误的,它是 xMinYMin
  • 感谢您发现 preserveAspectRatio 字符串存在的问题。我直接通过属性设置转换,这与在样式属性中设置它没有不同的效果。我正在使用此示例:stackoverflow.com/questions/16453446/… 用于使内容与视口左侧对齐,因为我正在尝试标准化不同宽度的 svg 的输出。如果外部 的宽度/高度设置为 100%,则 SVG.net 库似乎将路径的原始大小保留为画布大小。有解决办法吗?

标签: c# xpath svg png transformation


【解决方案1】:

简单的方法是:

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

Draw 需要一个宽度和高度。如果宽度或高度之一为 0,则输出使用纵横比进行缩放。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-02-16
    • 2020-04-08
    • 1970-01-01
    • 2014-05-03
    • 2019-05-28
    • 2021-01-24
    • 1970-01-01
    • 2017-05-08
    相关资源
    最近更新 更多