smehaffie
smehaffie

Reputation: 389

System.Drawing Error: Can or Cannot it be used in an ASP.Net application

We are using System.Drawing in and application to manipulate images in a web application (rotate, flip, etc). For the last month we have been getting the following error very sporadically.

"Attempted to read or write protected memory. This is often an indication that other memory is corrupt."

Based on the MSDN Library it appears that we should not be using System.Drawing in our web application (see insert below).

Classes within theSystem.Drawing.Imaging 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.

This just does not seem right since there is a whole forum on ASP.Net for using System.Drawing in ASP.Net applications. Plus, all the web Image Editor controls I have found use Sytem.Drawing, so that is another reason I am thinking it is not really an issue with System.Drawing. So the questions I have are:

1) Has anyone else has this issue when using System.Drawing in an ASP.Net application? If so, what was the fix.

2) The line that throws the error is below, could the issue be that we are using a MemoryStream and not really an issue with System.Drawing? Would using a different type of stream take care of the issue.

using (System.Drawing.Image oFullImg = System.Drawing.Image.FromStream(msImage))

3) If there really is a problem and we should not be using System.Drawing, what other alternatives are there for manipulating images on a web site.

This is becoming a more critical issue as it drags on, so the quicker I can find a solution the better. Any help will be appreciated.

Upvotes: 3

Views: 1566

Answers (3)

Lilith River
Lilith River

Reputation: 16468

My guess is that the AccessViolationException is being triggered by a corrupted image. A corrupted image could trick GDI into attempting to read past the end of the stream.

I would suggest logging the images this happens on, then analyzing the images to see if it is repeatable.

Are you performing quantization on the images, or using LockBits and accessing the data directly?

Upvotes: 0

AJ.
AJ.

Reputation: 16719

We use the System.Drawing namespace extensively in several ASP.NET applications, and we also get this error. It's entirely inconsistent, and I've come to believe that it has as much to do with circumstance (system resource usage, number of concurrent requests, etc.) than anything else. I can't offer you a solution that will guarantee that this doesn't happen, other than moving to an SOA-oriented solution in which the image manipulation is taken out of the ASP.NET site's domain, but I will say that you can minimize these occurrences by being absolutely positively without-a-doubt sure that you are disposing of any object in the System.Drawing and System.Drawing.Imaging namespaces that implements IDisposable. It looks like you're already doing this with the using pattern, but there are strange objects (like System.Drawing.Imaging.EncoderParameters) that need to be disposed along with the usual suspects.

Aside from this, you can do other stuff, like setting up the site in its own IIS Application Pool and setting the pool to recycle at regular intervals, which might help.

I'll be watching this question, though, as I'd love to see a 100% solution to this problem.

Upvotes: 1

jvanderh
jvanderh

Reputation: 2955

Well, the warning you describe is in the MSDN documentation for the System.Drawing.Imaging namespace, it doesn't get more official than that.

Take a look at this article from Scott Hanselman to see if it can shed some light and take a look at the ASP.NET Generated Image project at codeplex.

Most importantly take a look at the user comments as they discuss your issue.

The bottom line is that it seems to work but is not supported by Microsoft.

Upvotes: 1

Related Questions