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

    Question

  • I have used this Overlay function with tif files I create. However, when I download a "real" tif image from our repository, I get the subject line error.

    1. What is an indexed pixel format?

    2. Can I convert it in my code to an acceptible format? or use a different object to accomplish the text overlay header?

            public void Overlay(string fn, string accn)
            {
                string dir = Path.GetDirectoryName(fn);
                string temp = Path.Combine(dir, "temp.tif");
                File.Delete(temp);
                File.Move(fn, temp);
                string msg = "Accn:" + accn;
                FileStream fs = File.Open(temp, FileMode.Open, FileAccess.Read);
                Bitmap bm = (Bitmap)Bitmap.FromStream(fs);
                Graphics graphics = Graphics.FromImage(bm);
                SolidBrush brush = new SolidBrush(Color.Black);
                Font font = new Font("Courier New", 20, FontStyle.Bold);
                Rectangle rect = new Rectangle(8, 8, 300, 80);
                graphics.DrawString(msg, font, brush, rect);
                fs.Close();            
                bm.Save(fn);            
            }
    Thanks,

    Jon Jacobs
    Not sent from my iphone, because I don't have one.

    Saturday, April 20, 2013 1:55 AM

Answers

  • An indexed image is an image with a palette.  GDI+ doesn't support drawing to indexed images.  Draw your indexed image on a non-indexed image and then draw your text on the non-indexed image:

          Bitmap newBitmap = new Bitmap(bm.Width, bm.Height);
          
    Graphics graphics = Graphics.FromImage(newBitmap);
          graphics.DrawImage(bm, 0, 0);

    Saturday, April 20, 2013 6:47 PM
  • Hi.

    When a "normal" pixel format works somewhat like this: 

    RGB RGB RGB RGB RGB
    
    RGB RGB RGB RGB RGB
    
    RGB RGB RGB RGB RGB
    
    RGB RGB RGB RGB RGB
    
    RGB RGB RGB RGB RGB


    An indexed one defines a palette of colors as an array, and then references those indexes instead of defining a color at each pixel co-ordinate, effectively saving memory and disk space.

    P[] = { RGB, RGB, RGB, RGB, RGB }
    
    P[0] P[3] P[0] P[2] P[1]
    
    P[1] P[2] P[4] P[1] P[0]
    
    P[2] P[3] P[1] P[2] P[2]
    
    P[0] P[3] P[2] P[3] P[1]
    
    P[4] P[3] P[0] P[2] P[1]

    About your problem, I'm guessing that the exception gets thrown at this line: 

    Graphics graphics = Graphics.FromImage(bm);

    According to the documentation of Graphics.FromImage(the remarks section), you can save the indexed image as another format by using the Save(String, ImageFormat) method and then retrieve a Graphics object for the new image.

    So go ahead and try that! Convert the indexed TIFF to, say, PNG, and then call Graphics.FromImage on the converted image.


    Teo Selenius

    • Marked as answer by Jon Q Jacobs Monday, April 22, 2013 1:28 PM
    Saturday, April 20, 2013 5:22 AM

All replies

  • Hi.

    When a "normal" pixel format works somewhat like this: 

    RGB RGB RGB RGB RGB
    
    RGB RGB RGB RGB RGB
    
    RGB RGB RGB RGB RGB
    
    RGB RGB RGB RGB RGB
    
    RGB RGB RGB RGB RGB


    An indexed one defines a palette of colors as an array, and then references those indexes instead of defining a color at each pixel co-ordinate, effectively saving memory and disk space.

    P[] = { RGB, RGB, RGB, RGB, RGB }
    
    P[0] P[3] P[0] P[2] P[1]
    
    P[1] P[2] P[4] P[1] P[0]
    
    P[2] P[3] P[1] P[2] P[2]
    
    P[0] P[3] P[2] P[3] P[1]
    
    P[4] P[3] P[0] P[2] P[1]

    About your problem, I'm guessing that the exception gets thrown at this line: 

    Graphics graphics = Graphics.FromImage(bm);

    According to the documentation of Graphics.FromImage(the remarks section), you can save the indexed image as another format by using the Save(String, ImageFormat) method and then retrieve a Graphics object for the new image.

    So go ahead and try that! Convert the indexed TIFF to, say, PNG, and then call Graphics.FromImage on the converted image.


    Teo Selenius

    • Marked as answer by Jon Q Jacobs Monday, April 22, 2013 1:28 PM
    Saturday, April 20, 2013 5:22 AM
  • An indexed image is an image with a palette.  GDI+ doesn't support drawing to indexed images.  Draw your indexed image on a non-indexed image and then draw your text on the non-indexed image:

          Bitmap newBitmap = new Bitmap(bm.Width, bm.Height);
          
    Graphics graphics = Graphics.FromImage(newBitmap);
          graphics.DrawImage(bm, 0, 0);

    Saturday, April 20, 2013 6:47 PM