-1

I have a cam that is returning 10 bits pixels. I get an array that is double the size of the picture because it is using two 8 bit bytes to make one 10 bit integer.

so one pixel = two bytes .

So I have a working convert for that part. however, I need to display an array of integers in to a bitmap.

how do I do that ?

public static Bitmap ByteToImage(int[] blob)
    {
        MemoryStream mStream = new MemoryStream();
        int[] pData = blob;
        mStream.Write(pData, 0, Convert.ToInt32(pData.Length));

        Bitmap bm = new Bitmap(mStream, false);
        mStream.Dispose();
        return bm;
    }

I am getting an error on this one. but I really am not sure how to do this.

brandon
  • 71
  • 10
  • 1
    Explain what “display an array of integers in to a bitmap” means. Tell us what `blob` contains. Tell us what “error” your are getting. – Dour High Arch Jun 06 '19 at 22:47
  • I have an array int with values from 0 - 1023, that I am getting from a Camera. I need to convert that into a bitmap. blob is the array of ints – brandon Jun 06 '19 at 23:51
  • If the values are monochrome 10 bit, the best you can do is just shift them 2 bits down and end up with slightly less accurate 8-bit that you can perfectly display as `Format8bppIndexed` with a black-to-white fade colour palette. – Nyerguds Jul 06 '19 at 13:36

1 Answers1

1

You cannot just put a raw array of pixels into a stream and expect that to load as a bitmap. A bitmap needs headers and a stream of pixel data in a precise and word aligned format to load from a stream of bytes. You can form such a structure manually with some research and careful code and then load that into a Bitmap.

The easiest way to write a bitmap, however, is to create one using the new Bitmap(int width, int height, PixelFormat pxlFmt) constructor, and then use SetPixel() a bunch of times to set the pixels based on your 10 bit data coming in. Easy, but very slow.

If you need it to be faster, you can use Bitmap.LockBits to lock sections of your bitmap at a time and write copy big chunks from a byte array directly over the locked bits in the bitmap. This is much faster, but mich more advanced code (and you have to be very careful copying data to unmanaged memory to avoid writing beyond the locked region pointed to the pointer provided by LockBits). The pixel data will be similar to raw the bitmap data I discussed above, but at least you don't have to mess with BMP headers.

Tim
  • 5,715
  • 1
  • 9
  • 18
  • how do I use SetPixel? it wants a X and Y location and it want a color. I only have a 16 bit number – brandon Jun 17 '19 at 13:55
  • also X is your width and Y is your Height ? right ? – brandon Jun 17 '19 at 14:00
  • also what PixelFormat should I use ? – brandon Jun 17 '19 at 14:01
  • Also, my camera is a mono camera so it is really only black or white. How do I make a color? it want how much red, blue and green it is, but the camera only does back or white – brandon Jun 17 '19 at 14:04
  • @brandon SetPixel is a horrible idea here. It locks and unlocks the image data for every pixel it writes, making it ridiculously slow. You're better off constructing a byte array and loading that into a bitmap in one go. See [my answer here](https://stackoverflow.com/a/43967594/395685). – Nyerguds Jul 06 '19 at 13:35