Hi All,
I'm trying to display images stored in MSSql Db as varbinary, when execute my code it just hangs itself
Code is as follows...
XAML
<Window x:Class="ImageList.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:ImageList"
Title="Window1" Height="331" Width="575">
<Window.Resources>
<local:BinaryImageConverter x:Key="imgConverter" />
</Window.Resources>
<StackPanel>
<ListBox Name="lstImages" ItemsSource="{Binding}" >
<ListBox.Resources>
<DataTemplate x:Key="data">
<Image Source="{Binding Path=ImageData, Converter={StaticResource imgConverter}}"/>
</DataTemplate>
</ListBox.Resources>
</ListBox>
<Button Height="25" Name="button1" Click="button1_Click">Button</Button>
<Button Height="25" Name="button2" Click="button2_Click">Load Images</Button>
</StackPanel>
</Window>
C#
{
private void button2_Click(object sender, RoutedEventArgs e)
{
ImageDataContext db = new ImageDataContext();
var data = db.ImageDetails.Select(p => p.ImageData).ToList();
lstImages.ItemsSource = data;
}
}
public class BinaryImageConverter : IValueConverter
{
object IValueConverter.Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if (value != null && value is byte[])
{
byte[] ByteArray = value as byte[];
BitmapImage bmp = new BitmapImage();
bmp.BeginInit();
bmp.StreamSource = new MemoryStream(ByteArray);
bmp.EndInit();
return bmp;
}
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.");
}
}