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}"/>
This is because WPF provides built-in, automatic type conversion from several source types, including byte[]
, to ImageSource
.
The type conversion is performed by the class ImageSourceConverter
, which is registered as TypeConverter
[TypeConverterAttribute(typeof(ImageSourceConverter))]
public abstract class ImageSource ...
and does something similar to this:
byte[] buffer = ...
ImageSource result;
using (var stream = new MemoryStream(buffer))
{
result = BitmapFrame.Create(
stream, BitmapCreateOptions.None, BitmapCacheOption.OnLoad);
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…