//C#上传图片的代码

来源:互联网 发布:淘宝女运动套装 编辑:程序博客网 时间:2024/04/29 13:17

 

  private void btnPhoto_Click(object sender, EventArgs e)
        {
            string temp;

            string housr = DateTime.Now.Hour.ToString();
            string min = DateTime.Now.Minute.ToString();
            string second = DateTime.Now.Second.ToString();

            temp = housr + min + second ;

            opfdPhoto.InitialDirectory = Application.StartupPath;
            opfdPhoto.FileName = "";

            this.opfdPhoto.Title = "选择相片";
            opfdPhoto.Filter = "图片文件(*.jpg;*.bmp;*.gif)|*.jpg;*.bmp;*.gif|所有文件(*.*)|*.*";

            if (opfdPhoto.ShowDialog() == DialogResult.OK)
            {
              
                this.lblPhoto.Text =opfdPhoto.FileName;
                this.ptrPhoto.Image = Image.FromFile(this.opfdPhoto.FileName);
            }
            string fileName;
            int pos = this.opfdPhoto.FileName.LastIndexOf("//");
          
            fileName = this.lblPhoto.Text.Substring(pos + 1, this.lblPhoto.Text.Length - pos - 1);
            //fileName = this.opfdPhoto.FileName.Substring(pos + 1, this.opfdPhoto.FileName.Length - pos - 1);
            //MessageBox.Show(fileName);

             //int ps = this.opfdPhoto.FileName.LastIndexOf(".");

            FileInfo fi = new FileInfo(this.lblPhoto.Text);
            fi.CopyTo(Application.StartupPath + @"/StudentPhoto/" + fileName, true);
            Environment.CurrentDirectory = Application.StartupPath;