in a university project my team and I have to build a scanning unit. After processing the scanning unit returns a Byte Array consisting of 8-bit grayscale values (approx 3,7k Pixels in the Heigth and 20k Pixels in the Width, this huge resolution is necessary, so setting PixelData Pixelwise is no option as this would take to much time).
While searching the internet i have found an answer in This SO topic. I tried it implementing as the following:
public static class Extensions
{
public static Image ImageFromArray(
this byte[] arr, int width, int height)
{
var output = new Bitmap(width, height, PixelFormat.Format16bppGrayScale);
var rect = new Rectangle(0, 0, width, height);
var bmpData = output.LockBits(rect,
ImageLockMode.ReadWrite, output.PixelFormat);
var ptr = bmpData.Scan0;
Marshal.Copy(arr, 0, ptr, arr.Length);
output.UnlockBits(bmpData);
return output;
}
}
I tried running the Function with an array filled like this:
testarray[0] = 200;
testarray[1] = 255;
testarray[2] = 90;
testarray[3] = 255;
testarray[4] = 0;
testarray[5] = 0;
testarray[6] = 100;
testarray[7] = 155;
And wanted to show the bmp in a picturebox with:
pictureBox1.Image = Extensions.ImageFromArray(testarray, 2, 2);
When debugging, the code runs trough the function, but the result is the Errorimage showing.
Also, if someone knows an easier method to compute the task, it would be very nice to share the way. I could not find a way to
- Use a 8-bit-grayscale Pixelformat in C#
- Create a BitMap Object including a Stream AND the PixelFormat
Thank you very much for your help :)