Binding to an Image Byte[] in WPF

Posted Thursday, July 26, 2007 12:21 AM by cromwellryan

I'm working on a WPF app for my wife who coaches our local high school track team.  One of the features she wanted was to have some images for different players and teams and, religious debate aside, I have chosen to save these in SQL Server 2005.  I'm using NHibernate for all my data access which has been a great experience (my first).  Impressively, NHibernate supports SQL Server Image columns via the inherent Byte[]  type (See Section 5.1.9 of the docs).  This made adding the Image to my classes very easy. 

Here's a snippet of my class with the corresponding NHibernate Mapping Xml File:

/* Class */
[Serializable]
public class Organization : INotifyPropertyChanged
{
//...

public byte[] Image
{
get { return _image; }
set
{
_image = value;
OnPropertyChanged("Image");
}
}

//...
}
<!-- Mapping -->
<
hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" default-lazy="false">
<
class name="Cromwell.MyApp.Organization, Cromwell.MyApp"
table="dbo.Organization">
<
id name="Id" type="Int32" column="Id" unsaved-value="0">
<
generator class="native" />
</
id>
<
many-to-one name="Type"
class="Cromwell.MyApp.OrganizationType, Cromwell.MyApp"
column="Type"
not-null="true"/>
<
property name="Name" type="String" length="255" column="Name" not-null="true"/>
<
property name="Address1" type="String" length="255" column="Address1" not-null="false"/>
<
property name="Address2" type="String" length="255" column="Address2" not-null="false"/>
<
property name="City" type="String" length="255" column="City" not-null="false"/>
<
property name="State" type="String" length="2" column="State" not-null="false"/>
<
property name="PostalCode" type="String" length="7" column="PostalCode" not-null="false"/>
<
property name="Image" type="Byte[]" column="Image" not-null="false" />
</
class>
</
hibernate-mapping>

The key to binding this Image property on my WPF forms is the Binding Converter property.  This property allows you to specify any class which implements System.Windows.Data.IValueConverter to act as an intermediary between your bind data and WPF.  In my case, the goal was to turn a byte[] property, Image, into a BitmapImage which the an <Image /> control/element can display. 

Here's what I came up with:

class BinaryImageConverter : 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." );
}
}

Here's how I used it in my WPF Page:

<Page x:Class="Cromwell.MyApp.EditOrganization"
...
xmlns:converts="clr-namespace:Cromwell.MyApp.Converters"
>
<
Page.Resources>
<
converts:BinaryImageConverter x:Key="imgConverter" />
</
Page.Resources>
<
Grid>
<!--
Image Control -->
<
Image Source="{Binding Path=Image,
Converter={StaticResource imgConverter}}
"
Stretch="UniformToFill"
StretchDirection="Both">
<
Image.BitmapEffect>
<
DropShadowBitmapEffect Color="Black" />
</
Image.BitmapEffect>
</
Image>
</
Grid>
</
Page>

I'm going to have to spend some time in the WPF profiler, but this is a big step.  Pretty cool how simple it was and very clean.

Filed under: ,

Comments

# re: Binding to an Image Byte[] in WPF

Wednesday, February 13, 2008 1:31 AM by Atul Gupta

This is good. Thanks

# re: Binding to an Image Byte[] in WPF

Wednesday, September 03, 2008 1:52 PM by Kilksitnese

colt revolver serial numbers beaumont adams revolver  <a href=http://owgaveyn.00freehost.com>best 223 rifle</a>  22 auto magnum rifle semi action lever rifle

# re: Binding to an Image Byte[] in WPF

Sunday, October 12, 2008 7:00 AM by Almosemnere

Hello.

:) Watched attentively by big sisters Maud and Leah the newest member of the Norwegian royal family has been captured in homely shots used by proud parents Princess Martha Louise of Norway and her husband Ari Behn to introduce her to the world.

 Bye.

# re: Binding to an Image Byte[] in WPF

Monday, October 13, 2008 12:12 AM by sampidwinia

Hi!

My name is Jessika!

# re: Binding to an Image Byte[] in WPF

Wednesday, December 03, 2008 10:41 PM by hisa act of depression

depression era price depression clinical research depression statistic  <a href=helpdepression.itrello.com]beck depression inventory</a> cake depression glass pink plate bipolar depression group support depression free for life article depression great hamilton rating scale for depression

# re: Binding to an Image Byte[] in WPF

Thursday, December 04, 2008 10:11 AM by kaxy jeff magnum tour

beretta jubilee ballistic chart remington browning double automatic <a href=http://zcvqsid.gigazu.net>history winchester 1873</a>375 winchester brass 4 magnum pmp sports toto 50 cal browning

# re: Binding to an Image Byte[] in WPF

Thursday, December 04, 2008 10:39 PM by escandyDync

sorry blame it on me akon dont matter rock on dj tom rock on favourite worst nightmare arctic monkeys balaclava   <a href=http://spyefco.4sql.net>one chance paul potts music of the night</a> euro xclusive 07 35 various artists only getting better coco colbie caillat capri drop it like its hot snoop dogg drop it like its hot instrumental

Leave a Comment

(required) 
(required) 
(optional)
(required)