如何在wpf中从数据库中显示数据网格中的图像

如何在wpf中从数据库中显示数据网格中的图像

本文介绍了如何在wpf中从数据库中显示数据网格中的图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的朋友们





我有一个与我的问题相关的问题。



我想在数据网格中显示数据库中的图像(图像以二进制格式保存)在wpf应用程序中。



提前谢谢

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

推荐答案


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...

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

datagrid1.items.add(imagehere);





或者您可以使用视图模型视图方法并绑定图像,基本上硬件部分已完成,您现在拥有图像。



以上方法是我在这里找到的方法:

[]



还有其他方法,但这种方式适用于Silverlight,为了降低内存消耗,我猜测你可能正在使用silverlight。



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.


这篇关于如何在wpf中从数据库中显示数据网格中的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 01:25