【问题标题】:Converting SVG image from Url to PNG in C#在 C# 中将 SVG 图像从 Url 转换为 PNG
【发布时间】:2017-12-08 15:59:43
【问题描述】:

我正在将 svg 图像转换为 png 格式。我从 url 获取图像。我收到异常Parameter is not valid。以下是我的代码:

string svgFileName = "https://upload.wikimedia.org/wikipedia/commons/e/ed/Chicago_Cubs_Logo.svg";
using (WebClient webClient = new WebClient())
{
    byte[] data = webClient.DownloadData(svgFileName);

    ImageConverter imageConverter = new System.Drawing.ImageConverter();
    Image image = imageConverter.ConvertFrom(data) as Image;
    image.Save("c:\\hello", ImageFormat.Png);                    
}

我收到以下异常:

以下是我的 StackTrace:

at System.Drawing.Image.FromStream(Stream stream, Boolean useEmbeddedColorManagement, Boolean validateImageData)
at System.Drawing.ImageConverter.ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, Object value)   
at System.ComponentModel.TypeConverter.ConvertFrom(Object value)

我错过了什么?

【问题讨论】:

    标签: c# svg


    【解决方案1】:

    SVG 是文件而不是普通图像,您需要将 svg 下载为文件而不是图像。

    string svgFileName = "https://upload.wikimedia.org/wikipedia/commons/e/ed/Chicago_Cubs_Logo.svg";
    using (WebClient webClient = new WebClient())
    {
       webClient.DownloadFile(svgFileName, "hello.svg");             
    }
    

    下载文件后,您需要将此 svg 文件转换为图像,为此您可以使用SVG Nuget Gallery

    您可以访问此链接进行 svg 文件转换。 https://stackoverflow.com/a/58912/2745294 https://stackoverflow.com/a/12884409/2745294

    【讨论】:

    • 非常感谢。从过去的两个小时开始,我正在伸展我的头。我刚刚从您提供的链接中添加了代码。
    猜你喜欢
    • 1970-01-01
    • 2017-12-21
    • 1970-01-01
    • 2017-05-16
    • 1970-01-01
    • 2022-07-05
    • 2011-06-16
    • 2017-05-11
    相关资源
    最近更新 更多