簡體   English   中英

如何將Image控件綁定到以VarBinary類型保存在數據庫中的Image

[英]How to bind an Image control to Image that save in Database in VarBinary type

我想在XAML中綁定一個以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.");
    }
}

而且:

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.

如果從數據庫獲取字節數組,則無需將其轉換為圖像或位圖圖像...您可以將圖像的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;
        }
    }
}

希望這個能對您有所幫助...

這只能使用自定義轉換器來實現。 看看這個就如何貫徹執行“圖像”部分和細節在這里有關創建轉換器的詳細信息。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM