Barger

Ok, This is a hard one...For me at least.

I have a jpeg image that is being used by a picturebox in my windows form. During the duration of my program, I need to let the user replace the file that my picturebox uses an an image.

I recieve the following error:

Additional information: The process cannot access the file "C:\Documents and Settings\Ryan Barger\My Documents\Visual Studio Projects\WindowsApplication1\bin\Debug\workingDirectory\images\CRMImage[1].png" because it is being used by another process.

I have tried disposing the image prior to replacing the file.

Is there a way to close the file that was being used by the picureBox so the file can be replaced

Thanks!



Re: Visual C# General The process cannot access the file **HARD ONE**

Mark Betz

I assume you're creating an Image instance for the PictureBox by using the Image.FromFile() method Image also has a FromStream() method. So one option would be, instead of simply creating the Image using FromFile(), open the file yourself, read it into a MemoryStream, and then close it. You can then use Image.FromStream(MemoryStream) to create the image for the PictureBox, while leaving the file unlocked and able to be overwritten.



Re: Visual C# General The process cannot access the file **HARD ONE**

e. heringer

I think someone gave a solution in this forum somedays ago. I couldn¡¯t find the thread, so, let me try to reproduce it:

System.Drawing.Image img = Bitmap.FromFile( @"c:\my_example_image_path.jpg" );

Bitmap bmp = new Bitmap(img);

img.Dispose();

I think it was something like that.





Re: Visual C# General The process cannot access the file **HARD ONE**

nahguam

Yea I seem to remember that thread too. I think you got it. Essentially, creating a new bitmap of the original one and then disposing of the original to free the resources.



Re: Visual C# General The process cannot access the file **HARD ONE**

Barger

This worked GREAT for me!

I used a filestream and closed after creating the image.fromStream();

Thanks!