1. 程式人生 > >C# winform 上傳圖片,顯示圖片和gridview中新增按鈕

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();
        }