C# WinForm 实现增删改查等功能(Access版) 系列之七-图片处理

来源:互联网 发布:linux系统工程师职责 编辑:程序博客网 时间:2024/05/16 10:04

C# WinForm 实现增删改查等功能(Access) 系列之七

图片处理模块设计与实现

        /// <summary>

        /// 获得上传图片的图片名称

        /// </summary>

        /// <param name="txtName"></param>

        /// <returns></returns>

        public string getStrOfPictureBox(string txtName)

        {

            string strpictrue = "";

            string name = txtName;

            OleDbConnection conn = GetConnection();

            string sqlText = "select 图片 from  MResume where 姓名=@name order by id asc";

            OleDbCommand cmd = new OleDbCommand(sqlText, conn);

            cmd.Parameters.AddWithValue("@name", name);

            try

            {

                conn.Open();

                OleDbDataReader reader = cmd.ExecuteReader();

                if (reader.Read())

                {

                    if (!reader[0].ToString().Equals("0") && reader[0].ToString()!="" && reader[0].ToString()!=null)

                    {

                        strpictrue = reader[0].ToString();

                    }

                    else

                    {

                        strpictrue = Application.StartupPath + "//Image//nophoto.jpg";

                    }

                }

                /*DataTable dt = new DataTable();

                 OleDbDataAdapter oda = new OleDbDataAdapter(sqlText, conn);

                 oda.Fill(dt);

                 dataGridView1.DataSource = dt;

                 */

            }

            catch (Exception ex)

            {

                MessageBox.Show(ex.ToString());

            }

            finally

            {

                conn.Close();

            }

            return strpictrue;

        }

打开对话框,选择图片,上传图片

/// <summary>

        /// UploadImages

        /// </summary>

        /// <param name="sender"></param>

        /// <param name="e"></param>

        private void button1_Click(object sender, EventArgs e)

        {

          //本方法来源于Spirit's Home http://www.7788sky.cn/,原文地址http://www.7788sky.cn/post/csharp_winform_upload_image.html

         // openFileDialog1.Filter = "All files (*.*)|*.*|jpg files (*.jpg)|*.jpg";

            openFileDialog1.Filter = "jpg files (*.jpg)|*.jpg";

            DialogResult dr = openFileDialog1.ShowDialog();

            if (dr == DialogResult.OK)

            {

              string  image = openFileDialog1.FileName;

              string  picpath = openFileDialog1.FileName;

              DateTime dt1 = new DateTime(1970, 1, 1);

              TimeSpan ts = DateTime.Now - dt1;

              timeStamp = (long)ts.TotalMilliseconds;

              //string id = DateTime.Now.Millisecond.ToString();

              picPerson.Image = Image.FromFile(image);

              File.Copy(openFileDialog1.FileName, Application.StartupPath + "//Image//" + timeStamp.ToString() + ".jpg");

              lblImage.Text = timeStamp.ToString();    //设置图片字符串

            }

        }