Solved

How I can bind Images to datagrid In wpf

Posted on 2010-09-17
7
1,995 Views
Last Modified: 2013-11-12
I use wpf datagrid  which gets source from database. I store Images as byte array in database. What can i do to see the images from database on datagrid ?
Thanks In advance
0
Comment
Question by:Zherquen
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
7 Comments
 
LVL 29

Expert Comment

by:Gautham Janardhan
ID: 33699853
you can create a converter which will convert the byte array to an image source
0
 
LVL 29

Accepted Solution

by:
Gautham Janardhan earned 500 total points
ID: 33699881
something like this
public class ImageConverter: IValueConverter

{

#region IValueConverter Members

public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)

{

BitmapImage img = new BitmapImage();

using (MemoryStream stream = new MemoryStream(value as byte[])) 

{

      img.SetSource(stream);

} 

return img;

}


<Image Source="{Binding YourByteArray, Converter={StaticResource ImageConverter}"/>

Open in new window

0
 
LVL 29

Expert Comment

by:Kumaraswamy R
ID: 33699901
0
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 

Author Comment

by:Zherquen
ID: 33699902
Hello gauthampj,
I have found many converter in c# but dont know how to use them in xaml code.
For Example :
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)

{
Byte[] bytes = value as
Byte[];


MemoryStream mem = new
MemoryStream(bytes);

mem.Write(bytes, 0, bytes.Length);

mem.Seek(0, SeekOrigin.Begin);


BitmapImage image = new
BitmapImage();

 

image.BeginInit();

image.DecodePixelWidth = 60;

image.CacheOption = BitmapCacheOption.OnLoad;

image.StreamSource = mem;

image.EndInit();


return image;

 

}

 


public
object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)

{

     return
null;

}

How can i relate the code with datagrid in xaml code?
0
 
LVL 29

Expert Comment

by:Gautham Janardhan
ID: 33699973
you need to add it as a resource and then refer it.. see sample below
0
 
LVL 29

Expert Comment

by:Gautham Janardhan
ID: 33700003
oops forgot to attach
 namespace TestWpf
 {
     public class ImageConverter : IValueConverter
    {
        #region IValueConverter Members
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            BitmapImage img = new BitmapImage();
            using (MemoryStream stream = new MemoryStream(value as byte[]))
            {
                img.StreamSource = stream;
            }

            return img;

        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }

        #endregion
    }
}

/*XAML*/

    <Grid xmlns:local="clr-namespace:TestWpf">
        <Grid.Resources>
            <local:ImageConverter x:Key="Converter"/>
        </Grid.Resources>
        <Image Source="{Binding YourByteArray, Converter={StaticResource Converter}"/>
    </Grid>

Open in new window

0
 
LVL 29

Expert Comment

by:Gautham Janardhan
ID: 33700012
also don't forgot to replace the name space xmlns:local="clr-namespace:TestWpf" with the one where you put the converter
0

Featured Post

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
How to read text with RegEx... 2 33
C# class library debugging - Breakpoint will not hit. 8 30
C# Linq Select From List 3 43
C# XML Get Values 4 33
We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

735 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question