-1

I have a grayscale image coming from a camera and I need to display it on a form However, C# is only able to convert 8bit grayscale images to 8BPP indexed image with 1:1 color palette, there is no Grayscale format in C# Bitmap

And when I try to draw a line using this:

using (var graphics = Graphics.FromImage(outputBitmap))
{
  Pen blackPen = new Pen(Color.Black, 3);
  graphics.DrawLine(blackPen, outputBitmap.Width/2, 0, outputBitmap.Width / 2, outputBitmap.Height-1);
}

I get an error that

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

So what do I do? I don't want to change the format or duplicate the image by copying pixels in memory.

I also cannot set pixels directly in 8BPP images

Is this really a limitation of C# and windows forms? That you cannot edit grayscale images?

Every API like OpenCV is able to edit 8 bit images without issues.

Mich
  • 3,188
  • 4
  • 37
  • 85
  • Does this answer your question? [Graphics on indexed image](https://stackoverflow.com/questions/17313285/graphics-on-indexed-image) – nbk Aug 18 '23 at 21:38
  • No, this is copying the data, which is very slow – Mich Aug 18 '23 at 21:41
  • then you should batch convert them in gimp prior – nbk Aug 18 '23 at 21:56
  • BitmapSource supports 8Bit Grayscale images. The [WriteableBitmapEx](https://github.com/reneschulte/WriteableBitmapEx) class has methods similar to GDI+, as `DrawLine()` – Jimi Aug 19 '23 at 10:04

0 Answers0