用新图像替换图像 [英] Replace image with new image

查看:105
本文介绍了用新图像替换图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

when i try to delete the image i get this error i tried to dispose of the picture box but that doesn't seem to work.

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

System.IO.IOException: 'The process cannot access the file 'C:\Users*******\Documents\Visual Studio 2019\Project****************\dbo\DBImages\Bolgheri Sassicaia.jpg' because it is being used by another process.'

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

the image i load from this folder, i want to delete it and then add another picture with the same name. pretty much i want to update the image.

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

This is where i load the image to the picture box

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

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

This is where i try to replace the image.

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

推荐答案

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

So i figured it out after long trials and error. We have to dispose of the image before loading the new image. like so

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

这篇关于用新图像替换图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆