c# - raw pixel array to gray scale BitmapImage -


i have array of raw pixel data. convert 8bpp bitmap.

public static bitmap bytetograybitmap(byte[] rawbytes, int width, int height)  {      bitmap bitmap = new bitmap(width, height, pixelformat.format8bppindexed);     bitmapdata  bitmapdata = bitmap.lockbits(new rectangle(0, 0, width, height),                                     imagelockmode.writeonly, bitmap.pixelformat);      marshal.copy(rawbytes, 0, bitmapdata .scan0, rawbytes.length);     bitmap.unlockbits(bitmapdata);      return bitmap; } 

bitmap looks color image instead of grayscale.

try adding before return bitmap:

for (int c = 0; c < bitmap.palette.entries.length; c++)     bitmap.palette.entries[c] = color.fromargb(c, c, c); 

it create typical grayscale palette.


Comments

Popular posts from this blog

node.js - Mongoose: Cast to ObjectId failed for value on newly created object after setting the value -

gradle error "Cannot convert the provided notation to a File or URI" -

python - NameError: name 'subprocess' is not defined -