我以前在dotnetcore解决方案中也遇到过同样的问题,只是我没有使用svg edit。这是我解决这个问题的办法…
使用dotnet framework创建一个新的控制台应用程序…并在包管理器上安装svg库
>> Install-Package Svg -Version 2.3.0
在控制台应用程序的program.cs中,读取svg文件并使用svg库将其转换为png并使用console.writeline输出
class Program
{
static void Main(string[] args)
{
if (args.Length == 0)
return;
using (var oStream = new System.IO.MemoryStream())
{
string svgPath = args[0];
if (string.IsNullOrWhiteSpace(svgPath))
return;
var text = System.IO.File.ReadAllText(svgPath);
using (var xmlStream = new MemoryStream(Encoding.ASCII.GetBytes(text)))
{
xmlStream.Position = 0;
var svgDocument = Svg.SvgDocument.Open<SvgDocument>(xmlStream);
SetupThumbnailImage(svgDocument, out Bitmap bitmap, out Graphics graphics, 250, 120);
svgDocument.Draw(graphics);
bitmap.Save(oStream, ImageFormat.Png);
string pngBase64String = Convert.ToBase64String(oStream.ToArray());
Console.WriteLine(pngBase64String);
}
}
}
}
在dotnet核心端,需要将svg发布回api,将其保存在服务器上,作为进程运行consoleapp.exe,并从标准输出读取base64。
private async Task<string> SaveThumbnailImage(string svgString)
{
// Save the svg
var filePath = Path.Combine(Path.GetDirectoryName(Assembly.GetEntryAssembly().Location), "SvgHelper", Path.Combine(Guid.NewGuid() + ".svg"));
using (StreamWriter streamWriter = System.IO.File.CreateText(filePath))
{
streamWriter.WriteLine(svgString);
}
Process process = new Process();
process.StartInfo.FileName = Path.Combine(Path.GetDirectoryName(Assembly.GetEntryAssembly().Location), "SvgHelper", "lib.svgToPng.exe");
process.StartInfo.Arguments = filePath;
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.CreateNoWindow = true;
process.Start();
// Read the base64String from StandardOutput
string base64String = process.StandardOutput.ReadToEnd();
process.WaitForExit();
// Clean up the file
System.IO.File.Delete(filePath);
}