在c# wpf中从Memorystream获取Imagesource

发布于 2024-11-18 17:58:28 字数 153 浏览 3 评论 0原文

如何使用 C# 从 WPF 中的 MemoryStream 获取 ImageSource ?或将 MemoryStream 转换为 ImageSource 以将其显示为 wpf 中的图像?

How can I get ImageSource from MemoryStream in WPF using c# ? or convert MemoryStream to ImageSource to display it as image in wpf ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

因为看清所以看轻 2024-11-25 17:58:28
using (MemoryStream memoryStream = ...)
{
    var imageSource = new BitmapImage();
    imageSource.BeginInit();
    imageSource.StreamSource = memoryStream;
    imageSource.EndInit();

    // Assign the Source property of your image
    image.Source = imageSource;
}
using (MemoryStream memoryStream = ...)
{
    var imageSource = new BitmapImage();
    imageSource.BeginInit();
    imageSource.StreamSource = memoryStream;
    imageSource.EndInit();

    // Assign the Source property of your image
    image.Source = imageSource;
}
第几種人 2024-11-25 17:58:28

Additional to @Darin Dimitrov answer if you disposed the stream before assigning to Image.Source nothing will show, so be careful

例如,Next 方法将不起作用,来自我使用 LiteDB 的一个项目

public async Task<BitmapImage> DownloadImage(string id)
{
    using (var stream = new MemoryStream())
    {
        var f = _appDbManager.DataStorage.FindById(id);
        f.CopyTo(stream);
        var imageSource = new BitmapImage {CacheOption = BitmapCacheOption.OnLoad};
        imageSource.BeginInit();
        imageSource.StreamSource = stream;
        imageSource.EndInit();
        return imageSource;
    }
}

您不能使用上一个函数返回的 imageSource

但此实现将起作用


public async Task<BitmapImage> DownloadImage(string id)
{
    // TODO: [Note] Bug due to memory leaks, if we used Using( var stream = new MemoryStream()), we will lost the stream, and nothing will shown
    var stream = new MemoryStream();
    var f = _appDbManager.DataStorage.FindById(id);
    f.CopyTo(stream);
    var imageSource = new BitmapImage {CacheOption = BitmapCacheOption.OnLoad};
    imageSource.BeginInit();
    imageSource.StreamSource = stream;
    imageSource.EndInit();
    return imageSource;
}

Additional to @Darin Dimitrov answer if you disposed the stream before assigning to Image.Source nothing will show, so be careful

For example, the Next method will not work, From one of my projects using LiteDB

public async Task<BitmapImage> DownloadImage(string id)
{
    using (var stream = new MemoryStream())
    {
        var f = _appDbManager.DataStorage.FindById(id);
        f.CopyTo(stream);
        var imageSource = new BitmapImage {CacheOption = BitmapCacheOption.OnLoad};
        imageSource.BeginInit();
        imageSource.StreamSource = stream;
        imageSource.EndInit();
        return imageSource;
    }
}

you can not use returned imageSource from the last function

But this implementation will work


public async Task<BitmapImage> DownloadImage(string id)
{
    // TODO: [Note] Bug due to memory leaks, if we used Using( var stream = new MemoryStream()), we will lost the stream, and nothing will shown
    var stream = new MemoryStream();
    var f = _appDbManager.DataStorage.FindById(id);
    f.CopyTo(stream);
    var imageSource = new BitmapImage {CacheOption = BitmapCacheOption.OnLoad};
    imageSource.BeginInit();
    imageSource.StreamSource = stream;
    imageSource.EndInit();
    return imageSource;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文