System.Drawing.Imaging Namespace
I was really taken aback today when I read that Microsoft does not support the use of the “System.Drawing.Imaging” Namespace within a windows or ASP.NET service. I have never had a problem with it when I have used it before and don’t expect to have a problem with it. But it is surprising that Microsoft doesn’t support it. Just thought some others might have the same reaction to it as I did…
Advertisements
The reason is that it internally uses unmanaged GDI. As I understand it, a lot of the objects in the System.Drawing namespace (NOT only the Imaging namespace!) uses up device contexts, and unfortunately Windows has a very hard limit on those (it can even bring down the server if you use up all of them).
Make sure that you explicitly call dispose on your System.Drawing.* objects as soon as possible, and you should be fine. Generally the shorter lifespan they have, the less likely you are to hit this limit.
Technically this is not only a problem with ASP.NET only, but it’s much more likely to hit the limit with many users and a garbage collector that struggles to keep up with with disposing your objects.
Just a side note: It’s not all objects that have this problem. For instance System.Drawing.Color is just fine to use (generally if it doesn’t implement IDisposable you should be fine).
Hi Morten,
Thank you for the insights into the System.Drawing namespace. Do you know if using the WPF Canvas to make your images in 3.5 makes it more stable? (with WPF being mostly managed code)
Vish