首页 新闻 会员 周边 捐助

如何将数据库中的图片取出

0
悬赏园豆:50 [已解决问题] 解决于 2015-12-12 11:45

已经实现将图片转化为2进制字符串存入数据库的表中。如何取出该图片并显示在winform窗体的picturebox中?

将图片转化为2进制的代码如下:

 MemoryStream ms = new MemoryStream();
            picBoxHead.Image.Save("a");
            FileStream f1 = new FileStream("a", FileMode.Open, FileAccess.Read);
            BinaryReader b1 = new BinaryReader(f1);
            byte[] img = b1.ReadBytes((int)f1.Length);
            b1.Close();
            f1.Close();
            File.Delete("a");                          
            SqlConnection conn = db.Camcon();
            conn.Open();
            string sql = string.Format("insert into VX_Sign(ID,性别,个性签名,出生日期,联系方式,地区,教育程度,密码, 头像) values('{0}','{1}','{2}','{3}','{4}','{5}','{6}','{7}','{8}')", id, sex1, sign, data, tel, area, edu, password,img);
            SqlCommand cmd = new SqlCommand(sql,conn);
            cmd.ExecuteNonQuery();
            conn.Close();

 

如何实现从数据库取出?

我不是zjz的主页 我不是zjz | 初学一级 | 园豆:13
提问于:2015-12-07 19:25
< >
分享
最佳答案
0
using System;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.IO;
using System.Windows.Forms;

namespace MyApp
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            this.pictureBox1.Image = Image.FromFile(@"D:\Firen.jpg");
        }

        //保存图像到数据库
        private void btnSaveImage_Click(object sender, EventArgs e)
        {
            string connStr = @"Data Source=.\sqlexpress;Initial Catalog=TestDb;Integrated Security=True";
            using (SqlConnection conn = new SqlConnection(connStr))
            {
                using (SqlCommand cmd = conn.CreateCommand())
                {
                    cmd.CommandText = "INSERT INTO [dbo].[ImageTable]([ImageId],[ImageBytes]) VALUES (@ImageId,@ImageBytes)";
                    cmd.CommandType = CommandType.Text;

                    SqlParameter parmImageId = new SqlParameter("@ImageId", SqlDbType.Int);
                    parmImageId.Value = 1;
                    cmd.Parameters.Add(parmImageId);

                    SqlParameter parmImageBytes = new SqlParameter("@ImageBytes", SqlDbType.VarBinary);
                    using (MemoryStream ms = new MemoryStream())
                    {
                        pictureBox1.Image.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
                        parmImageBytes.Value = ms.GetBuffer();
                    }
                    cmd.Parameters.Add(parmImageBytes);

                    conn.Open();
                    cmd.ExecuteNonQuery();
                    conn.Close();
                }
            }
        }

        //加载图像
        private void btnLoadImage_Click(object sender, EventArgs e)
        {
            string connStr = @"Data Source=.\sqlexpress;Initial Catalog=TestDb;Integrated Security=True";
            using (SqlConnection conn = new SqlConnection(connStr))
            {
                using (SqlCommand cmd = conn.CreateCommand())
                {
                    cmd.CommandText = "SELECT [ImageBytes] FROM [dbo].[ImageTable] WHERE [ImageId]=@ImageId";
                    cmd.CommandType = CommandType.Text;

                    SqlParameter parmImageId = new SqlParameter("@ImageId", SqlDbType.Int);
                    parmImageId.Value = 1;
                    cmd.Parameters.Add(parmImageId);

                    conn.Open();
                    byte[] imageBytes = (byte[])cmd.ExecuteScalar();
                    using (MemoryStream ms = new MemoryStream(imageBytes))
                    {
                        pictureBox1.Image = Image.FromStream(ms);
                    }
                    conn.Close();
                }
            }
        }
    }
}
收获园豆:50
Firen | 大侠五级 |园豆:5385 | 2015-12-08 09:10

表结构

Firen | 园豆:5385 (大侠五级) | 2015-12-08 09:12

非常感谢,参考了你的代码后我感觉自己写的方法有很大的不足,进行了修改。看来我还有很多地方需要好好研究!

我不是zjz | 园豆:13 (初学一级) | 2015-12-08 21:22
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册