zoukankan      html  css  js  c++  java
  • 将图片保存到数据库,并且加载图片

    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>
  • 相关阅读:
    2014 年美国程序员薪资调查
    新加坡移民生活:想出都出不来了!
    mysql命令行参数
    甲骨文创始人埃里森的10大混蛋行为:曾翻微软垃圾堆
    为什么我要称自己为Javascript程序员
    原生JavaScript练习——弹出层
    Leetcode 344 Reverse String 字符串处理
    安装 Autoconf 2.69版
    Leetcode 28 Implement strStr()
    Leetcode 67 Add Binary 大数加法+字符串处理
  • 原文地址:https://www.cnblogs.com/hornet/p/4059210.html
Copyright © 2011-2022 走看看