码迷,mamicode.com
首页 > 数据库 > 详细

将图片保存到数据库,并且加载图片

时间:2014-10-29 14:27:05      阅读:227      评论:0      收藏:0      [点我收藏+]

标签:style   blog   io   color   os   ar   sp   文件   数据   

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>

将图片保存到数据库,并且加载图片

标签:style   blog   io   color   os   ar   sp   文件   数据   

原文地址:http://www.cnblogs.com/hornet/p/4059210.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!