Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
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 11-Nov-12 23:12pm
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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;
                }
            }
 
[edit]Where'd code block go to? - OriginalGriff[/edit]
  Permalink  
v2
Comments
Dhruv Shah007 at 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 at 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!
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

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..
  Permalink  
Comments
Dhruv Shah007 at 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.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

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()
  Permalink  
Comments
CHill60 at 10-Apr-14 17:35pm
   
Nearly a year late!

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

  Print Answers RSS
0 OriginalGriff 310
1 Sergey Alexandrovich Kryukov 166
2 PhilLenoir 164
3 Richard MacCutchan 160
4 Sharmanuj 146
0 Sergey Alexandrovich Kryukov 6,077
1 OriginalGriff 5,115
2 CPallini 2,473
3 Richard MacCutchan 1,597
4 Abhinav S 1,505


Advertise | Privacy | Mobile
Web04 | 2.8.140814.1 | Last Updated 9 Apr 2014
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100