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!
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!