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)
.