[c#]删除在picturebox中打开的图像 [英] [c#] delete image which is opend in picturebox

查看:79
本文介绍了[c#]删除在picturebox中打开的图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我将图像打开到一个图片框中时,同时我想从磁盘中删除图像。

此刻我不能因为它显示错误:



该进程无法访问文件'C:\ Users \ ......... .jpg',因为它正由另一个进程使用。



这个过程是我打开图像的程序。



如何解决这个问题?

When i open image into a picturebox, at the same time i want to delete the image from disk.
At this moment i can`t because it shows the error:

The process cannot access the file 'C:\Users\......... .jpg' because it is being used by another process.

This process is my program in which i open the image.

How to fix this?

推荐答案

不要只是加载图像 - 文档确实说文件或流必须在Image实例的持续时间内可用。

相反,从文件中加载它,从中创建一个新图像,然后处理原始图像:

Don't just load the image - the documentation does say that the file or stream must be available for the duration of the Image instance.
Instead, load it from the file, create a new image from it, and then dispose the original:
    Image im = GetCopyImage(@"D:\Temp\AAAA.jpg");
    myPictureBox.Image = im;
    File.Delete(@"D:\Temp\AAAA.jpg");
    }

private Image GetCopyImage(string path)
    {
    using (Image im = Image.FromFile(path))
        {
        Bitmap bm = new Bitmap(im);
        return bm;
        }
    }





代码块去哪里了? - OriginalGriff [/ edit]



[edit]Where'd code block go to? - OriginalGriff[/edit]


我还有一个想法,但我不知道它会不会工作..
当我们将图像加载到picturebox时
首先将其复制到操作系统的临时文件夹或任何其他任何文件夹,然后从那里加载图像,并在删除图像时将其从原始路径中删除。当我们关闭程序时,从temp中释放图像。

我认为这样可行..
I have one more idea but I don't weather it will work or not..
when we load the image to picturebox first copy it to temp folder of operating system or in any other folder whatever you want and then load image from there and while deleting the image delete it from original path.and when we close the program release the images from temp.
I think this will work..


Dim fs As System.IO.FileStream
' Specify a valid picture file path on your computer.
fs = New System.IO.FileStream("C:\WINNT\Web\Wallpaper\Fly Away.jpg",
     IO.FileMode.Open, IO.FileAccess.Read)
PictureBox1.Image = System.Drawing.Image.FromStream(fs)
fs.Close()


这篇关于[c#]删除在picturebox中打开的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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