码迷,mamicode.com
首页 > Windows程序 > 详细

winform 导入导出图片

时间:2015-06-08 16:35:00      阅读:133      评论:0      收藏:0      [点我收藏+]

标签:

private void button1_Click(object sender, EventArgs e)
        {
            OpenFileDialog op = new OpenFileDialog();
            if (op.ShowDialog()==DialogResult.OK)
            {
                FileStream fs = new FileStream(op.FileName,FileMode.Open);
                Image img = System.Drawing.Bitmap.FromStream(fs);
                pictureBox1.Image = img;
                fs.Close();
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            OpenFileDialog op = new OpenFileDialog();
            if (op.ShowDialog() == DialogResult.OK)
            {
                FileStream fs = new FileStream(op.FileName, FileMode.Open);
                BinaryReader dr = new BinaryReader(fs);
                byte[] by=new byte[fs.Length];
               by= dr.ReadBytes((int)fs.Length);
               SqlConnection conn = new SqlConnection("server=.;database=students_information;user=sa;pwd=123");
               conn.Open();
               SqlCommand cmd = conn.CreateCommand();
               cmd.CommandText = "insert into img(img) values(@img)";
               cmd.Parameters.Add(new SqlParameter("@img",by));
               cmd.ExecuteNonQuery();
               cmd.Dispose();
               conn.Close();
               MessageBox.Show("添加成功");
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            byte[] by = null;
            SqlConnection conn = new SqlConnection("server=.;database=students_information;user=sa;pwd=123");
            conn.Open();
            SqlCommand cmd = conn.CreateCommand();
            cmd.CommandText = "select * from img";
            SqlDataReader dr = cmd.ExecuteReader();
            if (dr.Read())
            {
                by = (byte[])dr["img"];
            }
           
            conn.Close();

            MemoryStream ms = new MemoryStream(by);
            Image img = System.Drawing.Bitmap.FromStream(ms);
            pictureBox1.Image = img;
            ms.Close();

        }

 

winform 导入导出图片

标签:

原文地址:http://www.cnblogs.com/lushixiong/p/4561159.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!