Click here to Skip to main content
15,879,348 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
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?
Posted

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:
C#
    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;
        }
    }


[edit]Where'd code block go to? - OriginalGriff[/edit]
 
Share this answer
 
v2
Comments
Dhruv Shah007 16-Jul-13 6:46am    
thnx a lotttt...after searchng a lot for this issue I got this answer .
I have one more idea but I don't weather it will wrk or not..
when we load the image to picturebox first copy it to temp folder of os 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..
OriginalGriff 16-Jul-13 7:22am    
Well, yes...but why? It relies on your program remembering to tide up after itself - which if it crashes, it won't...
Better not to write file copies if you don't have to!
Brinda Arumugam 17-Sep-15 3:14am    
Superb :) thank you its working....
Sid_Joshi 10-Nov-15 1:15am    
Thank You OriginalGriff 5 Votes (y)
nirman b doshi 22-Mar-17 5:27am    
excellent solution. Worked like a charm
VB
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()
 
Share this answer
 
Comments
CHill60 10-Apr-14 17:35pm    
Nearly a year late!
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..
 
Share this answer
 
Comments
Dhruv Shah007 16-Jul-13 6:59am    
Sir I have some query. You have given solution for one picture box on the form ,but I am dynamically generating the picture boxes by fetching images from the folder and then I want to delete image from folder so it is showing error of file is used by another process..
please help me out its working for single file.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900