【问题标题】:Replace image with new image用新图像替换图像
【发布时间】:2019-11-15 17:24:47
【问题描述】:

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

System.IO.IOException: '进程无法访问文件 'C:\Users*******\Documents\Visual Studio 2019\项目****************\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);
            }
          }
        }

【问题讨论】:

    标签: c# image


    【解决方案1】:

    您必须释放 image1 对象才能关闭文件句柄。这是保存文件句柄的实际对象。

    这次通话之前

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

    确保处置image1对象

    image1.Dispose();
    

    【讨论】:

    • 请查看我在上面编辑的代码中添加 dispose 的位置。
    • drinkImageView.Dispose(); 之后也做drinkImageView.Image.Dispose();。试试看会发生什么。
    • 它仍在使用中。我不知道我似乎无法找到正在使用该文件的内容我的意思是如果我在以第一种方法加载图像后尝试删除以查看,它可以删除。
    • 图像保存在我的项目文件夹中,这可能是该图像仍在使用的原因吗?
    • @RickHamton 您确定不是从应用程序的其他位置打开此文件吗?
    【解决方案2】:

    所以经过长时间的反复试验,我想通了。 我们必须在加载新图像之前处理图像。 像这样

        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;
            }
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-10-17
      • 2014-06-09
      • 2018-11-30
      • 2023-04-10
      • 1970-01-01
      • 2013-05-17
      • 2011-11-26
      相关资源
      最近更新 更多