C# winform 上传图片,显示图片和gridview中添加按钮

上传图片,显示图片
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.IO;

namespace WAJS
{
    public partial class F_HJWF_UploadImg : Form
    {
        string djbh = "";

        public F_HJWF_UploadImg(string _djbh)
        {
            djbh = _djbh;
            InitializeComponent();
        }

        

        private void button1_Click(object sender, EventArgs e)
        {
            openFileDialog1.InitialDirectory = "C:\\";
            openFileDialog1.Filter = "图片文件 (*.jpg)|*.jpg";
            openFileDialog1.FilterIndex = 1;
            openFileDialog1.RestoreDirectory = true;
            openFileDialog1.Multiselect = true;
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                textBox1.Text = openFileDialog1.FileName;
            }
        }
        //上传按钮事件处理  
        private void button2_Click(object sender, EventArgs e)
        {
            Save(PhotoToArray(textBox1.Text.ToString()));
            show();
        }
        //将图片信息转换成二进制信息  
        private byte[] PhotoToArray(string path)
        {
            FileStream stream = new FileStream(path, FileMode.Open, FileAccess.Read);
            byte[] bufferPhoto = new byte[stream.Length];
            stream.Read(bufferPhoto, 0, Convert.ToInt32(stream.Length));
            stream.Flush();
            stream.Close();
            return bufferPhoto;
        }
        //把二进制的图片插到数据库  
        private void Save(byte[] image)
        {
            string sql = "UPDATE HJWF SET TPSC = (@TPSC) where id = "+djbh;
            SqlParameter param = new SqlParameter();
            param = new SqlParameter("@TPSC", SqlDbType.Image);
            param.Value = image;
            SqlConnection SaveDataConnect = new SqlConnection(AppTool.ConStr);
            SqlCommand commd = new SqlCommand(sql, SaveDataConnect);
            commd.Parameters.Add(param);
            try
            {
                SaveDataConnect.Open();
                commd.ExecuteNonQuery();
                MessageBox.Show("您已经把图片成功的更新到数据库!");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                SaveDataConnect.Close();
            }
        }
        //显示图片按钮事件处理  
        private void show()
        {
            string strSQL = "Select   [TPSC]   From   [HJWF]   Where   id = "+djbh;
            SqlParameter param = new SqlParameter();
            //param = new SqlParameter("@photo", SqlDbType.Int);
            //param.Value = comboBox1.Text.ToString();
            SqlConnection SaveDataConnect = new SqlConnection(AppTool.ConStr);
            SqlCommand cmd = new SqlCommand(strSQL, SaveDataConnect);
            //cmd.Parameters.Add(param);
            SaveDataConnect.Open();
            System.Data.SqlClient.SqlDataReader reader = cmd.ExecuteReader();
            try
            {
                reader.Read();
                if (string.IsNullOrEmpty(reader["TPSC"].ToString())) return;
                MemoryStream ms = new MemoryStream((byte[])reader["TPSC"]);
                System.Drawing.Image image = System.Drawing.Image.FromStream(ms, true);
                this.pictureBox1.Image = image;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                SaveDataConnect.Close();
            }
        }

        private void F_HJWF_UploadImg_Load(object sender, EventArgs e)
        {
            show();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            Close();
        }
        //private void Form1_Load(object sender, EventArgs e)
        //{
        //    // TODO: 这行代码将数据加载到表“myPhotoDataSet.Photo”中。您可以根据需要移动或移除它。  
        //    this.photoTableAdapter.Fill(this.myPhotoDataSet.Photo);
        //}  
    }
}


小问题

能让在picturebox中的图片大小随picturebox的大小变化吗?

把PictureBox的SizeMode设为   StretchImage   就可以让图片随PictureBox   Resize了。


gridview中添加按钮 

        private void dataGridView2_CellEnter(object sender, DataGridViewCellEventArgs e)
        {
            this.dataGridView2.Controls.Clear();//移除所有控件 
            if (e.ColumnIndex.Equals(this.dataGridView2.Columns["上传图片"].Index))
            //判断单元格是否是"Company"列? 
            {
                System.Windows.Forms.Button btn = new System.Windows.Forms.Button();//创建Buttonbtn 
                btn.Text = "上传";//设置button文字 
                btn.Font = new System.Drawing.Font("Arial", 7);//设置文字格式 
                btn.Visible = true;//设置控件允许显示 

                btn.Width = this.dataGridView2.GetCellDisplayRectangle(e.ColumnIndex,
                                e.RowIndex, true).Width;//获取单元格高并设置为btn的宽 
                btn.Height = this.dataGridView2.GetCellDisplayRectangle(e.ColumnIndex,
                                e.RowIndex, true).Height;//获取单元格高并设置为btn的高 

                
                string DJBH = dataGridView2.Rows[e.RowIndex].Cells["ID"].Value.ToString();
                label31.Text = DJBH;

                btn.Click += new EventHandler(btn_Click);//为btn添加单击事件 

                this.dataGridView2.Controls.Add(btn);//dataGridView2中添加控件btn 

                btn.Location = new System.Drawing.Point(((this.dataGridView2.GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex, true).Right) -
                        (btn.Width)), this.dataGridView2.GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex, true).Y);//设置btn显示位置 
            }
        }

        void btn_Click(object sender, EventArgs e)
        {
            

            F_HJWF_UploadImg f = new F_HJWF_UploadImg(label31.Text);
            f.ShowDialog();
        }


C# winform 上传图片,显示图片和gridview中添加按钮,布布扣,bubuko.com

C# winform 上传图片,显示图片和gridview中添加按钮

上一篇:谷歌开源项目Chromium的源码获取与项目构建(Win7+vs10/vs13)


下一篇:PS后期打造超有质感的商业人像欣赏