0

I am trying to show a grayscale image. My understanding is that I need to set the pixel format to Format8bppIndexed, and then set up my own "colormap" by editing the palette.

Dim Width as integer = 50
Dim OutputArr(Width*Width - 1) as byte

'Generate some data
For i = 0 To Width * Width - 1
    OutputArr(i) = CByte(255 * Rnd())
Next

'Create a bitmap
Dim Bmp As New Bitmap(Width, Width, Imaging.PixelFormat.Format8bppIndexed)

'Load in some dummy data
Dim BmpData As Drawing.Imaging.BitmapData = Bmp.LockBits(
                                        New Drawing.Rectangle(0, 0, Width, Width),
                                        Drawing.Imaging.ImageLockMode.ReadWrite,
                                        Bmp.PixelFormat)
Marshal.Copy(OutputArr, 0, BmpData.Scan0, Width * Width)
Bmp.UnlockBits(BmpData)

'Edit the colormap
For i = 0 To 255
    Bmp.Palette.Entries(i) = Color.FromArgb(255, i, i, i)
Next

The image I get is colorful, though! I don't understand why: haven't I set the palette to only contain grayscale values? I have also tried using Color.FromArgb(i,i,i).

A colorful picture

RPM
  • 1,704
  • 12
  • 15
  • https://stackoverflow.com/questions/2265910/convert-an-image-to-grayscale – dr.null Jul 20 '22 at 17:44
  • Using VB.Net: [Analyze colors of an Image](https://stackoverflow.com/a/59102380/7444103), last snippet -- I suggest to use the ColorMatrix shown here, it gives better results. This is also DpiAware-friendly -- Similar, in C#, as an extension method: [How can I gray-out a disabled PictureBox used as Button?](https://stackoverflow.com/a/61584671/7444103) – Jimi Jul 20 '22 at 19:47
  • @dr.null, thanks for your help. The question you link to deals with converting an image to grayscale. My image is already grayscale (I have one byte / one color channel per pixel). I'm curious as to why it's still being displayed as color. – RPM Jul 21 '22 at 11:02
  • @Jimi. Using a ColorMatrix multiplies my ARGB vector by a matrix to get a new ARGB vector. But I don't have an ARGB vector, I'm trying to work with a grayscale image. – RPM Jul 21 '22 at 11:05
  • To be clear, I'm aware there are work-arounds, I'm trying to understand why my use of the palette above doesn't work. – RPM Jul 21 '22 at 11:06
  • Not sure why you're working with that Image Format; anyway, see the notes (and the code) here: [How to make colors in a Palette transparent when drawing 8bpp Bitmaps](https://stackoverflow.com/a/65498663/7444103) -- Also, take a close look at how `Bitmap.LockBits()` is handled there (with the same `PixelFormat.Format8bppIndexed` you have here). – Jimi Jul 21 '22 at 11:35
  • `Not sure why you're working with that Image Format;` : I have a grayscale camera that outputs 1 byte per pixel. – RPM Jul 21 '22 at 12:11
  • `Also, take a close look at how Bitmap.LockBits() is handled there (with the same PixelFormat.Format8bppIndexed you have here).` : ??? Do you mean that I should take the image stride into account? – RPM Jul 21 '22 at 12:13
  • Found the answer!!! I need to actually set the Palette property, rather than just editing elements. https://stackoverflow.com/questions/2593212/editing-8bpp-indexed-bitmaps?rq=1 – RPM Jul 21 '22 at 12:15
  • If you use `Bitmap.LockBits()`, you always need to *consider* the Stride: all Bitmaps bytes are aligned to a 4-bytes boundaries -- How you set the Palette entries is described in [How to make colors in a Palette transparent when drawing 8bpp Bitmaps](https://stackoverflow.com/a/65498663/7444103), you probably didn't read it. – Jimi Jul 21 '22 at 13:57

0 Answers0