需要安装nuget包【svg】
public class SVGHelper
{
public static ImageSource ConvertSvgToDrawingImage(string path)
{
var svgContent = File.ReadAllText(path);
// 使用SvgDocument解析SVG内容
SvgDocument svgDocument = SvgDocument.FromSvg<SvgDocument>(svgContent);
// 将SvgDocument转换为Drawing对象
System.Drawing.Bitmap bitmap = svgDocument.Draw();
var imageSource = ToImageSource(bitmap);
return imageSource;
}
[DllImport("gdi32.dll", SetLastError = true)]
private static extern bool DeleteObject(IntPtr hObject);
public static ImageSource ToImageSource( Bitmap bitmap)
{
IntPtr hBitmap = bitmap.GetHbitmap();
ImageSource wpfBitmap = Imaging.CreateBitmapSourceFromHBitmap(hBitmap, IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions());
// 记得要进行内存释放。否则会有内存不足的报错。
if (!DeleteObject(hBitmap))
{
throw new Win32Exception();
}
return wpfBitmap;
}
}
标签:c#,svg,bitmap,Bitmap,static,hBitmap,SvgDocument,ImageSource
From: https://www.cnblogs.com/ives/p/18349860