Exception Thrown When Rotating Photos Successively

P

PB

As part of an ASP.NET WEb Application I have a routine (relevant portion is
below) that lets users rotate a photo (jpg or gif).

The routine works just fine if it is run once. If run a second time
immediately after the first, then an exception with the following message is
thrown:

"The process cannot access the file
"C:\InetPub\Files\MyApp\SubDir\MyPic.JPG" because it is being used by
another process"

What do I need to change in order to be able to run this code more than once
without that exception being thrown?

// BEGIN Snippet
System.IO.FileStream fs = new System.IO.FileStream(pathToOriginal,
System.IO.FileMode.Open, System.IO.FileAccess.ReadWrite);
System.Drawing.Image imageToFlip = System.Drawing.Image.FromStream(fs);

if (direction.ToUpper() == "LEFT") {
imageToFlip.RotateFlip(System.Drawing.RotateFlipType.Rotate270FlipNone);
}
else {
imageToFlip.RotateFlip(System.Drawing.RotateFlipType.Rotate90FlipNone);
}

// Delete the original (non rotated) so we can recreate it with the original
file name
fs.Close();

File.Delete(pathToOriginal);

// Save the new image, setting the ContentType correctly
if (fileType == "JPG" || fileType == "JPEG") {
imageToFlip.Save(pathToOriginal, System.Drawing.Imaging.ImageFormat.Jpeg);
}
else {
imageToFlip.Save(pathToOriginal, System.Drawing.Imaging.ImageFormat.Gif);
}

// clean up now that we're done with it.
imageToFlip.Dispose();

// END Snippet


The users need to be able to rotate more than once.

Thanks!
 
B

Bob Powell [MVP]

This looks like the old file-locking problem. Open the image from a stream
and explicitly close the stream. Then you can save the rotated image back to
the original filename without this problem.

--
Bob Powell [MVP]
Visual C#, System.Drawing

Ramuseco Limited .NET consulting
http://www.ramuseco.com

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.
 
P

PB

Hi Bob - I'm a bit confused - as you are suggesting that I do something I
think I'm already doing. Can you elaborate? Am I actually *not* opening the
image from a stream even though I'm working with it as System.IO.FileStream
? Is there another type of stream you are thinking of that I'm not using
that I should be using?

Thanks.
 
J

Jay B. Harlow [MVP - Outlook]

PB,
As bob suggests it sounds like the old file-locking problem, however I have
not tried your code to find the problem.

One item I would suggest is to use the using statement to ensure that files
& other objects are disposed of (closed) properly.

Something like (untested):

// BEGIN Snippet
System.Drawing.Image imageToFlip;
using (System.IO.FileStream fs = new System.IO.FileStream(pathToOriginal,
System.IO.FileMode.Open, System.IO.FileAccess.ReadWrite))
{

imageToFlip = System.Drawing.Image.FromStream(fs);

if (direction.ToUpper() == "LEFT")
{
imageToFlip.RotateFlip(System.Drawing.RotateFlipType.Rotate270FlipNone);
}
else
{
imageToFlip.RotateFlip(System.Drawing.RotateFlipType.Rotate90FlipNone);
}

}

File.Delete(pathToOriginal);

using (imageToFlip)
{
// Save the new image, setting the ContentType correctly
if (fileType == "JPG" || fileType == "JPEG")
{
imageToFlip.Save(pathToOriginal,
System.Drawing.Imaging.ImageFormat.Jpeg);
}
else
{
imageToFlip.Save(pathToOriginal,
System.Drawing.Imaging.ImageFormat.Gif);
}

}

// END Snippet

I wonder if the Image.Save calls are tripping you up. Have you tried
replacing them with Streams instead of file names?

Hope this helps
Jay

| Hi Bob - I'm a bit confused - as you are suggesting that I do something I
| think I'm already doing. Can you elaborate? Am I actually *not* opening
the
| image from a stream even though I'm working with it as
System.IO.FileStream
| ? Is there another type of stream you are thinking of that I'm not using
| that I should be using?
|
| Thanks.
|
|
|
| | > This looks like the old file-locking problem. Open the image from a
stream
| > and explicitly close the stream. Then you can save the rotated image
back
| > to the original filename without this problem.
| >
| > --
| > Bob Powell [MVP]
| > Visual C#, System.Drawing
| >
| > Ramuseco Limited .NET consulting
| > http://www.ramuseco.com
| >
| > Find great Windows Forms articles in Windows Forms Tips and Tricks
| > http://www.bobpowell.net/tipstricks.htm
| >
| > Answer those GDI+ questions with the GDI+ FAQ
| > http://www.bobpowell.net/faqmain.htm
| >
| > All new articles provide code in C# and VB.NET.
| > Subscribe to the RSS feeds provided and never miss a new article.
| >
| >
| >
| >
| >
| > | >> As part of an ASP.NET WEb Application I have a routine (relevant
portion
| >> is below) that lets users rotate a photo (jpg or gif).
| >>
| >> The routine works just fine if it is run once. If run a second time
| >> immediately after the first, then an exception with the following
message
| >> is thrown:
| >>
| >> "The process cannot access the file
| >> "C:\InetPub\Files\MyApp\SubDir\MyPic.JPG" because it is being used by
| >> another process"
| >>
| >> What do I need to change in order to be able to run this code more than
| >> once without that exception being thrown?
| >>
| >> // BEGIN Snippet
| >> System.IO.FileStream fs = new System.IO.FileStream(pathToOriginal,
| >> System.IO.FileMode.Open, System.IO.FileAccess.ReadWrite);
| >> System.Drawing.Image imageToFlip = System.Drawing.Image.FromStream(fs);
| >>
| >> if (direction.ToUpper() == "LEFT") {
| >>
imageToFlip.RotateFlip(System.Drawing.RotateFlipType.Rotate270FlipNone);
| >> }
| >> else {
| >> imageToFlip.RotateFlip(System.Drawing.RotateFlipType.Rotate90FlipNone);
| >> }
| >>
| >> // Delete the original (non rotated) so we can recreate it with the
| >> original file name
| >> fs.Close();
| >>
| >> File.Delete(pathToOriginal);
| >>
| >> // Save the new image, setting the ContentType correctly
| >> if (fileType == "JPG" || fileType == "JPEG") {
| >> imageToFlip.Save(pathToOriginal,
| >> System.Drawing.Imaging.ImageFormat.Jpeg);
| >> }
| >> else {
| >> imageToFlip.Save(pathToOriginal,
System.Drawing.Imaging.ImageFormat.Gif);
| >> }
| >>
| >> // clean up now that we're done with it.
| >> imageToFlip.Dispose();
| >>
| >> // END Snippet
| >>
| >>
| >> The users need to be able to rotate more than once.
| >>
| >> Thanks!
| >>
| >
| >
|
|
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top