Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Graphics on indexed image

I am getting error:

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

in function:

public static void AdjustImage(ImageAttributes imageAttributes, Image image) {         Rectangle rect = new Rectangle(0, 0, image.Width, image.Height);          Graphics g = Graphics.FromImage(image);                g.InterpolationMode = InterpolationMode.HighQualityBicubic;         g.DrawImage(image, rect, 0, 0, image.Width, image.Height, GraphicsUnit.Pixel, imageAttributes);         g.Dispose(); } 

I would like to ask you, how can I fix it?

like image 421
Krivers Avatar asked Jun 26 '13 06:06

Krivers


People also ask

What is indexed image in image processing?

An indexed image uses direct mapping of pixel values to colormap values. The color of each image pixel is determined by using the corresponding value of X as an index into map . The value 1 points to the first row in map , the value 2 points to the second row, and so on.


2 Answers

Refering to this, it can be solved by creating a blank bitmap with the same dimensions and the correct PixelFormat and the draw on that bitmap.

// The original bitmap with the wrong pixel format.  // You can check the pixel format with originalBmp.PixelFormat Bitmap originalBmp = new (Bitmap)Image.FromFile("YourFileName.gif");  // Create a blank bitmap with the same dimensions Bitmap tempBitmap = new Bitmap(originalBmp.Width, originalBmp.Height);  // From this bitmap, the graphics can be obtained, because it has the right PixelFormat using(Graphics g = Graphics.FromImage(tempBitmap)) {     // Draw the original bitmap onto the graphics of the new bitmap     g.DrawImage(originalBmp, 0, 0);     // Use g to do whatever you like     g.DrawLine(...); }  // Use tempBitmap as you would have used originalBmp return tempBitmap; 
like image 135
Microsoft DN Avatar answered Oct 01 '22 10:10

Microsoft DN


The simplest way is to create a new image like this:

Bitmap EditableImg = new Bitmap(IndexedImg); 

It creates a new image exactly like the original was with all its contents.

like image 43
Bart Vanseer Avatar answered Oct 01 '22 12:10

Bart Vanseer