WinForm把图片以二进制的方式存入到数据库,并将存入数据库的二进制图片读出显示到PictureBox中

时间:2021-01-22 21:43:45

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient; --连接数据库必用的命名空间---------------Remember------------------------------------
using System.IO;             --连接数据库必用的命名空间---------------Remember------------------------------------     

namespace _11_19Happy
{
    public partial class TestForm : Form
    {
        public Form3()
        {
            InitializeComponent();
        }
        OpenFileDialog ofd = null;
        string connectionString = "Data Source=.;Initial Catalog=UserInfo;Integrated Security=true";
        //加载图片到PictureBox中
        private void btnOpenAndLoad_Click(object sender, EventArgs e)
        {
            ofd = new OpenFileDialog();
            if (ofd.ShowDialog() == DialogResult.OK)
            {
                pictureBox1.ImageLocation = ofd.FileName;
                MessageBox.Show(ofd.FileName);
            }
        }

        //保存用户加载到PictureBox中的图片到数据库
        private void btnSave_Click(object sender, EventArgs e)
        {
            //-------标题(实现图片以二进制方式存入数据库,并从数据库中读出并显示在PictureBox图片框中)------------------
            //-----UserDB表里面的内容----- 说明:一.UserDB表在UserInfo数据库里面;二.进行简单的测试设计的简单数据库
            //UserName Password EMail              UserImage
            //Hello     123   1649387955@qq.com  <二进制数据>
            //How     123     11111111@qq.com
            //John     123   NULL
            //Sla     123 
            //------------界面信息----------
            //PictureBox(作用:显示图片信息)
            //Button(作用加载图片到PictureBox) 事件: private void btnOpenAndLoad_Click(object sender, EventArgs e)
            //Button(保存加载到PictureBox控件上的以二进制的方式保存到数据库) 事件: private void btnSave_Click(object sender, EventArgs e)
            //Button(将数据库中的图片二进制读出来显示到PictureBox控件上) 事件:private void btnReadPic_Click(object sender, EventArgs e)
            //-----------------------------
            FileStream fs = new FileStream(ofd.FileName,FileMode.Open);
            byte[] picContent=new byte[fs.Length];
            fs.Read(picContent, 0, picContent.Length);           
            SqlConnection con = new SqlConnection(connectionString);
            SqlCommand cmd = new SqlCommand();
            cmd.CommandText = "update UserDB set UserImage=@Image where UserName='Hello'";
            cmd.Parameters.Add("@Image", SqlDbType.Image, (int)fs.Length, "UserImage");
            cmd.Connection = con;           
            cmd.Parameters["@Image"].Value = picContent;
            con.Open();
            int result = cmd.ExecuteNonQuery();
            fs.Close();
            fs.Dispose();
            con.Close();
            con.Dispose();
            if (result > 0)
            {
                MessageBox.Show("插入数据成功!");
            }
            else
            {
                MessageBox.Show("插入数据失败!");
            }
        }

        //从数据库中读取二进制到PictureBox显示图片
        private void btnReadPic_Click(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection(connectionString);
            SqlCommand cmd = new SqlCommand("select UserImage from UserDB where UserName='Hello'",con);
            con.Open();
            SqlDataReader sda = cmd.ExecuteReader();
            sda .Read ();
            byte[] picContent = (byte[])sda[0];
            sda.Close();
            if (picContent != null)
            {
                MemoryStream memStr = new MemoryStream(picContent,true);
                Image myImage = Image.FromStream(memStr);
                pictureBox1.Image = myImage;
                memStr.Close();
                memStr.Dispose();
                con.Close();
                con.Dispose();

                pictureBox1.Refresh();
            }
            else
            {
                MessageBox.Show("没有图片");
            }
        }   
    }
}