Click here to Skip to main content
12,501,890 members (54,212 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: C# image picturebox
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 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....
88388132 10-Nov-15 1:15am
   
Thank You OriginalGriff 5 Votes (y)
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 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 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
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.160919.1 | Last Updated 9 Apr 2014
Copyright © CodeProject, 1999-2016
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