winforms 中图片的更新

wenxue005 2008-06-04 05:26:24
在开发中 经常遇见这样的情况,在 添加 个人信息的时候 要求 同时提供一张相片,当提交个人信息成功后 相片也就保存到指定的文件夹里去了,但是 当 下次我要 重新换一张相片的时候 就要修改我原先提供的信息,并同时修改我的相片,
问题:我要更新我的相片,首先就要把我之前的相片删除掉,再上传新的相片,但是我在删除相片的时候 系统抛了异常
下面是我的代码片段

1--通过点击 datagrid 中的单元格来获取相片,并把相片显示到 pictureBox 中,并取出相片的路径(正常运行)
if (e.RowIndex == i)
{
if (objds.Tables[0].Rows[i][7].ToString() != "")
{
pictureBox1.Image = Image.FromFile(objds.Tables[0].Rows[i][7].ToString());
this.textBox1.Text = objds.Tables[0].Rows[i][7].ToString();
}
else
{
pictureBox1.Image = null;
textBox1.Text = "";
}
}

2----选取新的相片,选取后也显示到 pictureBox 中去(正常运行)
OpenFileDialog dlg = new OpenFileDialog();
dlg.Filter = "所有文件(*.*)|*.*|jpeg文件(*.jpeg,*.jpg)|*.jpeg;*.jpg|gif文件(*.gif)|*.gif|bmp文件(*.bmp)|*.bmp";
dlg.Title = "Selected Photo";
if (dlg.ShowDialog() == DialogResult.OK)
{
this.pictureBox1.Image = new Bitmap(dlg.FileName);
this.textBox1.Text = Path.GetFullPath(dlg.FileName);
}

3---选定图片后 就要更新 图片了 (抛异常:正由另一个进程使用,因此给进程无法访问该文件)
Directory.CreateDirectory("e:\\img");
if (File.Exists("e:\\img\\" + this.txtname.Text + " " + txtpid.Text + ".jpg"))
{
File.Delete("e:\\img\\" + this.txtname.Text + " " + txtpid.Text + ".jpg");
}
File.Copy(textBox1.Text.ToString(), "e:\\img\\" + this.txtname.Text + " " + txtpid.Text + ".jpg");
在这儿我的想法是,既然要更新该图片 首先就要把原来的图片删除,再把新的图片按照原来图片的名称命名,但当程序执行到这里的时候就出问题了
请各位 帮忙解决一下,在下感激不尽,或者给出代码,只要第3步的就行了,谢谢 vs 2005 开发,winforms 程序
...全文
52 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
wenxue005 2008-06-04
  • 打赏
  • 举报
回复
Directory.CreateDirectory("e:\\img");
if (File.Exists("e:\\img\\" + this.txtname.Text + " " + txtpid.Text + ".jpg"))
{
//第二次 执行到这里的时候就抛了
File.Copy(textBox1.Text.ToString(), "e:\\img\\" + this.txtname.Text + " " + txtpid.Text + ".jpg", true);
}
else
{
// 直接保存起来
File.Copy(textBox1.Text.ToString(), "e:\\img\\" + this.txtname.Text + " " + txtpid.Text + ".jpg");
}
wenxue005 2008-06-04
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 yuwenge 的回复:]
把文件操作部分Lock起来就行了。
[/Quote]
怎么弄呢
卧_槽 2008-06-04
  • 打赏
  • 举报
回复
把文件操作部分Lock起来就行了。

110,502

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧