用新图像替换图像

当我尝试删除图像时,出现此错误,我试图处理图片框,但这似乎不起作用。

  

System.IO.IOException:'进程无法访问文件   'C:\ Users ******* \ Documents \ Visual Studio   2019 \ Project **************** \ dbo \ DBImages \ Bolgheri Sassicaia.jpg'   因为它正在被另一个进程使用。'

我从此文件夹加载的图像,我想将其删除,然后添加另一张具有相同名称的图片。 我几乎想更新图像。

这是我将图像加载到图片框的地方

if (imagepathtext.TextLength > 0)
{
    image1 = Image.FromFile(imagepathtext.Text);
    drinkImageView.Image = image1;
}

这是我尝试替换图像的地方。

      private void UpdatebtnClick(object sender,EventArgs e)
        {
        if (imagepath.TextLength > 9)
        {

            DialogResult result = MessageBox.Show("Are you also trying to 
            update image?","Confirmation",MessageBoxButtons.Yesno);

            if (result == DialogResult.Yes)
            {
                image1.Dispose();
                System.IO.File.Delete(imagepathtext.Text);
            }
          }
        }
SanG_Soul 回答:用新图像替换图像

您必须处置image1对象以关闭文件句柄。这是保存文件的实际对象。

通话之前

System.IO.File.Delete(imagepathtext.Text);

确保处置image1对象

image1.Dispose();
,

所以我经过长时间的尝试和错误才弄清楚了。 在加载新图像之前,我们必须处理该图像。 像这样

    private void BrowseImage_Click(object sender,EventArgs e)
    {
        DrinkView.Image.Dispose();
        var imagePath = image.BROWSEIMAGE();
        if (imagePath.Length > 10)
        {
            DrinkView.Image = new Bitmap(imagePath);
            imagepath.Text = imagePath;
        }
    }
本文链接:https://www.f2er.com/3113454.html

大家都在问