一个ListBox,
private void listBox1_SelectedValueChanged(object sender, EventArgs e)
{
try
{
dabuID = Convert.ToInt32(listBox1.SelectedValue.ToString());
tbPart.Text = listBox1.Text;
picbxPort.Image = Image.FromFile(Application.StartupPath + md.Get_A_Dabu(listBox1.SelectedValue.ToString()).PicUrl);
tbPicUrl.Text = "";
}
catch { }
}
删除选中的某个项时,顺便删除它的图片:
private void tsbtnDel_Click(object sender, EventArgs e)
{
if (dabuID == 0)
{
MessageBox.Show("请选择要删除的大部!");
}
else
{
if (MessageBox.Show("确定要删除?", "此删除不可恢复!", MessageBoxButtons.OKCancel) == DialogResult.OK)
{
try
{
string oldPath = Application.StartupPath + md.Get_A_Dabu(dabuID.ToString()).PicUrl;
picbxPort.Image = Image.FromFile(Application.StartupPath + "\\DabuImage\\default.gif");
Image.FromFile(oldPath).Dispose();
md.Del(dabuID.ToString());
BindLb(cbbAssType.SelectedValue.ToString());
File.Delete(oldPath);
MessageBox.Show("删除成功!");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "错误提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
}
错误就在 File.Delete(oldPath);
提示另外一个进程正在使用oldPath所指示的图片!哪里的错误呢?该怎么办?
picbxPort.Image = Image.FromFile(Application.StartupPath + "\\DabuImage\\default.gif");
Image.FromFile(oldPath).Dispose();
这两句去掉也是这个错误!
private void listBox1_SelectedValueChanged(object sender, EventArgs e)
{
try
{
dabuID = Convert.ToInt32(listBox1.SelectedValue.ToString());
tbPart.Text = listBox1.Text;
picbxPort.Image = Image.FromFile(Application.StartupPath + md.Get_A_Dabu(listBox1.SelectedValue.ToString()).PicUrl);
tbPicUrl.Text = "";
}
catch { }
}
删除选中的某个项时,顺便删除它的图片:
private void tsbtnDel_Click(object sender, EventArgs e)
{
if (dabuID == 0)
{
MessageBox.Show("请选择要删除的大部!");
}
else
{
if (MessageBox.Show("确定要删除?", "此删除不可恢复!", MessageBoxButtons.OKCancel) == DialogResult.OK)
{
try
{
string oldPath = Application.StartupPath + md.Get_A_Dabu(dabuID.ToString()).PicUrl;
picbxPort.Image = Image.FromFile(Application.StartupPath + "\\DabuImage\\default.gif");
Image.FromFile(oldPath).Dispose();
md.Del(dabuID.ToString());
BindLb(cbbAssType.SelectedValue.ToString());
File.Delete(oldPath);
MessageBox.Show("删除成功!");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "错误提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
}
错误就在 File.Delete(oldPath);
提示另外一个进程正在使用oldPath所指示的图片!哪里的错误呢?该怎么办?
picbxPort.Image = Image.FromFile(Application.StartupPath + "\\DabuImage\\default.gif");
Image.FromFile(oldPath).Dispose();
这两句去掉也是这个错误!
里面也用到了Image.FromFile,是不是同一个文件啊而且
Image.FromFile(oldPath).Dispose();
这种写法是什么意思啊,画蛇添足?
------------------
Image.FromFile这种方法一定不行???
我本以为是为了释放已经引用的部分
这两个图片有什么关系,
肯定是什么地方的Image没有dispose掉,
建议楼主Image SourceImage = Bitmap.FromFile(路径);
然后SourceImage.dispose();
肯定是图片资源还在内存中,没有释放
picbxPort.Image=null;
不知道这样可以么。。
picbxPort.Image = Image.FromFile(Application.StartupPath + md.Get_A_Dabu(listBox1.SelectedValue.ToString()).PicUrl);
这种方式不行吗?
tbPicUrl.Text = "";这来也的需要dispose
picbxPort.Image = Image.FromFile(Application.StartupPath + "\\DabuImage\\default.gif");
Image.FromFile(oldPath).Dispose();
md.Del(dabuID.ToString());
BindLb(cbbAssType.SelectedValue.ToString());
picbxPort.Dispose();//不知道你的方法能不能点出Dispose();来
File.Delete(oldPath);
private void Form2_Load(object sender, EventArgs e)
{
this.pictureBox1.Image = Image.FromFile("Blue hills.jpg");
}//删除
private void button1_Click(object sender, EventArgs e)
{
this.pictureBox1.Image.Dispose();
this.pictureBox1.Image = null;
System.IO.File.Delete("Blue hills.jpg");
}