views:

346

answers:

2

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.

+1  A: 

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.

jvanderh
Have already checked out the ASP.Net Generated image project on codeplex. The irony of that is that it uses the System.Drawing library in the background as well. So this issue and other related to using System.Drawing would probably also occur using this library.
smehaffie
I did play with it a bit a few months back. However I use System.Drawing.Imaging to generate charts on our site and have never run into any issues. My guess is that there is a reason why it is not supported, maybe they could not figure out where the problem was and that was their solution.
jvanderh
+1  A: 

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.

AJ