[ACCEPTED]-Problem with PNG images in C#-png

Accepted answer
Score: 12

You cannot create a graphics from an indexed 3 image format (PNG, GIF,...). You should 2 use a Bitmap (file or convert your image 1 to a bitmap).

Image img = Image.FromFile("file.png");
img = new Bitmap(img);
newGraphics = Graphics.FromImage(img);
Score: 10

Without a better PNG library that supports 11 indexed PNGs you're out of luck trying to 10 draw to that image since evidently the GDI+ graphics 9 object doesn't support indexed images.

If 8 you don't need to use indexed PNGs you could 7 trap that error and convert your input to 6 regular RGB PNGs using a 3rd party utility.

edit:

I 5 did find this link http://fci-h.blogspot.com/2008/02/c-indexed-pixel-problem.html that gives a method 4 to draw on your image, however it won't 3 affect the original, just a copy you can 2 Save() if you require.

In case the link goes 1 down:

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);

More Related questions