我正在尝试使用 MAUI 应用程序中的 Aspose.Imaging 包将 svg 图像从 url 转换为 png 格式。
但是我在 SvgImage image = (SvgImage)Image.Load(localPath); 处遇到以下错误
{Aspose.Imaging.CoreExceptions.ImageLoadException: Image loading failed.
---> System.TypeInitializationException: The type initializer for 'Aspose.Imaging.ImageExportersRegistry' threw an exception.
---> Aspose.Imaging.CoreExceptions.ImageLoadException: Error invocation Aspose.Imaging.InjectExporters.Process
---> System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation.
---> System.ArgumentException: Font 'FreeSans' cannot be found
at System.Drawing.FontFamily.(String , FontCollection )
at System.Drawing.FontFamily..ctor(String name)
at System.Drawing.Font.(String , Single , FontStyle , GraphicsUnit , Byte , Boolean )
以下是我的代码:
string imageUrl = "https://dev.w3.org/SVG/tools/svgweb/samples/svg-files/dst.svg";
using var httpResponse = await Client.GetAsync(imageUrl).ConfigureAwait(false);
if (httpResponse.StatusCode is HttpStatusCode.OK)
{
var downloadedImage = await httpResponse.Content.ReadAsByteArrayAsync().ConfigureAwait(false);
var downloadedImageModel = new DownloadedImageModel
{
ImageUrl = imageUrl,
DownloadedImageBlob = downloadedImage
};
Uri myUri = new Uri(imageUrl, UriKind.Absolute);
//// Load the image
string documentsPath = Android.OS.Environment.GetExternalStoragePublicDirectory(Android.OS.Environment.DirectoryPictures).AbsolutePath;
string localFilename = "dst.svg";
string localPath = System.IO.Path.Combine(documentsPath, localFilename);
File.WriteAllBytes(localPath, downloadedImageModel.DownloadedImageBlob);
try
{
SvgImage image = (SvgImage)Image.Load(localPath);
using (image)
{
// Create an instance of PNG options and Save the results to disk
PngOptions pngOptions = new PngOptions();
SvgRasterizationOptions svgOptions = new SvgRasterizationOptions();
svgOptions.PageWidth = 100;
svgOptions.PageHeight = 200;
pngOptions.VectorRasterizationOptions = svgOptions;
//image.Save(dataDir + "result.png", pngOptions);
}
}
}
如有任何帮助,我们将不胜感激!
在使用Image.Load之前,需要调用FontSettings.SetFontFolders()