首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何将System.Drawing.Image转换为Microsoft.Maui.Controls.Image?

如何将System.Drawing.Image转换为Microsoft.Maui.Controls.Image?
EN

Stack Overflow用户
提问于 2022-05-08 10:49:12
回答 1查看 1.7K关注 0票数 1

我使用一个库(spire.pdf)从我要求用户的PDF文件中提取图像。该库将返回一个System.Drawing.Image数组,我目前正在试图找出如何将它们转换为Microsoft.Maui.Controls.Image

首先,我将System.Drawing.Image存储在一个MemoryStream中,它可以正常工作(参见屏幕快照)。但我的输出毛伊图没有任何内容。(ImageSource.FromStream)

我是不是漏掉了什么?我在这里添加了我的转换方法,谢谢您的帮助!

代码语言:javascript
运行
复制
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;
}
EN

回答 1

Stack Overflow用户

发布于 2022-09-11 16:34:37

我遇到了同样的问题: ImageSource.FromStream(..)在Windows 11上不能正常工作,并返回空图像。

我为假本地文件创建了一个类,这样我就可以调用ImageSource.FromFile(..)相反:

假本地文件:

代码语言:javascript
运行
复制
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);
    }
}

呼叫:

代码语言:javascript
运行
复制
var fakeFile = new FakeLocalFile(info.Data);
Img.Source = ImageSource.FromFile(fakeFile.Path);
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72160249

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档