beginner2k10
beginner2k10

Reputation: 121

How to retrieve images from SQL Server database into WPF form?? (LINQ, Databinding, SQL)

I have a cheapo window where I insert data into a database. My db-columns are the following:

col name(varchar),  
col age (int),  
col photo (image) (all NOT NULL)

Now I want to retrieve the info from the db into my window. I have a listbox where I retrieve the names like this (LINQ query)

public void updateListbox(){
DataClasses1DataContext dc = new DataClasses1DataContext();
var query = from s in dc.tablename select s;

_listBox1.ItemsSource = query.ToList();
}

My xaml code for binding looks like this:

<ListBox ... DisplayMemberPath="Name"/>

so the names are now being displayed in the listbox.

My next step is to display the image/photo of the representative person that is selected in the listbox on my window/form. I did it with the age property in a textblock like this:

<TextBlock ... Text="{Binding ElementName=_listBox1, Path=SelectedItem.Age}" />

but I have no idea how to retrieve the image into my wpf window.

I save the image like this

byte[] image = File.ReadAllBytes(@imagepath);
...
sqlcommandobject.Parameters.Add(new SqlParameter("@Photo", image));
...

by using openFileDialog to load the image(-path) before insterting into the db. Just retrieving is my problem now. I really would like to keep the binding as simple as it is at this moment, but I guess there is no way to retrieve the image by doing something like this:

<Image ... Source="{Binding ElementName=_listBox, Path=SelectedItem.Photo}" />

As the binary data has to be converted back to an image object(?), and I'm lost on how to do that and conbine this with the bindings. Anyone have an idea?

Thanks for reading!

Upvotes: 0

Views: 7274

Answers (2)

Thomas Levesque
Thomas Levesque

Reputation: 292385

You can create a converter to convert the value from byte[] to BitmapSource:

[ValueConversion(typeof(byte[]), typeof(BitmapSource))]
public class ByteArrayToImageConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        byte[] bytes = (byte[]) value;
        using (MemoryStream ms = new MemoryStream(bytes))
        {
            BitmapImage image = new BitmapImage();
            image.BeginInit();
            image.StreamSource = ms;
            image.EndInit();
            return image;
        }
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        BitmapSource image = (BitmapSource) value;
        BitmapEncoder encoder = new PngBitmapEncoder();
        encoder.Frames.Add(BitmapFrame.Create(image));
        using (MemoryStream ms = new MemoryStream())
        {
            encoder.Save(ms);
            return ms.ToArray();
        }
    }
}

(The ConvertBack method encodes to PNG, you might want to change that depending on your needs. Or you might not need ConvertBack at all...)

Upvotes: 2

Waleed A.K.
Waleed A.K.

Reputation: 1656

Bind the image to ObjectDataProvider that convert the parameter to image.

Upvotes: -1

Related Questions