Image Resizing and Renaming...

R

Robert Walter

I have the following code that loads an image, resizes it as a new
image and then saves to a new file, the original image file then gets
deleted and the new image is renamed to the old filename.

Now I get the following exception on the File.Delete line

System.IO.IOException: The process cannot access the file
"C:\test.jpg" because it is being used by another process. at
System.IO.__Error.WinIOError(Int32 errorCode, String str) at
System.IO.File.Delete(String path) at
tt360.Cms.Adm.Content.Objects.EditDimensions.Save_Click(Object sender,
EventArgs e) in c:\vss\tt3\cms\002\web\tt360.cms.adm\content\objects\editdimensions.aspx.cs:line
215

I read that System.Drawing.Image.FromFile would keep the file open so
I changed the code to use System.Drawing.Image.FromStream so I can
explicitly close the file but it appears this is not the case as the
exception occurs from the file not being closed.

Now I have another web page that also accesses the original file but
only to open it to generate a thumbnail. I use the same FromStream
method to open and then close the file. I am pretty sure this page is
not keep the file open either. Nothing else is accessing this page.

This code runs in a submit button click handler and it takes maybe 10
seconds of waiting/clicking the button before it saves correctly. Is
it this page that's not working correctly or could it be to do with
the other page?

Thanks in advance for your help.



Rob.


string "C:\\test.jpg";
string "C:\\new_test.jpg";

GenerateThumbnail(fileName, newFileName);

System.Drawing.Image image = null;
FileStream fs = null;

try
{
// image = System.Drawing.Image.FromFile(Context.Server.MapPath(fileName));

fs = new FileStream(Context.Server.MapPath(fileName), FileMode.Open,
FileAccess.Read, FileShare.Read);

image = System.Drawing.Image.FromStream(fs);
}
catch { }

if (image != null)
{
int _maxWidth = 0;
int _maxHeight = 0;

try
{
_maxWidth = Convert.ToInt32(Width.Text);
}
catch { }

try
{
_maxHeight = Convert.ToInt32(Height.Text);
}
catch { }

System.Drawing.Bitmap thumbnailBitmap = new
System.Drawing.Bitmap(_maxWidth, _maxHeight);

System.Drawing.Graphics graphics =
System.Drawing.Graphics.FromImage(thumbnailBitmap);

graphics.DrawImage(image, new System.Drawing.Rectangle(0, 0,
_maxWidth, _maxHeight));

thumbnailBitmap.Save(Context.Server.MapPath(newFileName),
image.RawFormat);

thumbnailBitmap.Dispose();
thumbnailBitmap = null;

image.Dispose();
image = null;

graphics.Dispose();
graphics = null;
}
else
{
ResponseMessage.Text = "Could not load image. ";
}

if (fs != null)
{
fs.Close();
fs = null;
}


try
{
File.Delete(Server.MapPath(fileName));
File.Move(Server.MapPath(newFileName), Server.MapPath(fileName));

ObjectImage.ImageUrl = fileName;

EditPanel.Visible = false;
ResponseMessage.Text = "The image has been updated. ";
}
catch (Exception ex)
{
ResponseMessage.Text = ex.ToString();
}
 
N

Nicholas Paldino [.NET/C# MVP]

Robert,

You might want to try loading the contents of the file into memory first
(a MemoryStream), and then passing that to the FromStream method. This way,
you know the file is never opened. However, if the files are particularly
large, it might have a performance impact (given that the contents of the
file have to be loaded completely into memory).

Hope this helps.
 
R

Robert Walter

Nicholas Paldino said:
Robert,

You might want to try loading the contents of the file into memory first
(a MemoryStream), and then passing that to the FromStream method. This way,
you know the file is never opened. However, if the files are particularly
large, it might have a performance impact (given that the contents of the
file have to be loaded completely into memory).

What is the easiest way to load the file into a memory stream?

Thanks,



Rob.
 

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

Top