首页 > 代码库 > 将图片保存到数据库,并且加载图片
将图片保存到数据库,并且加载图片
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>
将图片保存到数据库,并且加载图片
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。