There are at least 2 completely independent problems in the code that cause it to crash.
Problem # 1: GDI + Error
This is a recurring problem that continues to arise throughout the ecosystem. I had it, I managed to fix it, now I donβt remember exactly how to do it. I tried to replicate your problem, and I was not able to get a failure.
I suggest you check this topic: http://forums.asp.net/t/624305.aspx/1 Here, some guys have successfully coped with the problem:
- Making sure that they have placed all instances of Bitmap before executing other drawings.
- ( interesting ): Executing GC.Collect () (possibly after each bitmap rendering)
This is not the main reason why I wanted to try and help you with the answer. It seems that you have already done (you say you did) everything that I usually do to make sure that I do not get into this situation (security, trust, etc.). Furthemore, you cleanse after yourself more than necessary (a little too much, as you learn when reading my answer).
I found that your code has a second problem that you probably don't suspect. Just solving this problem in my own VS environment, I successfully executed Bitmaps (in both IIS 7, Express, and ASP Development Server).
It is possible that by reorganizing things in your application code, you will be able to solve problem # 1. So: please check what I have to say about issue # 2.
Problem # 2: Threads cannot be deleted and returned at the same time
You cannot go and return the threads that you just created and are located , for example:
public static Stream SomeMethod() { using (MemoryStream stream = new MemoryStream()) {
I really don't understand how this piece of code works on the ASP.NET Development Server. The problem I'm trying to point out here is that your code will always throw an ObjectDisposedException (regardless of whether you use the code as a service or in the user's interactive space):

Who closes the stream? Completing an action with .
Possible solutions to problem # 2
A quick fix to this problem (which could use more memory than you predicted) would be to have your method return a byte [] instead of a stream.
public static byte[] SomeMethod() { using (MemoryStream stream = new MemoryStream()) {
Allowing myself to make assumptions about the needs of your applications, I would say that this other solution could be even better:
If , then you want to return these generated graphic images to <img> back to the web browser, and you do this using, say, ASP.NET common handlers, then you can pass the OutputStream of the current WebResponse method to , instead of taking the resulting byte [] (or stream) from , like this:
In HTML:
<img src="path/Foo.ashx" alt="chart" ... etc ... />
while in the application:
public class Foo : IHttpHandler { public void ProcessRequest(HttpContext context) { context.Response.ContentType = "application/octet-stream"; Stream alreadyExistingStream = context.Response.OutputStream; Etc.SomeMethod(stream); } } public class Etc { public static void SomeMethod(Stream stream) {