<StackPanel>
<StackPanel.Resources>
<local:ImageConverter x:Key="cvt_image" />
</StackPanel.Resources>
<TextBox x:Name="txtPath" Text="E:\网盘\我的酷盘\MyFolder\MyPhoto\Panda.jpg" />
<Image Source="{Binding ElementName=txtPath, Path=Text}"/>
<Image Source="{Binding ElementName=txtPath, Path=Text, Converter={StaticResource cvt_image}}" />
</StackPanel>
这里只是一个示范,其实是可以将数据库中取出来的二进制数据,直接转换成一个BitmapImage对象返回的:
public class ImageConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
return new BitmapImage(new Uri(value.ToString()));
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new N

本文揭示了WPF中Image控件的Source属性实际上是一个ImageSource对象,它可以接受BitmapImage和DrawingImage作为子类。作者分享了如何将数据库中的二进制数据转化为BitmapImage对象直接赋值给Source,指出以往误解为只能设置图片地址的问题,并感叹WPF纯代码技术资料的匮乏。
最低0.47元/天 解锁文章
372

被折叠的 条评论
为什么被折叠?



