pictureBox1.BackgroundImage = null;
OpenFileDialog ofDlg = new OpenFileDialog();
DialogResult dRet = ofDlg.ShowDialog();
if (dRet == DialogResult.OK)
{
string strFile = ofDlg.FileName;
string type = strFile.Substring(strFile.LastIndexOf(".") + 1);
if (type == "bmp" || type == "jpg" || type == "gif" || type == "jpeg" || type == "png")
{
txtSingle.Text = strFile;
pictureBox1.ImageLocation = txtSingle.Text;
System.IO.Directory.SetCurrentDirectory(Application.StartupPath);
lblShow.Hide();
}
else
{
MessageBox.Show("不是有效的图片格式!");
}
}
else
{
txtSingle.Text = "";
}
上传代码:
string filepath = this.txtSingle.Text.Trim();
if (filepath == string.Empty)
{
MessageBox.Show("图片路径不能为空!");
return;
}
FileInfo info = new FileInfo(filepath);
string imgpath = "\\images\\";
//string dirpath = Application.StartupPath + imgpath;
string dirpath = \\\\ip\\路径\\+ imgpath
try
{
if (!Directory.Exists(dirpath))
{
Directory.CreateDirectory(dirpath);
}
info.CopyTo(dirpath + info.Name, true);
txtSingle.Text = imgpath + info.Name;
MessageBox.Show("图片上传成功!");
}
catch (Exception ex)
{
lblShow.Show();
this.lblShow.Text = "图片上传失败,失败原因:" + ex.Message;
}