I am playing in the System.Drawing.Imaging namespace, and had an issue trying to draw over the top of bitmaps I had pulled from the web via HttpWebResponse.GetResponseStream.

This Code:

Graphics graph = Graphics.FromImage(bitmap);

Returned this error:

A Graphics object cannot be created from an image that has an indexed pixel format.

The image I was trying to consume had a Image.PixelFormat of Format8bppIndexed. After searching a number of places, I finally found this bug listing. Turns out it’s an old issue that has not been fixed in 2.0. Either way, the fix is pretty simple. The following line of code will change the PixelFormat to Format32bppArgb, and the Graphics.FromImage method takes that format:

bitmap.MakeTransparent(Color.FromArgb(0,0,0));

For me, this was definitely a needle in the preverbal haystack. I think my problem is that I often search on the wrong terms. Google will tell you when you misspell something. I just wish it told me when I search on the wrong keywords for my problem. So I will put those keywords here, so a keyword search might lead you here. Hope it saves you the two hours I lost.

Image.PixelFormat
ImageFormatConverter
System.Drawing.Imaging.Imageformat
System.Windows.Media.Imaging ( yeah, my solution might have been there also, but wasn’t ready to use 3.0 for a Click Once deployment. )

And the error again for good measure...

A Graphics object cannot be created from an image that has an indexed pixel format.