加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 如何从类型Image转换为类型BitmapImage?

发布时间:2020-12-15 08:29:25 所属栏目:百科 来源:网络整理
导读:有谁知道如何从图像创建Bitmap Image?这是我正在使用的代码: MemoryStream memStream = new MemoryStream(bytes);Image img = Image.FromStream(memStream); 现在我可以在内存中访问此图像.问题是我需要将其转换为类型BitmapImage才能在我的解决方案中使用
有谁知道如何从图像创建Bitmap Image?这是我正在使用的代码:
MemoryStream memStream = new MemoryStream(bytes);
Image img = Image.FromStream(memStream);

现在我可以在内存中访问此图像.问题是我需要将其转换为类型BitmapImage才能在我的解决方案中使用它.

注意:我需要将其转换为类型BitmapImage而不是类型Bitmap.我无法弄明白,因为类型BitmapImage为它的构造函数接受了一个Uri.

解决方法

从 this post起
public BitmapImage ImageFromBuffer(Byte[] bytes)
{
    MemoryStream stream = new MemoryStream(bytes);
    BitmapImage image = new BitmapImage();
    image.BeginInit();
    image.StreamSource = stream;
    image.EndInit();
    return image;
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读