Nie znam się ale poczytaj na przykład to:
https://social.msdn.microsoft.com/Forums/en-US/f84cf487-fcef-4e32-aebe-e3c152464b8f/converting-raw-8bit-blackwhite-to-bmp?forum=csharpgeneral
lub to (chyba bardziej przypomina Twój problem)
https://stackoverflow.com/questions/52439389/create-8-bit-bitmap-from-array
A tutaj masz gotowiec, niestety 12bit RAW Data, ale nietrudno to przerobić na 8bit
https://stackoverflow.com/questions/6832948/c-sharp-converting-8-bit-or-16-bit-grayscale-raw-pixel-data
Przerobiłem gotowiec pod swoje potrzeby i kod teraz wygląda tak:
public class RAW2Image
{
/// <summary>
/// Konwersja tablicy bitowej do bitmapy
/// </summary>
/// <param name="input">Tablica byte</param>
/// <param name="width">szerokość</param>
/// <param name="height">wysokość</param>
/// <param name="bit">ilość bitów</param>
/// <returns></returns>
public static Bitmap Convert(byte[] input, int width, int height, int bit)
{
Bitmap bitmap = null;
switch (bit)
{
case 8:
bitmap = new Bitmap(width, height, PixelFormat.Format8bppIndexed);
break;
case 16:
bitmap = new Bitmap(width, height, PixelFormat.Format16bppGrayScale);
break;
case 24:
bitmap = new Bitmap(width, height, PixelFormat.Format24bppRgb);
break;
case 32:
bitmap = new Bitmap(width, height, PixelFormat.Format32bppRgb);
break;
}
var rect = new Rectangle(0, 0, width, height);
var lut = CreateLut(bit);
var bitmap_data = bitmap.LockBits(rect, ImageLockMode.WriteOnly, bitmap.PixelFormat);
ConvertCore(width, height, bit, input, bitmap_data, lut);
bitmap.UnlockBits(bitmap_data);
return bitmap;
}
private static uint[] CreateLut(int bits)
{
// Create a linear LUT to convert from grayscale to ARGB
int max_input = 1 << bits;
uint[] lut = new uint[max_input];
for (int i = 0; i < max_input; i++)
{
// map input value to 8-bit range
//
byte intensity = (byte)((i * 0xFF) / max_input);
// create ARGB output value A=255, R=G=B=intensity
//
lut[i] = (uint)(0xFF000000L | (intensity * 0x00010101L));
}
return lut;
}
private static unsafe void ConvertCore(int width, int height, int bits, byte[] input, BitmapData output, uint[] lut)
{
// Copy pixels from input to output, applying LUT
ushort mask = (ushort)((1 << bits) - 1);
int in_stride = output.Stride;
int out_stride = width * 2;
byte* out_data = (byte*)output.Scan0;
fixed (byte* in_data = input)
{
for (int y = 0; y < height; y++)
{
uint* out_row = (uint*)(out_data + (y * in_stride));
ushort* in_row = (ushort*)(in_data + (y * out_stride));
for (int x = 0; x < width; x++)
{
ushort in_pixel = (ushort)(in_row[x] & mask);
out_row[x] = lut[in_pixel];
}
}
}
}
}
Kod zwraca wyjątek:
Co do tego Exception
znalazłem takie rozwiązanie: https://stackoverflow.com/questions/13884503/bitmap-lockbits-error-parameter-is-not-valid-when-bitmap-is-a-certain-size
Jednak ono wyrzuciło znowu takie Exception