Indexed pixel format problem in GDI+

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

this exception happed in my application while trying to draw string on a .gif image,

I read about this problem and found in MSDN that: If the image has an indexed pixel format, this method throws an exception with the message, "A Graphics object cannot be created from an image that has an indexed pixel format." The indexed pixel formats are shown in the following list.

And found hundreds of articles talking about solving this problem, some a little complex and some really simple, i tested the simple way:

 

 

C# : indexed pixel problem

And its simply worked, just create a temp Bitmap and use it to create the Graphics.

Strange but worked!!

One thought on “Indexed pixel format problem in GDI+

  1. You’re only up-scaling the gif to a 24bpp PNG this way. It slows down your code dramatically… you have to be very careful.. What complex methods did you get?

    PInvoke and getting access to low level methods?

Comments are closed.