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

Error when attempting to open files

P: n/a
Hi,

I have the following code:

private Image _image;

public MyImage(string filename)
{
FileStream file = File.Open(filename, FileMode.Open);
_image = Image.FromStream(file);
//file.Close();
}

Which essentially is used for passing a file name of an image, which is
then used for various things such as creating a thumbnail.

The problem I'm having is that if I create a thumbnail of the image that
is open, then reload the page in my browser, I get an error that reads:

"The process cannot access the file ... because it is being used by
another process"

So I figured that was because I forget to close the file stream after
opening the image file, so I added in the line of code that is commented
out in the example above.

Now when I run the code I receive:

"Out of memory."

If I attempt to create a thumbnail image with:

_image = _image.GetThumbnailImage(size, size, null, IntPtr.Zero);)

or:

"A generic error occurred in GDI+."

If I simply write the contents of the image to the browser:

_image.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);

When I don't close the file stream it works okay for the first time I
view the page (as a thumbnail, or the full-sized image), but gives the
error about it being in use if I refresh. It doesn't work at all when I
close the file stream.

Any ideas what I'm doing wrong here?

--
Dylan Parry
http://electricfreedom.org | http://webpageworkshop.co.uk

The opinions stated above are not necessarily representative of
those of my cats. All opinions expressed are entirely your own.
Jul 1 '08 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Any ideas what I'm doing wrong here?

Well, using System.Drawing from ASP.NET would be the first place to look:
http://msdn.microsoft.com/en-us/libr...m.drawing.aspx
Caution:
Classes within the System.Drawing namespace are not supported for use
within a Windows or ASP.NET service. Attempting to use these classes
from within one of these application types may produce unexpected
problems, such as diminished service performance and run-time exceptions.
As for something more specific - sorry, nothing leaps to mind.

Marc

Jul 1 '08 #2

P: n/a
Hi Dylan,

Image.FromStream holds the underlying stream while it is in use. Try
loading the image data into memory and create an image from that. After the
ReadAllBytes line the File is released.

byte[] data = File.ReadAllBytes(imagepath);
MemoryStream ms = new MemoryStream(data);
Image img = Image.FromStream(ms);

Remember not to dispose the MemoryStream or you will end up with the
OutOfMemoryException you got when you closed the File.

--
Happy Coding!
Morten Wennevik [C# MVP]
"Dylan Parry" wrote:
Hi,

I have the following code:

private Image _image;

public MyImage(string filename)
{
FileStream file = File.Open(filename, FileMode.Open);
_image = Image.FromStream(file);
//file.Close();
}

Which essentially is used for passing a file name of an image, which is
then used for various things such as creating a thumbnail.

The problem I'm having is that if I create a thumbnail of the image that
is open, then reload the page in my browser, I get an error that reads:

"The process cannot access the file ... because it is being used by
another process"

So I figured that was because I forget to close the file stream after
opening the image file, so I added in the line of code that is commented
out in the example above.

Now when I run the code I receive:

"Out of memory."

If I attempt to create a thumbnail image with:

_image = _image.GetThumbnailImage(size, size, null, IntPtr.Zero);)

or:

"A generic error occurred in GDI+."

If I simply write the contents of the image to the browser:

_image.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);

When I don't close the file stream it works okay for the first time I
view the page (as a thumbnail, or the full-sized image), but gives the
error about it being in use if I refresh. It doesn't work at all when I
close the file stream.

Any ideas what I'm doing wrong here?

--
Dylan Parry
http://electricfreedom.org | http://webpageworkshop.co.uk

The opinions stated above are not necessarily representative of
those of my cats. All opinions expressed are entirely your own.
Jul 1 '08 #3

P: n/a
Morten Wennevik [C# MVP] wrote:
Image.FromStream holds the underlying stream while it is in use. Try
loading the image data into memory and create an image from that. After the
ReadAllBytes line the File is released.
Excellent, thanks. Working with images is very much out of my
comfort-zone, so it's little things like this that tend to trip me up!

Thanks again,

--
Dylan Parry
http://electricfreedom.org | http://webpageworkshop.co.uk

The opinions stated above are not necessarily representative of
those of my cats. All opinions expressed are entirely your own.
Jul 1 '08 #4

This discussion thread is closed

Replies have been disabled for this discussion.