C#修改SVG图片显示大小

        private void Main()
        {
            if (File.Exists(_svgFile))
            {
                SetSvgSize(_svgFile, spnWidth.Value, spnHeight.Value);
            }
        }

        private void SetSvgSize(string file,decimal width,decimal height)
        {
            XmlDocument xml = new XmlDocument();
            xml.Load(file);
            var node = xml.DocumentElement;
            UpdateAttribute(node, "width", width.ToString());
            UpdateAttribute(node, "height", height.ToString());
            xml.Save(file);
        }

        private void UpdateAttribute(XmlNode node,string attrName,string value)
        {
            var atts = node.Attributes;
            for (int i = 0; i < atts.Count; i++)
            {
                if (atts[i].Name.Equals(attrName,StringComparison.CurrentCultureIgnoreCase))
                {
                    atts[i].Value = value;
                    return;
                }
            }
            XmlAttribute tmp = node.OwnerDocument.CreateAttribute(attrName);
            tmp.Value = value;
            node.Attributes.Append(tmp);
        }

 

posted @ 2021-02-28 23:31  devs  阅读(382)  评论(0编辑  收藏  举报