【问题标题】:transform SVG string to Bitmap ideally in memory in C#在 C# 的内存中理想地将 SVG 字符串转换为位图
【发布时间】:2018-12-23 12:57:00
【问题描述】:

我正在使用下面的代码将 SVG 字符串 (svgString) 转换为位图 (myBitmap)。理想情况下,我想避免往返硬盘驱动器。你认为这是可能的吗(没有找到太多关于 SVG nuget 包的文档)。顺便说一句,我知道我也可以将 bmp 写入硬盘,但我选择了 png,因为由于某种原因,bmp 的背景总是黑色。

using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using Svg;

namespace Bla
{
  class Program
  {
    static void Main(string[] args)
    {
      var svgPath = @"d:\test.svg";
      var pngPath = @"d:\test.png";
      var svgString = @"
      <!DOCTYPE html>
      <html>
      <body>
      <svg height='200' width='500'>
        <polyline points='20,20 40,25 60,40 80,120 120,140 200,180' style='fill:none;stroke:black;stroke-width:3' />
        Sorry, your browser does not support inline SVG.
      </svg>
      </body>
      </html>
      ";

      File.WriteAllText(svgPath, svgString);

      var svgDocument = SvgDocument.Open(svgPath);  
      svgDocument.ShapeRendering = SvgShapeRendering.Auto;

      var bmp = svgDocument.Draw();              
      bmp.Save(pngPath, ImageFormat.Png);

      var myBitmap = new Bitmap(pngPath);
    }
  }
}

【问题讨论】:

  • 我不熟悉那个库,但我see an overload of Open that takes a stream.
  • 还有FromSVG()
  • 谢谢你看看,但请随时发布答案。
  • 我将如何使用 FromSVG?不确定泛型是做什么用的。尝试 SvgDocument.FromSvg(svgString)
  • Here's a unit test in the project 演示了一种从字符串获取 svg 文档的方法。但我认为FromSVG 会更容易,你试过了吗?编译器应该能够确定泛型类型,所以只需像任何方法一样调用它。

标签: c# svg bitmap


【解决方案1】:

您可以使用var mySvg = SvgDocument.FromSvg&lt;SvgDocument&gt;(svgString); 从字符串创建一个SvgDocument,然后您可以使用SvgDocument 提供的Draw() 方法将其绘制到位图,就像var myBmp = mySvg.Draw(); 一样,无需接触硬盘。

【讨论】:

  • 这似乎可以使用其他人提供的答案 var svgDocument = SvgDocument.FromSvg(svgString); svgDocument.ShapeRendering = SvgShapeRendering.Auto;字节[]位图数据; using(Bitmap bmp = svgDocument.Draw()) { using (MemoryStream stream = new MemoryStream()) { bmp.Save(stream, ImageFormat.Png); BitmapData = stream.ToArray(); } }
  • @tottaka 编辑您的答案以包含该代码。 OP 的 svg 部分有问题,而不是位图部分。
  • @Crowcoder 你说得对,我应该更仔细地阅读代码以了解试图实现的目标。刚刚更新了我的答案,谢谢!
【解决方案2】:

当你阅读SvgDocument.cs时,你可以找到'SvgDocument.Open(path)',所以我写了下面的方法。

private void TextToBitmap(string text)
{
    try
    {
        byte[] bytes = Encoding.UTF8.GetBytes(text);
        using (var stream = new MemoryStream(bytes))
        {
            svgDoc = SvgDocument.Open<SvgDocument>(stream, null);
        }
        pictureBox1.Image = svgDoc.Draw();
    }
    catch { }
}

【讨论】:

    【解决方案3】:

    这就是我使用该函数从 Stream 加载 SVG 而不保存到文件的方式。下一步是将其保存为 PNG 文件。

    public static void LoadSVG(string coinName)
        {
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://linktogetfile.com/" + coinName.ToUpper() + ".svg");
            request.Method = "GET";
            request.KeepAlive = true;
            request.ContentType = "image/png";
            request.Accept = "image/webp,image/apng";
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            Stream stream = response.GetResponseStream();
            try
            {
                SvgDocument svgDoc = SvgDocument.Open<SvgDocument>(stream);
                System.Drawing.Bitmap bitmap = svgDoc.Draw(rasterWidth: 24, rasterHeight: 24);
                bitmap.Save("mfile.png", System.Drawing.Imaging.ImageFormat.Png);
            }
            catch (Exception ex1)
            {
                //Error handling
            }
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-04-11
      • 2022-11-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多