将图像作为流存储在数据库中 - WPF [英] Storing image as stream in database - WPF

查看:44
本文介绍了将图像作为流存储在数据库中 - WPF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将图像保存为 SQL 数据库中的流,我在 Silverlight 中这样做:

I want to save image as stream in SQL database, I have doing that in silverlight as:

 OpenFileDialog fd = new OpenFileDialog();
 if (fd.ShowDialog() == true)
 {
     Stream stream = fd.File.OpenRead();
     byte[] binaryImage = new byte[stream.Length];
     stream.Read(binaryImage, 0, (int)stream.Length);

     BitmapImage bitmapImage = new BitmapImage();
     bitmapImage.SetSource(stream);
     _business.PersonalPhoto = binaryImage;
 }

使用 WPF 此代码不起作用,fd.File 不存在.如何更正此代码以在 WPF 上工作

With WPF this code does not work, fd.File does not exist. How can I correct this code to work on WPF

提前致谢.

推荐答案

终于解决了这个问题,这里是将图像以二进制形式存储在数据库中的代码:

Finally I have solved this, here is the code for store the image in database as binary :

存储图像:

       Microsoft.Win32.OpenFileDialog fd = new Microsoft.Win32.OpenFileDialog();
        if (fd.ShowDialog() == true)
        {
            ILogo.Source = new BitmapImage(new Uri(fd.FileName));

            Stream stream = File.OpenRead(fd.FileName);
            binaryImage = new byte[stream.Length];
            stream.Read(binaryImage, 0, (int)stream.Length);
        }

        _merchantInfo.Logo = binaryImage;
        _context.SaveChanges();

感谢大家帮助我:)

这篇关于将图像作为流存储在数据库中 - WPF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆