C# 以编程方式将svg转换为图像

C# 以编程方式将svg转换为图像,c#,image,svg,C#,Image,Svg,我正在尝试将svg转换为图像 我一直在研究几种工具,但仍然无法实现这一点:( 1. 但我有麻烦,因为它没有文档 这是我的代码: using (FileStream fileStream = File.OpenRead(@"C:\sample.svg")) { MemoryStream memoryStream = new MemoryStream(); memoryStream.SetLength(fileStream.Lengt

我正在尝试将svg转换为图像 我一直在研究几种工具,但仍然无法实现这一点:(

1. 但我有麻烦,因为它没有文档

这是我的代码:

 using (FileStream fileStream = File.OpenRead(@"C:\sample.svg"))
        {
            MemoryStream memoryStream = new MemoryStream();
            memoryStream.SetLength(fileStream.Length);
            fileStream.Read(memoryStream.GetBuffer(), 0, (int)fileStream.Length);
            SvgDocument svgDocument = SvgDocument.Open(memoryStream);
            string imagePath = "local.jpg";
            svgDocument.Draw().Save(imagePath);
        }
或者这个:

        var sampleDoc = SvgDocument.Open(@"C:\sample.svg");
        sampleDoc.Draw().Save(@"C:\sample.png");
两个都不行

我得到一个错误:

Object reference not set to an instance of an object.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

Source Error: 


Line 363:                SvgDocument svgDocument = SvgDocument.Open(memoryStream);
Line 364:                string imagePath = "local.jpg";
Line 365:                svgDocument.Draw().Save(imagePath);
Line 366:            }
Line 367:  
问题似乎是svgDocument为空


2.

我的代码:

 string svgFileName = HttpContext.Current.Server.MapPath("sample.svg");
        string PngRelativeDirectory = HttpContext.Current.Server.MapPath("~/");
        string pngName = "svgpieresult.png";
        string pngFileName = HttpContext.Current.Server.MapPath("pngName);


        // ignored assume sample.svg is in the web app directory
        //using (StreamWriter writer = new StreamWriter(svgFileName, false))
        //{
        //    writer.Write(svgXml);
        //}


        string inkscapeArgs = string.Format(@"-f ""{0}"" -e ""{1}""", svgFileName, pngFileName);

        Process inkscape = Process.Start(
          new ProcessStartInfo("C:\\Program Files\\inkscape\\inkscape.exe", inkscapeArgs));

        inkscape.WaitForExit(3000);
        //Context.RewritePath(pngName);
        context.Response.Redirect(PngRelativeDirectory + pngName); 
但是什么也没发生!!我想我错过了一些保存图像的东西,而不是重写路径()


如果您有更好的解决方案,请将其与如何使用的示例一起发布,谢谢

这是我的svg:

<?xml version="1.0" standalone="yes"?><!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">

<svg width="598px" height="346px" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" version="1.1">

<defs><linearGradient x1="0" y1="0" x2="1" y2="0.9999999999999998" id="theme-94ae0a-6b7e07"><stop offset="0%" stop-color="#94ae0a" stop-opacity="1"></stop><stop offset="100%" stop-color="#6b7e07" stop-opacity="1"></stop></linearGradient><linearGradient x1="0" y1="0" x2="1" y2="0.9999999999999998" id="theme-115fa6-0c4578"><stop offset="0%" stop-color="#115fa6" stop-opacity="1"></stop><stop offset="100%" stop-color="#0c4578" stop-opacity="1"></stop></linearGradient><linearGradient x1="0" y1="0" x2="1" y2="0.9999999999999998" id="theme-a61120-780c17"><stop offset="0%" stop-color="#a61120" stop-opacity="1"></stop><stop offset="100%" stop-color="#780c17" stop-opacity="1"></stop></linearGradient><linearGradient x1="0" y1="0" x2="1" y2="0.9999999999999998" id="theme-ff8809-d56e00"><stop offset="0%" stop-color="#ff8809" stop-opacity="1"></stop><stop offset="100%" stop-color="#d56e00" stop-opacity="1"></stop></linearGradient><linearGradient x1="0" y1="0" x2="1" y2="0.9999999999999998" id="theme-ffd13e-ffc50b"><stop offset="0%" stop-color="#ffd13e" stop-opacity="1"></stop><stop offset="100%" stop-color="#ffc50b" stop-opacity="1"></stop></linearGradient><linearGradient x1="0" y1="0" x2="1" y2="0.9999999999999998" id="theme-a61187-780c61"><stop offset="0%" stop-color="#a61187" stop-opacity="1"></stop><stop offset="100%" stop-color="#780c61" stop-opacity="1"></stop></linearGradient><linearGradient x1="0" y1="0" x2="1" y2="0.9999999999999998" id="theme-24ad9a-1b8374"><stop offset="0%" stop-color="#24ad9a" stop-opacity="1"></stop><stop offset="100%" stop-color="#1b8374" stop-opacity="1"></stop></linearGradient><linearGradient x1="0" y1="0" x2="1" y2="0.9999999999999998" id="theme-7c7474-625b5b"><stop offset="0%" stop-color="#7c7474" stop-opacity="1"></stop><stop offset="100%" stop-color="#625b5b" stop-opacity="1"></stop></linearGradient><linearGradient x1="0" y1="0" x2="1" y2="0.9999999999999998" id="theme-a66111-78460c"><stop offset="0%" stop-color="#a66111" stop-opacity="1"></stop><stop offset="100%" stop-color="#78460c" stop-opacity="1"></stop></linearGradient></defs><rect width="100%" height="100%" fill="#fff" fill-opacity="0" stroke="none" /><path d="M 230 173L 272.3305450559981 68.22822443395302A 113 113 0 0 1 343 173L 343 173A 113 113 0 0 1 272.3305450559981 277.771775566047L 272.3305450559981 277.771775566047L 230 173Z" fill="none" stroke="rgb(200, 200, 200)" stroke-width="6" stroke-opacity="1" transform="matrix(1,0,0,1,1,2)" /><path d="M 230 173L 272.3305450559981 68.22822443395302A 113 113 0 0 1 343 173L 343 173A 113 113 0 0 1 272.3305450559981 277.771775566047L 272.3305450559981 277.771775566047L 230 173Z" fill="none" stroke="rgb(150, 150, 150)" stroke-width="4" stroke-opacity="1" transform="matrix(1,0,0,1,0,1)" /><path d="M 230 173L 272.3305450559981 68.22822443395302A 113 113 0 0 1 343 173L 343 173A 113 113 0 0 1 272.3305450559981 277.771775566047L 272.3305450559981 277.771775566047L 230 173Z" fill="none" stroke="rgb(100, 100, 100)" stroke-width="2" stroke-opacity="1" transform="matrix(1,0,0,1,0,1)" /><path d="M 230 173L 210.37775592363687 61.7167239096205A 113 113 0 0 1 241.81171634924485 60.619025823385115L 241.81171634924485 60.619025823385115A 113 113 0 0 1 272.3305450559981 68.22822443395302L 272.3305450559981 68.22822443395302L 230 173Z" fill="none" stroke="rgb(200, 200, 200)" stroke-width="6" stroke-opacity="1" transform="matrix(1,0,0,1,1,2)" /><path d="M 230 173L 210.37775592363687 61.7167239096205A 113 113 0 0 1 241.81171634924485 60.619025823385115L 241.81171634924485 60.619025823385115A 113 113 0 0 1 272.3305450559981 68.22822443395302L 272.3305450559981 68.22822443395302L 230 173Z" fill="none" stroke="rgb(150, 150, 150)" stroke-width="4" stroke-opacity="1" transform="matrix(1,0,0,1,0,1)" /><path d="M 230 173L 210.37775592363687 61.7167239096205A 113 113 0 0 1 241.81171634924485 60.619025823385115L 241.81171634924485 60.619025823385115A 113 113 0 0 1 272.3305450559981 68.22822443395302L 272.3305450559981 68.22822443395302L 230 173Z" fill="none" stroke="rgb(100, 100, 100)" stroke-width="2" stroke-opacity="1" transform="matrix(1,0,0,1,0,1)" /><path d="M 230 173L 120.3565829308124 200.33717420276244A 113 113 0 0 1 135.23022582216709 111.4557890433019L 135.23022582216709 111.4557890433019A 113 113 0 0 1 210.37775592363687 61.7167239096205L 210.37775592363687 61.7167239096205L 230 173Z" fill="none" stroke="rgb(200, 200, 200)" stroke-width="6" stroke-opacity="1" transform="matrix(1,0,0,1,1,2)" /><path d="M 230 173L 120.3565829308124 200.33717420276244A 113 113 0 0 1 135.23022582216709 111.4557890433019L 135.23022582216709 111.4557890433019A 113 113 0 0 1 210.37775592363687 61.7167239096205L 210.37775592363687 61.7167239096205L 230 173Z" fill="none" stroke="rgb(150, 150, 150)" stroke-width="4" stroke-opacity="1" transform="matrix(1,0,0,1,0,1)" /><path d="M 230 173L 120.3565829308124 200.33717420276244A 113 113 0 0 1 135.23022582216709 111.4557890433019L 135.23022582216709 111.4557890433019A 113 113 0 0 1 210.37775592363687 61.7167239096205L 210.37775592363687 61.7167239096205L 230 173Z" fill="none" stroke="rgb(100, 100, 100)" stroke-width="2" stroke-opacity="1" transform="matrix(1,0,0,1,0,1)" /><path d="M 230 173L 151.5036041381333 254.28539743826758A 113 113 0 0 1 132.13912937235844 229.5L 132.13912937235844 229.5A 113 113 0 0 1 120.3565829308124 200.33717420276244L 120.3565829308124 200.33717420276244L 230 173Z" fill="none" stroke="rgb(200, 200, 200)" stroke-width="6" stroke-opacity="1" transform="matrix(1,0,0,1,1,2)" /><path d="M 230 173L 151.5036041381333 254.28539743826758A 113 113 0 0 1 132.13912937235844 229.5L 132.13912937235844 229.5A 113 113 0 0 1 120.3565829308124 200.33717420276244L 120.3565829308124 200.33717420276244L 230 173Z" fill="none" stroke="rgb(150, 150, 150)" stroke-width="4" stroke-opacity="1" transform="matrix(1,0,0,1,0,1)" /><path d="M 230 173L 151.5036041381333 254.28539743826758A 113 113 0 0 1 132.13912937235844 229.5L 132.13912937235844 229.5A 113 113 0 0 1 120.3565829308124 200.33717420276244L 120.3565829308124 200.33717420276244L 230 173Z" fill="none" stroke="rgb(100, 100, 100)" stroke-width="2" stroke-opacity="1" transform="matrix(1,0,0,1,0,1)" /><path d="M 230 173L 208.43858352245047 283.92387172958604A 113 113 0 0 1 177.8224067044411 273.23222414913903L 177.8224067044411 273.23222414913903A 113 113 0 0 1 151.5036041381333 254.28539743826758L 151.5036041381333 254.28539743826758L 230 173Z" fill="none" stroke="rgb(200, 200, 200)" stroke-width="6" stroke-opacity="1" transform="matrix(1,0,0,1,1,2)" /><path d="M 230 173L 208.43858352245047 283.92387172958604A 113 113 0 0 1 177.8224067044411 273.23222414913903L 177.8224067044411 273.23222414913903A 113 113 0 0 1 151.5036041381333 254.28539743826758L 151.5036041381333 254.28539743826758L 230 173Z" fill="none" stroke="rgb(150, 150, 150)" stroke-width="4" stroke-opacity="1" transform="matrix(1,0,0,1,0,1)" /><path d="M 230 173L 208.43858352245047 283.92387172958604A 113 113 0 0 1 177.8224067044411 273.23222414913903L 177.8224067044411 273.23222414913903A 113 113 0 0 1 151.5036041381333 254.28539743826758L 151.5036041381333 254.28539743826758L 230 173Z" fill="none" stroke="rgb(100, 100, 100)" stroke-width="2" stroke-opacity="1" transform="matrix(1,0,0,1,0,1)" /><path d="M 230 173L 270.49557829861897 278.4945881941838A 113 113 0 0 1 239.84859893048534 285.57000088436723L 239.84859893048534 285.57000088436723A 113 113 0 0 1 208.43858352245047 283.92387172958604L 208.43858352245047 283.92387172958604L 230 173Z" fill="none" stroke="rgb(200, 200, 200)" stroke-width="6" stroke-opacity="1" transform="matrix(1,0,0,1,1,2)" /><path d="M 230 173L 270.49557829861897 278.4945881941838A 113 113 0 0 1 239.84859893048534 285.57000088436723L 239.84859893048534 285.57000088436723A 113 113 0 0 1 208.43858352245047 283.92387172958604L 208.43858352245047 283.92387172958604L 230 173Z" fill="none" stroke="rgb(150, 150, 150)" stroke-width="4" stroke-opacity="1" transform="matrix(1,0,0,1,0,1)" /><path d="M 230 173L 270.49557829861897 278.4945881941838A 113 113 0 0 1 239.84859893048534 285.57000088436723L 239.84859893048534 285.57000088436723A 113 113 0 0 1 208.43858352245047 283.92387172958604L 208.43858352245047 283.92387172958604L 230 173Z" fill="none" stroke="rgb(100, 100, 100)" stroke-width="2" stroke-opacity="1" transform="matrix(1,0,0,1,0,1)" /><path d="M 230 173L 272.3305450559981 68.22822443395302A 113 113 0 0 1 343 173L 343 173A 113 113 0 0 1 272.3305450559981 277.771775566047L 272.3305450559981 277.771775566047L 230 173Z" fill="url(#theme-94ae0a-6b7e07)" stroke-width="0px" transform="matrix(1,0,0,1,0,0)" /><path d="M 230 173L 210.37775592363687 61.7167239096205A 113 113 0 0 1 241.81171634924485 60.619025823385115L 241.81171634924485 60.619025823385115A 113 113 0 0 1 272.3305450559981 68.22822443395302L 272.3305450559981 68.22822443395302L 230 173Z" fill="url(#theme-115fa6-0c4578)" stroke-width="0px" transform="matrix(1,0,0,1,0,0)" /><path d="M 230 173L 120.3565829308124 200.33717420276244A 113 113 0 0 1 135.23022582216709 111.4557890433019L 135.23022582216709 111.4557890433019A 113 113 0 0 1 210.37775592363687 61.7167239096205L 210.37775592363687 61.7167239096205L 230 173Z" fill="url(#theme-a61120-780c17)" stroke-width="0px" transform="matrix(1,0,0,1,0,0)" /><path d="M 230 173L 151.5036041381333 254.28539743826758A 113 113 0 0 1 132.13912937235844 229.5L 132.13912937235844 229.5A 113 113 0 0 1 120.3565829308124 200.33717420276244L 120.3565829308124 200.33717420276244L 230 173Z" fill="url(#theme-ff8809-d56e00)" stroke-width="0px" transform="matrix(1,0,0,1,0,0)" /><path d="M 230 173L 208.43858352245047 283.92387172958604A 113 113 0 0 1 177.8224067044411 273.23222414913903L 177.8224067044411 273.23222414913903A 113 113 0 0 1 151.5036041381333 254.28539743826758L 151.5036041381333 254.28539743826758L 230 173Z" fill="url(#theme-ffd13e-ffc50b)" stroke-width="0px" transform="matrix(1,0,0,1,0,0)" /><path d="M 230 173L 270.49557829861897 278.4945881941838A 113 113 0 0 1 239.84859893048534 285.57000088436723L 239.84859893048534 285.57000088436723A 113 113 0 0 1 208.43858352245047 283.92387172958604L 208.43858352245047 283.92387172958604L 230 173Z" fill="url(#theme-a61187-780c61)" stroke-width="0px" transform="matrix(1,0,0,1,0,0)" /><text x="286.5" y="173" font-size="18" font-family="Arial" text-anchor="middle" fill="#333333" transform="matrix(1,0,0,1,0,0)" ><tspan x="286.5" dy="4.5">January</tspan></text><text x="235.1665388416979" y="116.73671822229682" font-size="18" font-family="Arial" text-anchor="middle" fill="#cccccc" transform="matrix(0.0914,-0.9958,0.9958,0.0914,97.4145,340.2432)" ><tspan x="235.1665388416979" dy="4.5">February</tspan></text><text x="182.2358551905599" y="142.81993256099588" font-size="18" font-family="Arial" text-anchor="middle" fill="#cccccc" transform="matrix(0.8454,0.5342,-0.5342,0.8454,104.4655,-75.2608)" ><tspan x="182.2358551905599" dy="4.5">March</tspan></text><text x="181.4045806349622" y="201.8224776300739" font-size="18" font-family="Arial" text-anchor="middle" fill="#333333" transform="matrix(0.8601,-0.5101,0.5101,0.8601,-77.5769,120.7761)" ><tspan x="181.4045806349622" dy="4.5">April</tspan></text><text x="204.31408267270237" y="223.32378812306592" font-size="18" font-family="Arial" text-anchor="middle" fill="#333333" transform="matrix(0.4546,-0.8907,0.8907,0.4546,-87.4823,303.7766)" ><tspan x="204.31408267270237" dy="4.5">May</tspan></text><text x="235.16653884169793" y="229.26328177770318" font-size="18" font-family="Arial" text-anchor="middle" fill="#cccccc" transform="matrix(0.0914,0.9958,-0.9958,0.0914,441.9649,-25.8825)" ><tspan x="235.16653884169793" dy="4.5">June</tspan></text><rect x="459.5" y="85.5" width="78" height="173" fill="#FFF" stroke="#000" stroke-width="1" transform="matrix(1,0,0,1,0,0)" /><text x="485" y="102" font-size="12" font-family="Helvetica, sans-serif" fill="#000" transform="matrix(1,0,0,1,0,0)" ><tspan x="485" dy="3">January</tspan></text><rect x="0" y="0" width="12" height="12" fill="url(#theme-94ae0a-6b7e07)" transform="matrix(1,0,0,1,465,96)" /><text x="485" y="129" font-size="12" font-family="Helvetica, sans-serif" fill="#000" transform="matrix(1,0,0,1,0,0)" ><tspan x="485" dy="3">February</tspan></text><rect x="0" y="0" width="12" height="12" fill="url(#theme-115fa6-0c4578)" transform="matrix(1,0,0,1,465,123)" /><text x="485" y="156" font-size="12" font-family="Helvetica, sans-serif" fill="#000" transform="matrix(1,0,0,1,0,0)" ><tspan x="485" dy="3">March</tspan></text><rect x="0" y="0" width="12" height="12" fill="url(#theme-a61120-780c17)" transform="matrix(1,0,0,1,465,150)" /><text x="485" y="183" font-size="12" font-family="Helvetica, sans-serif" fill="#000" transform="matrix(1,0,0,1,0,0)" ><tspan x="485" dy="3">April</tspan></text><rect x="0" y="0" width="12" height="12" fill="url(#theme-ff8809-d56e00)" transform="matrix(1,0,0,1,465,177)" /><text x="485" y="210" font-size="12" font-family="Helvetica, sans-serif" fill="#000" transform="matrix(1,0,0,1,0,0)" ><tspan x="485" dy="3">May</tspan></text><rect x="0" y="0" width="12" height="12" fill="url(#theme-ffd13e-ffc50b)" transform="matrix(1,0,0,1,465,204)" /><text x="485" y="237" font-size="12" font-family="Helvetica, sans-serif" fill="#000" transform="matrix(1,0,0,1,0,0)" ><tspan x="485" dy="3">June</tspan></text><rect x="0" y="0" width="12" height="12" fill="url(#theme-a61187-780c61)" transform="matrix(1,0,0,1,465,231)" /><rect x="0" y="-9" width="63" height="21" fill="#f00" fill-opacity="0" transform="matrix(1,0,0,1,465,96)" /><rect x="0" y="-9" width="68" height="21" fill="#f00" fill-opacity="0" transform="matrix(1,0,0,1,465,123)" /><rect x="0" y="-9" width="53" height="21" fill="#f00" fill-opacity="0" transform="matrix(1,0,0,1,465,150)" /><rect x="0" y="-9" width="44" height="21" fill="#f00" fill-opacity="0" transform="matrix(1,0,0,1,465,177)" /><rect x="0" y="-9" width="41" height="21" fill="#f00" fill-opacity="0" transform="matrix(1,0,0,1,465,204)" /><rect x="0" y="-9" width="47" height="21" fill="#f00" fill-opacity="0" transform="matrix(1,0,0,1,465,231)" />

</svg>

1月、2月、3月、3月、3月、3月、5月、6月

您的SVG在Chrome中加载并呈现

我已经从CodePlex下载了SVG和DLL,并在LinqPad4中使用SVG重新创建了简单的两行fail

我得到了同样的失败,但是LinqPad4显示了DLL跟踪输出,它显示了

...
LINQPad.exe Information: 0 : Begin CreateElement: rect
LINQPad.exe Information: 0 : Begin SetAttributes
LINQPad.exe Information: 0 : End SetAttributes
LINQPad.exe Information: 0 : End CreateElement
LINQPad.exe Information: 0 : End Read
loaded -- I added this to show the different method calls

LINQPad.exe Information: 0 : Begin Render
... 
StackTrace    at Svg.SvgUnit.ToDeviceValue(ISvgStylable styleOwner, Boolean vertical)
   at Svg.SvgRectangle.get_Path()
   at Svg.SvgGraphicsElement.Render(SvgRenderer renderer)
   at Svg.SvgRectangle.Render(SvgRenderer renderer)
   at Svg.SvgElement.RenderChildren(SvgRenderer renderer)
   at Svg.SvgElement.Render(SvgRenderer renderer)
   at Svg.SvgDocument.Draw()  
HelpLink null  
因此,当它试图绘制第一条路径时,出现了一些问题

您说没有文档,但整个源代码(包括项目文件等)都在CodePlex上。您可以下载它,看看是否可以在Svg.SvgUnit.ToDeviceValue方法中解决此问题


…或者只需下载ImageMagick并使用Process类触发命令行程序Convert.exe

我在这里对您的代码做了一些更改: 我认为InkScape对您使用的路径有问题

PngRelativeDirectory+“\”+pngFileName

PngRelativeDirectory中已经有一个“\”,因此路径是c:\\sample.png

我还将Context.RewritePath更改为Response.Redirect-我在浏览器中呈现饼图

string svgFileName = HttpContext.Current.Server.MapPath("sample.svg");
string PngRelativeDirectory = "C:\\";
string pngName = "svgpieresult.png";
string pngFileName = HttpContext.Current.Server.MapPath(pngName);


/* ignored assume sample.svg is in the web app directory
using (StreamWriter writer = new StreamWriter(svgFileName, false))
{
    writer.Write(svgXml);
}
 */

string inkscapeArgs = string.Format(@"-f ""{0}"" -e ""{1}""", svgFileName, pngFileName);

Process inkscape = Process.Start(
  new ProcessStartInfo( "C:\\Program Files\\inkscape\\inkscape.exe", inkscapeArgs));

inkscape.WaitForExit(3000);
//Context.RewritePath(pngName);
this.Response.Redirect(pngName);

svg有效吗?您应该放置完整的堆栈跟踪以帮助调试!我如何知道svg有效?(问题已编辑)它是否在兼容的应用程序中打开?我怀疑local.jpg是不够的。请尝试提供完整路径``string imagePath=@“c:\temp\local.jpg”;已经尝试过了,但仍然会出现同样的错误。在兼容的应用程序中打开是什么意思?谢谢,我尝试过查找源代码,但我仍然是c#的初学者,所以这对我来说有点困难!现在我尝试使用Inkscape(见编辑的问题)如果你给我一些示例代码,我也可以看看ImageMagick谢谢你,但它似乎没有解决问题,svgpieresult.png没有创建我得到:HTTP错误404.0-找不到。我不需要将它呈现到浏览器,我需要将它保存在文件夹中,我知道我使用了重定向,但它只是用于测试,因为我没有找到方法在命令行模式下使用InkScape保存文件,-f选项表示打开此文件,-e标志表示在此处将其导出为png,然后将退出。因此没有保存方法。您是否更改了输出png的路径?您的代码试图写入C:驱动器的根目录。C:\上的权限通常设置为不允许文件为created在那里。因此,运行网站的用户帐户可能没有必要的权限将文件写入c:\yes,我将路径更改为local。你知道如何保存png吗?我查看了选项,但似乎没有任何选项可以这样做!你不需要保存它。当你运行带有参数的Process类时,它是almost与从命令行运行程序相同,-e选项表示将文件导出为png。