我使用一个库(spire.pdf)从我要求用户的PDF文件中提取图像。该库将返回一个System.Drawing.Image数组,我目前正在试图找出如何将它们转换为Microsoft.Maui.Controls.Image
首先,我将System.Drawing.Image存储在一个MemoryStream中,它可以正常工作(参见屏幕快照)。但我的输出毛伊图没有任何内容。(ImageSource.FromStream)
我是不是漏掉了什么?我在这里添加了我的转换方法,谢谢您的帮助!
private Image Convert(System.Drawing.Image img)
{
MemoryStream stream = new MemoryStream();
img.Save(stream, System.Drawing.Imaging.ImageFormat.Bmp);
Image output = new Image()
{
Source = ImageSource.FromStream(() => stream)
};
return output;
}
发布于 2022-09-11 16:34:37
我遇到了同样的问题: ImageSource.FromStream(..)在Windows 11上不能正常工作,并返回空图像。
我为假本地文件创建了一个类,这样我就可以调用ImageSource.FromFile(..)相反:
假本地文件:
internal class FakeLocalFile : IDisposable
{
public string FilePath { get; }
/// <summary>
/// Currently ImageSource.FromStream is not working on windows devices.
/// This class saves the passed stream in a cache directory, returns the local path and deletes it on dispose.
/// </summary>
public FakeLocalFile(Stream source)
{
FilePath = Path.Combine(FileSystem.Current.CacheDirectory, $"{Guid.NewGuid()}.tmp");
using var fs = new FileStream(FilePath, FileMode.Create);
source.CopyTo(fs);
}
public void Dispose()
{
if (File.Exists(FilePath))
File.Delete(FilePath);
}
}
呼叫:
var fakeFile = new FakeLocalFile(info.Data);
Img.Source = ImageSource.FromFile(fakeFile.Path);
https://stackoverflow.com/questions/72160249
复制相似问题