public class BinaryToImageConverter:IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value is Binary)
{
byte[] bytes = new byte[(value as Binary).Bytes.Length];
bytes = (value as Binary).Bytes;
MemoryStream stream = new MemoryStream(bytes, 0, bytes.Length);
BitmapImage image = new BitmapImage();
try
{
image.SetSource(stream);
return image;
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
if (value is String)
{
string s = value.ToString();
return s + 1111;
}
return null;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
<Grid x:Name="LayoutRoot" Background="White">
<Grid.Resources>
<silverlightApplication2:BinaryToImageConverter x:Key="BinaryConventer"></silverlightApplication2:BinaryToImageConverter>
</Grid.Resources>
<StackPanel>
<Button x:Name="btselectfile" Height="30" Content="選擇文件" Click="btselectfile_Click"></Button>
<Button x:Name="btshow" Height="30" Content="顯示圖片" Click="btupload_Click"></Button>
<ListBox x:Name="ListBox" Height="260" SelectionChanged="ListBox_SelectionChanged">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Vertical">
<TextBlock Height="30" Text="{Binding ProductENname,Converter={StaticResource BinaryConventer}}" Width="100"></TextBlock>
<Image x:Name="Image" Height="100" Width="100" Source="{Binding ImageBinary, Converter={StaticResource BinaryConventer}}"></Image>
<!--<Image x:Name="Image" Height="100" Width="100" Source="{Binding ProductENname}"></Image>-->
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</StackPanel>
</Grid>
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步