By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,723 Members | 844 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,723 IT Pros & Developers. It's quick & easy.

Exception Thrown When Rotating Photos Successively

P: n/a
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.RotateFlipTy pe.Rotate270FlipNone);
}
else {
imageToFlip.RotateFlip(System.Drawing.RotateFlipTy pe.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!
Nov 17 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
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.

"PB" <A@B.com> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
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.RotateFlipTy pe.Rotate270FlipNone);
}
else {
imageToFlip.RotateFlip(System.Drawing.RotateFlipTy pe.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!

Nov 17 '05 #2

P: n/a
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.

"Bob Powell [MVP]" <bob@_spamkiller_bobpowell.net> wrote in message
news:OQ****************@TK2MSFTNGP14.phx.gbl...
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.

"PB" <A@B.com> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
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.RotateFlipTy pe.Rotate270FlipNone);
}
else {
imageToFlip.RotateFlip(System.Drawing.RotateFlipTy pe.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!


Nov 17 '05 #3

P: n/a
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.RotateFlipTy pe.Rotate270FlipNone);
}
else
{
imageToFlip.RotateFlip(System.Drawing.RotateFlipTy pe.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

"PB" <A@B.com> wrote in message
news:eG**************@TK2MSFTNGP12.phx.gbl...
| 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.
|
|
|
| "Bob Powell [MVP]" <bob@_spamkiller_bobpowell.net> wrote in message
| news:OQ****************@TK2MSFTNGP14.phx.gbl...
| > 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.
| >
| >
| >
| >
| >
| > "PB" <A@B.com> wrote in message
| > news:%2****************@tk2msftngp13.phx.gbl...
| >> 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.RotateFlipTy pe.Rotate270FlipNone);
| >> }
| >> else {
| >> imageToFlip.RotateFlip(System.Drawing.RotateFlipTy pe.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!
| >>
| >
| >
|
|
Nov 17 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.