473,379 Members | 1,167 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,379 software developers and data experts.

Exception Thrown When Rotating Photos Successively

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
3 1640
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Gary.Hu | last post by:
I was trying to catch the Arithmetic exception, unsuccessfully. try{ int a = 0, b = 9; b = b / a; }catch(...){ cout << "arithmetic exception was catched!" << endl; } After ran the program,...
42
by: cody | last post by:
public DateTime Value { get { try { return new DateTime(int.Parse(tbYear.Text), int.Parse(tbMonth.Text), int.Parse(tbDay.Text)); } catch (FormatException)
6
by: Páll Ólafsson | last post by:
Hi I have a problem with the Microsoft.ApplicationBlocks.ExceptionManagement? I can't get it to work in RELEASE mode? If I run the project in debug mode the block works fine but when I run the...
5
by: n_o_s_p_a__m | last post by:
Can't compile. Does this mean that all functions that throw exceptions must be of return type void? examples: // won't compile: "not all code paths return a value" public override int Run() {...
4
by: Bhavya Shah | last post by:
Hello, I am facing a very strange problem in my application. I have a form on which I select a path. I open the FolderBrowserDialog for path selection. Once the path is selected I press a button...
14
by: Nenad Dobrilovic | last post by:
Hi, Is it possible for exception object to be aware of it's throwing? I want to log in the text file when exeption is thrown, not when the exception object is created (because I can create...
44
by: craig | last post by:
I am wondering if there are some best practices for determining a strategy for using try/catch blocks within an application. My current thoughts are: 1. The code the initiates any high-level...
6
by: Steve Long | last post by:
Help, I'm running VS.NET 2003 and when I try to start my application, I get the "unhandled exception" dialog instead of the IDE highlighting the offending line of code. The problem appears to be...
132
by: Zorro | last post by:
The simplicity of stack unraveling of C++ is not without defective consequences. The following article points to C++ examples showing the defects. An engineer aware of defects can avoid...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.