代码之家  ›  专栏  ›  技术社区  ›  harriyott Erik Funkenbusch

使用C#[关闭]将SVG转换为PNG

  •  89
  • harriyott Erik Funkenbusch  · 技术社区  · 16 年前

    6 回复  |  直到 16 年前
        1
  •  84
  •   AxelEckenberger    8 年前

    有一种更简单的方法使用这个库 http://svg.codeplex.com/ (更新版本@ GIT NuGet ).这是我的密码

    var byteArray = Encoding.ASCII.GetBytes(svgFileContents);
    using (var stream = new MemoryStream(byteArray))
    {
        var svgDocument = SvgDocument.Open(stream);
        var bitmap = svgDocument.Draw();
        bitmap.Save(path, ImageFormat.Png);
    }
    
        2
  •  70
  •   harriyott Erik Funkenbusch    11 年前

    http://harriyott.com/2008/05/converting-svg-images-to-png-in-c.aspx

    另外还有一个C#SVG渲染引擎,主要设计用于允许在codeplex上的web上使用SVG文件,如果这是您的问题,它可能适合您的需要:

    原始项目
    http://www.codeplex.com/svg

    Fork提供修复和更多活动: (2013年7月7日增补)
    https://github.com/vvvv/SVG

        3
  •  12
  •   Uwe Keim    8 年前

    当我不得不在服务器上光栅化SVG时,我最终使用P/Invoke调用librsvg函数(您可以从windows版本的GIMP图像编辑程序中获取DLL)。

    [DllImport("kernel32.dll", SetLastError = true)]
    static extern bool SetDllDirectory(string pathname);
    
    [DllImport("libgobject-2.0-0.dll", SetLastError = true)]
    static extern void g_type_init(); 
    
    [DllImport("librsvg-2-2.dll", SetLastError = true)]
    static extern IntPtr rsvg_pixbuf_from_file_at_size(string file_name, int width, int height, out IntPtr error);
    
    [DllImport("libgdk_pixbuf-2.0-0.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
    static extern bool gdk_pixbuf_save(IntPtr pixbuf, string filename, string type, out IntPtr error, __arglist);
    
    public static void RasterizeSvg(string inputFileName, string outputFileName)
    {
        bool callSuccessful = SetDllDirectory("C:\\Program Files\\GIMP-2.0\\bin");
        if (!callSuccessful)
        {
            throw new Exception("Could not set DLL directory");
        }
        g_type_init();
        IntPtr error;
        IntPtr result = rsvg_pixbuf_from_file_at_size(inputFileName, -1, -1, out error);
        if (error != IntPtr.Zero)
        {
            throw new Exception(Marshal.ReadInt32(error).ToString());
        }
        callSuccessful = gdk_pixbuf_save(result, outputFileName, "png", out error, __arglist(null));
        if (!callSuccessful)
        {
            throw new Exception(error.ToInt32().ToString());
        }
    }
    
        4
  •  8
  •   stevenvh    16 年前

    Batik 为了这个。完整的Delphi代码:

    procedure ExecNewProcess(ProgramName : String; Wait: Boolean);
    var
      StartInfo : TStartupInfo;
      ProcInfo : TProcessInformation;
      CreateOK : Boolean;
    begin
      FillChar(StartInfo, SizeOf(TStartupInfo), #0);
      FillChar(ProcInfo, SizeOf(TProcessInformation), #0);
      StartInfo.cb := SizeOf(TStartupInfo);
      CreateOK := CreateProcess(nil, PChar(ProgramName), nil, nil, False,
                  CREATE_NEW_PROCESS_GROUP + NORMAL_PRIORITY_CLASS,
                  nil, nil, StartInfo, ProcInfo);
      if CreateOK then begin
        //may or may not be needed. Usually wait for child processes
        if Wait then
          WaitForSingleObject(ProcInfo.hProcess, INFINITE);
      end else
        ShowMessage('Unable to run ' + ProgramName);
    
      CloseHandle(ProcInfo.hProcess);
      CloseHandle(ProcInfo.hThread);
    end;
    
    procedure ConvertSVGtoPNG(aFilename: String);
    const
      ExecLine = 'c:\windows\system32\java.exe -jar C:\Apps\batik-1.7\batik-rasterizer.jar ';
    begin
      ExecNewProcess(ExecLine + aFilename, True);
    end;
    
        5
  •  4
  •   Michal Kieloch    12 年前

    要添加到@Anish的响应中,如果您在将SVG导出到图像时遇到看不到文本的问题,可以创建一个递归函数来循环SVGDocument的子级,如果可能,尝试将其强制转换为SvgText(添加您自己的错误检查),并设置字体系列和样式。

        foreach(var child in svgDocument.Children)
        {
            SetFont(child);
        }
    
        public void SetFont(SvgElement element)
        {
            foreach(var child in element.Children)
            {
                SetFont(child); //Call this function again with the child, this will loop
                                //until the element has no more children
            }
    
            try
            {
                var svgText = (SvgText)parent; //try to cast the element as a SvgText
                                               //if it succeeds you can modify the font
    
                svgText.Font = new Font("Arial", 12.0f);
                svgText.FontSize = new SvgUnit(12.0f);
            }
            catch
            {
    
            }
        }
    

    如果有问题,请告诉我。

        6
  •  -4
  •   lost lost    16 年前

    您可以使用altsoft xml2pdf lib来实现此目的