Click here to Skip to main content
15,891,204 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Dear Friends


I have a question related to my question.

I want to show the image in datagrid from database (Image saved in binary format) in wpf application.

Thanks in advance
Posted

Plenty of examples are given on Google[^].
But see this[^]

-KR
 
Share this answer
 
Comments
. _Puneet Sharma 20-Feb-14 6:06am    
http://www.codeproject.com/Articles/730713/Show-Images-in-Wpf-Datagrid-from-Database
C#
private Image CreateThumbnailImage(Stream stream, int width)

{
    BitmapImage bi = new BitmapImage();
    bi.SetSource(stream);
 
    double cx = width;
    double cy = bi.PixelHeight * (cx / bi.PixelWidth);
 
    Image image = new Image();
    image.Source = bi;
 
    WriteableBitmap wb1 = new WriteableBitmap((int)cx, (int)cy);
    ScaleTransform transform = new ScaleTransform();
    transform.ScaleX = cx / bi.PixelWidth;
    transform.ScaleY = cy / bi.PixelHeight;
    wb1.Render(image, transform);
    wb1.Invalidate();
 
    WriteableBitmap wb2 = new WriteableBitmap((int)cx, (int)cy);
    for (int i = 0; i < wb2.Pixels.Length; i++)
        wb2.Pixels[i] = wb1.Pixels[i];
    wb2.Invalidate();
 
    Image thumbnail = new Image();
    thumbnail.Width = cx;
    thumbnail.Height = cy;
    thumbnail.Source = wb2;
    return thumbnail;
} 


Put that somewhere, then...

C#
byte[] bytes = null;
//bytes you got from database
//bytes = something
	//200 or whatever you want the width to be.
CreateThumbnailImage(new System.IO.MemoryStream(bytes), 200));


maybe then something like this

C#
datagrid1.items.add(imagehere);


Or you can use a view model view approach and bind the image, basically the hard part is done you now have the image.

The above method is something I found here:
http://www.wintellect.com/blogs/jprosise/silverlight-s-big-image-problem-and-what-you-can-do-about-it[^]

There are other ways but this way is geared towards silverlight, for lower memory consumption, I took a wild guess that you are probably using silverlight.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900