IO.Stream 到 WPF 中的图像

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16857325/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 08:53:54  来源:igfitidea点击:

IO.Stream to Image in WPF

c#wpf

提问by Tanuj Wadhwa

I am trying to read an image from a resource only DLL file. I am able to read the image name and image bytes, but How do I set the Imagecontrol to stream buffer? In windows form, I know I can use this :

我正在尝试从仅资源的 DLL 文件中读取图像。我能够读取图像名称和图像字节,但是如何将Image控件设置为流缓冲区?在 Windows 窗体中,我知道我可以使用它:

pictureBox1.Image=new System.Drawing.Bitmap(IOStream);

since there is no Drawing namespace in wpf, how can I achieve the same thing?

由于 wpf 中没有 Drawing 命名空间,我该如何实现相同的功能?

采纳答案by nvoigt

In WPF you probably have an Imageelement in your xaml. The Sourcecan be any BitmapImage. You can bind a BitmapImagefrom your ViewModel, where you can create an instance from a Streamlike this.

在 WPFImage中,您的 xaml 中可能有一个元素。该Source可任意BitmapImage。你可以绑定一个BitmapImage从您的视图模型,在那里你可以从创建一个实例Stream这样

回答by John Willemse

In WPF, you can set the Sourceproperty of an Image, as in this example:

在 WPF 中,您可以设置 的Source属性Image,如下例所示:

Image image = new Image();
using (MemoryStream stream = new MemoryStream(byteArray))
{
    image.Source = BitmapFrame.Create(stream,
                                      BitmapCreateOptions.None,
                                      BitmapCacheOption.OnLoad);
}

Where byteArrayis the array of bytes with the source of the image.

byteArray带有图像源的字节数组在哪里。