473,326 Members | 2,168 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,326 software developers and data experts.

Error when attempting to open files

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

Similar topics

0
by: Blood... | last post by:
Parser Error Description: An error occurred during the parsing of a resource required to service this request. Please review the following specific parse error details and modify your source file...
6
by: AlanS | last post by:
I have used Visual Studio for about 8 months. I have developed some ASP solutions. I had to get on with some other things and have not worked with ASP.NET for a couple months. In the meantime, I...
1
by: chanmm | last post by:
I hit the problem in my WinXP can someone help me: The Web server reported the following error when attempting to create or open the Web project located at the following URL:...
8
by: Rod | last post by:
I have been working with ASP.NET 1.1 for quite a while now. For some reason, opening some ASP.NET applications we wrote is producing the following error message: "The Web server reported...
0
by: Ashutosh | last post by:
---------------------------------------------------------------------------- ---- Parser Error Description: An error occurred during the parsing of a resource required to service this request....
3
by: ctk70 | last post by:
I'm trying to run a ASP.NET 1.1 app on a Windows 2003 server. The app runs fine on my local workstation (Windows 2000 SP4). Both the server and workstation have Visual Studio .NET 2003 installed. ...
1
by: PK9 | last post by:
I am receiving the following error in Visual Studio 2003: "the web server reported the following error when attempting to create or open the web project HTTP/1.1 403 Access Forbidden" I am...
2
by: thersitz | last post by:
Hi, I have VStudio2005, SQLServer 2005 dev edition loaded on a windowsXP Pro machine. I installed it ok. I just attempted to load the Personal Web Site Starter Kit (I downloaded off...
10
by: =?Utf-8?B?QklKVQ==?= | last post by:
When I am executing a Asp.Net web page containg Tree View web control, I got an error message "File or assembly name microsoft.web.UI.Web controls or one of its dependence, was not found". Kindly...
13
by: bytesc | last post by:
Hey guys, I have created many picture files using gd_library, I recently decided to rename all of my pictures files on my web server to text instead of just numbers. Example: productsssh.jpg ...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
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...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.