数据库存储图片

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using System.Data.SqlClient;

namespace WindowsFormsApplication1
{
    public partial class cun_img : Form
    {
        public cun_img()
        {
            InitializeComponent();
        }
        private void button1_Click(object sender, EventArgs e)//-----------------------打开
        {
            DialogResult dr = openFileDialog1.ShowDialog();
            if (dr==DialogResult.OK)
            {
                FileStream fs = new FileStream(openFileDialog1.FileName, FileMode.Open, FileAccess.Read);//打开读取文件 读到fs这个流里面
                Image img = System.Drawing.Bitmap.FromStream(fs);//fs从硬盘上读取的图片通过drawing.bitmap位图绘制出来就是image类
                pictureBox1.Image = img;//指定
            }
        }

        private void button2_Click(object sender, EventArgs e)//---------------存入到数据库
        {
            DialogResult dr = openFileDialog1.ShowDialog();
            if (dr==DialogResult.OK)
            {
                //文件流
                FileStream fs = new FileStream(openFileDialog1.FileName, FileMode.Open, FileAccess.Read);
                BinaryReader br = new BinaryReader(fs);//二进制读取器
                byte[] buffer = br.ReadBytes(int.Parse(fs.Length.ToString()));

                //连接数据库
                SqlConnection conn = new SqlConnection("server=.;database=tupian;user=sa;pwd=123");
                SqlCommand cmd = conn.CreateCommand();
                cmd.CommandText = "insert into imatable values(@buffer)";
                cmd.Parameters.Add("@buffer", buffer);
                conn.Open();
                cmd.ExecuteNonQuery();
                cmd.Dispose();
                conn.Close();
            }
        }

        private void button3_Click(object sender, EventArgs e)//-----------------------从数据库读取
        {
            SqlConnection conn = new SqlConnection("server=.;database=tupian;user=sa;pwd=123");
            SqlCommand cmd = conn.CreateCommand();
            cmd.CommandText = "select *from imatable";
            conn.Open();
            SqlDataReader dr = cmd.ExecuteReader();
            byte[] buffer = null;
            if (dr.Read())
            {
                buffer = (byte[])dr["img"];//转换为byte数组型
            }
            cmd.Dispose();
            conn.Close();
            //将二进制数据buffer显示为图片
            MemoryStream ms = new MemoryStream(buffer);//构建对象
            Image img = System.Drawing.Image.FromStream(ms);
            pictureBox1.Image = img;
        }
    }
}

  

转载于:https://www.cnblogs.com/Mr-xue/p/4607205.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值