none
請問,silverlight 可通過將數據庫中的Binary轉換成BitmapImage吧? RRS feed

  • 问题

  • HI,ALL

    請問,silverlight 可通過將數據庫中的Binary轉換成BitmapImage吧?可否提供些依據?

       

    2010年8月2日 7:05

答案

  • HI,ALL

    还是由我自己来回复吧:经过一天的研究,总算发现其实很简单的,代码如下:

    只要实现IValueConverter接口就行了:

     public class BinaryToImageConverter : IValueConverter
      {
    
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
          //throw new NotImplementedException();
          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;
          }
          else
            return null;
        }
    
        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
          throw new NotImplementedException();
        }
      }
    

     前台:

     <Grid x:Name="LayoutRoot" Background="White">
        <Grid.Resources>
          <local:BinaryToImageConverter x:Key="BinaryConventer"></local: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>
    
    我用的是用转换格式的方式来实现的,但有图片大小的约束,如果加载大图片会出现"致命错误"~不知道为什么.
    2010年8月2日 12:26

全部回复

  •  

    HI,ALL

    我想補充下問題:數據庫中有一個表的字段類型是Image,專門用來存儲圖片用的.

    2010年8月2日 7:24
  • HI,ALL

    还是由我自己来回复吧:经过一天的研究,总算发现其实很简单的,代码如下:

    只要实现IValueConverter接口就行了:

     public class BinaryToImageConverter : IValueConverter
      {
    
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
          //throw new NotImplementedException();
          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;
          }
          else
            return null;
        }
    
        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
          throw new NotImplementedException();
        }
      }
    

     前台:

     <Grid x:Name="LayoutRoot" Background="White">
        <Grid.Resources>
          <local:BinaryToImageConverter x:Key="BinaryConventer"></local: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>
    
    我用的是用转换格式的方式来实现的,但有图片大小的约束,如果加载大图片会出现"致命错误"~不知道为什么.
    2010年8月2日 12:26