从byte []显示图像 [英] Display image from byte[ ]
问题描述
我使用C#,wpf。我有一个图像,它以以下形式存储: byte []
I use C#, wpf. I have an image, it is stored in the form of: byte[ ]
public interface IFile
{
int Id { get; set; }
byte[] FileData { get; set; }
string FileName { get; set; }
int? FileSize { get; set; }
string FileExtension { get; set; }
}
如何显示图像(FileData byte [] )在表单上?
How can I display my image (FileData byte[ ]) on the form?
<GroupBox BorderThickness="1">
<Image Source="..."/>
</GroupBox>
我必须写 Source =...
,如果我从byte []创建一个临时文件?
I have to write in Source="..."
, if I create a temporary file from a byte[ ]?
推荐答案
如果你有一个视图模型类实现你的 IFile
接口,并且它的 FileData
属性包含一个像PNG或JPEG一样的编码图像缓冲区,你可以直接绑定到这样的属性:
Provided that you have a view model class that implements your IFile
interface, and that its FileData
property contains an encoded image buffer like a PNG or JPEG, you could directly bind to the property like this:
<Image Source="{Binding FileData}"/>
这是因为WPF提供了来自多种源类型的内置自动类型转换,包括 byte []
,到 ImageSource
。
This is because WPF provides built-in, automatic type conversion from several source types, including byte[]
, to ImageSource
.
类型转换由类 ImageSourceConverter
执行,注册为TypeConverter
The type conversion is performed by the class ImageSourceConverter
, which is registered as TypeConverter
[TypeConverterAttribute(typeof(ImageSourceConverter))]
public abstract class ImageSource ...
并做类似的事情:
byte[] buffer = ...
ImageSource result;
using (var stream = new MemoryStream(buffer))
{
result = BitmapFrame.Create(
stream, BitmapCreateOptions.None, BitmapCacheOption.OnLoad);
}
这篇关于从byte []显示图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!