从byte 显示图像
我用C#,wpf。 我有一个图像,它以以下forms存储: 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 [] )?
如果我从byte []创建临时文件,我必须在Source="..."
写入?
如果你有一个实现你的IFile
接口的视图模型类,并且它的FileData
属性包含像PNG或JPEG这样的编码图像缓冲区,你可以直接绑定到属性,如下所示:
这是因为WPF提供了从几种源类型(包括byte[]
到ImageSource
内置自动类型转换。
类型转换由ImageSourceConverter
类执行,该类注册为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); }