2011-07-27 58 views
1

我想要在XAML中綁定保存爲數據庫中varbinary類型的圖像。如何才能做到這一點?如何將圖像控件綁定到以VarBinary類型保存在數據庫中的圖像

例如Northwind DataBase中的圖片字段。

感謝

編輯:) 1

我寫(在Northwind數據庫分類表圖片字段)這個代碼轉換圖片領域,但每一次我得到異常:

class ImageConverter : IValueConverter 
{ 
    object IValueConverter.Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) 
    { 
     if (value == null) { return null; } 

     var image = (System.Drawing.Image)value; 
     var bitmap = new System.Windows.Media.Imaging.BitmapImage(); 
     bitmap.BeginInit(); 
     MemoryStream memoryStream = new MemoryStream(); 
     image.Save(memoryStream, ImageFormat.Bmp); 
     memoryStream.Seek(0, System.IO.SeekOrigin.Begin); 
     bitmap.StreamSource = memoryStream; 
     bitmap.EndInit(); 
     return bitmap; 

    } 

    object IValueConverter.ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) 
    { 
     throw new Exception("The method or operation is not implemented."); 
    } 
} 

And:

class ImageConverter : IValueConverter 
{ 
    object IValueConverter.Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) 
    { 
     if (value != null && value is byte[]) 
     { 
      byte[] bytes = value as byte[]; 
      MemoryStream stream = new MemoryStream(bytes); 
      BitmapImage image = new BitmapImage(); 
      image.BeginInit(); 
      image.StreamSource = stream; 
      image.EndInit(); 
      return image; 
     } 
     return null; 
    } 

    object IValueConverter.ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) 
    { 
     throw new Exception("The method or operation is not implemented."); 
    } 
} 

和例外:

Provide value on 'System.Windows.Markup.StaticResourceHolder' threw an exception. 

回答

4

如果您從數據庫中獲取字節數組,然後無需將其轉換爲圖像或位圖圖像... 您可以將圖像的Source屬性綁定到字節數組。wpf在內部處理字節數組,並將字節數組圖像...

編輯:

如果你仍然想字節數組轉換到這裏位圖圖像這是測試

public BitmapImage ImageFromBytearray(byte[] imageData) 
     { 

      if (imageData == null) 
       return null; 
      MemoryStream strm = new MemoryStream(); 
      strm.Write(imageData, 0, imageData.Length); 
      strm.Position = 0; 
      System.Drawing.Image img = System.Drawing.Image.FromStream(strm); 

      BitmapImage bitmapImage = new BitmapImage(); 
      bitmapImage.BeginInit(); 
      MemoryStream memoryStream = new MemoryStream(); 
      img.Save(memoryStream, System.Drawing.Imaging.ImageFormat.Bmp); 
      memoryStream.Seek(0, SeekOrigin.Begin); 
      bitmapImage.StreamSource = memoryStream; 
      bitmapImage.EndInit(); 

      return bitmapImage; 
     } 

我的方法已裝箱使用上述方法的樣本...

XAML代碼:

<Window x:Class="WpfApplication1.ImageFromByteArray" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     Title="ImageFromByteArray" Height="300" Width="300" Name="Root"> 
    <Grid> 
     <Image Source="{Binding ImageSource,ElementName=Root}" Height="300" Width="300" RenderOptions.BitmapScalingMode="HighQuality"/> 
    </Grid> 
</Window> 

代碼隱藏

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Windows; 
using System.Windows.Controls; 
using System.Windows.Data; 
using System.Windows.Documents; 
using System.Windows.Input; 
using System.Windows.Media; 
using System.Windows.Media.Imaging; 
using System.Windows.Shapes; 
using System.IO; 

namespace WpfApplication1 
{ 
    /// <summary> 
    /// Interaction logic for ImageFromByteArray.xaml 
    /// </summary> 
    public partial class ImageFromByteArray : Window 
    { 


     public byte[] ByteArray 
     { 
      get 
      { 
       return (byte[])GetValue(ByteArrayProperty); 
      } 
      set 
      { 
       SetValue(ByteArrayProperty, value); 
      } 
     } 

     // Using a DependencyProperty as the backing store for ByteArray. This enables animation, styling, binding, etc... 
     public static readonly DependencyProperty ByteArrayProperty = 
      DependencyProperty.Register("ByteArray", typeof(byte[]), typeof(ImageFromByteArray)); 



     public BitmapImage ImageSource 
     { 
      get { return (BitmapImage)GetValue(ImageSourceProperty); } 
      set { SetValue(ImageSourceProperty, value); } 
     } 

     // Using a DependencyProperty as the backing store for ImageSource. This enables animation, styling, binding, etc... 
     public static readonly DependencyProperty ImageSourceProperty = 
      DependencyProperty.Register("ImageSource", typeof(BitmapImage), typeof(ImageFromByteArray), new UIPropertyMetadata(null)); 


     public ImageFromByteArray() 
     { 
      InitializeComponent(); 
      Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog(); 
      if (dlg.ShowDialog().GetValueOrDefault()) 
      { 
       FileStream fs = new FileStream(dlg.FileName, FileMode.Open, FileAccess.Read); 
       ByteArray = new byte[fs.Length]; 
       fs.Read(ByteArray, 0, System.Convert.ToInt32(fs.Length)); 
       fs.Close(); 
       ImageSource = ImageFromBytearray(ByteArray); 
      } 
     } 


     public BitmapImage ImageFromBytearray(byte[] imageData) 
     { 

      if (imageData == null) 
       return null; 
      MemoryStream strm = new MemoryStream(); 
      strm.Write(imageData, 0, imageData.Length); 
      strm.Position = 0; 
      System.Drawing.Image img = System.Drawing.Image.FromStream(strm); 

      BitmapImage bitmapImage = new BitmapImage(); 
      bitmapImage.BeginInit(); 
      MemoryStream memoryStream = new MemoryStream(); 
      img.Save(memoryStream, System.Drawing.Imaging.ImageFormat.Bmp); 
      memoryStream.Seek(0, SeekOrigin.Begin); 
      bitmapImage.StreamSource = memoryStream; 
      bitmapImage.EndInit(); 

      return bitmapImage; 
     } 
    } 
} 

希望這將幫助你...

0

這隻能使用自定義轉換器來實現。有關如何實現「圖像」部分的詳細信息,請參閱this,有關創建轉換器的詳細信息,請參閱here

+0

如果我使用價值轉換器什麼類型我轉換Linq.Binary? – Arian

+0

僅當一個(源或目標)屬性是依賴項屬性時,綁定才起作​​用。如果您有Linq.Binary類型的源屬性,則需要將其轉換爲目標類型。 – kubal5003

相關問題