Using Themes in ASP.NET loads System.Drawing namespace

Using the System.Drawing namespace is strongly discouraged in ASP.NET as it leads to a lot of performance/runtime problems.

Recently one of our customers reported an issue where we were getting GDI+ errors in a  normal ASP.NET application.

Exception type: System.Runtime.InteropServices.ExternalException
Message: GDI+ is not properly initialized (internal GDI+ error).
InnerException: <none>
StackTrace (generated):
    SP       IP       Function
    048CE1A0 7AE65963 System.Drawing.Bitmap..ctor(System.IO.Stream)
    048CE1C8 7AE31EAE System.Drawing.ToolboxBitmapAttribute..cctor()

StackTraceString: <none>
HResult: 80004005

The GDI+ calls were not something intentional, none of the application code contained any  reference to the System.Drawing namespace. But the namespace was getting loaded in the process as a result of applying a Theme to the page.

System.Reflection.CustomAttribute._CreateCaObject(Void*, Void*, Byte**, Byte*, Int32*)
System.Reflection.CustomAttribute.CreateCaObject(System.Reflection.Module, System.RuntimeMethodHandle, IntPtr ByRef, IntPtr, Int32 ByRef)
System.Reflection.CustomAttribute.GetCustomAttributes(System.Reflection.Module, Int32, Int32, System.RuntimeType, Boolean, System.Collections.IList)
System.Reflection.CustomAttribute.GetCustomAttributes(System.RuntimeType, System.RuntimeType, Boolean)
System.RuntimeType.GetCustomAttributes(System.Type, Boolean)
System.Web.UI.Page.ProcessRequestMain(Boolean, Boolean)

When an ASPX page has to load a Theme we loop through all Controls/Types on the page and check if we can apply the theme to it(IsTypeThemeable). This involves generating  a list of attributes for the Control/Type. Its a simple call to TypeDescriptor.GetAttributes method which results in each attribute being instantiated. And this is where the trouble starts.

Controls have a ToolboxBitmap attribute which is a picture representation of the control. Its a bitmap and this results in the unintentional loading of the System.Drawing namespace in the worker process.

Currently a fix is being worked on and should be released soon.

Bookmark and Share

Comments (9)

  1. Mike737 says:

    Has a fix been released yet for this issue?

  2. César says:

    Today I get the ‘System.Drawing.ToolboxBitmapAttribute’ exception for the first time on a test server ๐Ÿ™

    Any news about this fix ? ๐Ÿ˜‰

  3. Shinva says:

    Please contact MS Support for assistance on this issue.

  4. Mike737 says:

    Do you have a hot fix or patch number for the issue?

    MS support cannot help me unless I have a number.

    I also logged the issue on connect a while back so it may be useful to respond in their as well since others seem to be experiencing the issue.

  5. Michael G says:

    You can find the update here to fix the GDI error.


    Google instructions to download the hotfix.  :google: download microsoft KB hotfix if it isn’t provided on the page.

  6. Dunxz says:

    Is this hotfix included in .net3.5 sp1?

  7. Xin Gu says:

    Where can I download this update? It seems that there is no download link on the page ";.


  8. Avie says:

    It is the end of 2011, but it seems like the error is not fixed ๐Ÿ™

Skip to main content