Solution for "A Graphics Object cannot be Created from an Image that has an Indexed Pixel Format"

When you access/edit the following types of images we will get the error "a graphics object cannot be created from an image that has an indexed pixel format"

  • PixelFormatUndefined
  • PixelFormatDontCare
  • PixelFormat1bppIndexed
  • PixelFormat4bppIndexed
  • PixelFormat8bppIndexed
  • PixelFormat16bppGrayScale
  • PixelFormat16bppARGB1555

Solution for this problem:

The following code will fix the error.

try

{

    // create Image Object using rear image byte[]

    Image imag = Image.FromStream(new MemoryStream(imageR));

    // Derive BitMap object using Image instance, so that you can avoid the issue

    //"a graphics object cannot be created from an image that has an indexed pixel format"

    Bitmap img = new Bitmap(new Bitmap( imag ));

}

catch (Exception ex)

{

    MessageBox.Show(ex.Message);

}