FCI-Helwan blog

Just another FCI-H weblog

C# : indexed pixel problem

Here I will mention the solve for a common problem happens when you try to create Graphic object from Bitmap images as in the next two lines..yes, it would fire an exception if your bitmap was indexed pixel .

Bitmap tmp = new Bitmap(“Fci-h.jpg”);
Graphics grPhoto = Graphics.FromImage(tmp);

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

To pass this exception you will need to add some additional lines of code . Just create an image with the same size like the original image (Fci-h.jpg) then draw the original image onto the new Graphics object .

Here is an Example :

Bitmap bm = (Bitmap) System.Drawing.Image.FromFile(“Fci-h.jpg”,true);
Bitmap tmp=new Bitmap (bm.Width ,bm.Height );
Graphics grPhoto = Graphics.FromImage(tmp);
grPhoto.DrawImage(bm, new Rectangle(0, 0, tmp.Width , tmp.Height ), 0, 0, tmp.Width , tmp.Height , GraphicsUnit.Pixel);


February 18, 2008 - Posted by | C#, Microsoft, Tips


  1. Thanks for this.

    Comment by Juice | June 13, 2008 | Reply

  2. Worked perfectly! Thanks. Probably not very memory efficent but I’m just drawing a simple graph on a picture.

    Comment by Anonymous | February 16, 2009 | Reply

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: