Displaying the image - Windows Forms

The next big problem is finding a way to display a depth frame.

As the data take the form of a 13-bit image the most obvious thing to do is convert it into a grey scale image.

This is where we hit a small snag. While Windows Forms supports the Format16bppGrayScale constant it doesn't actually support the type. That is you can't load up a Bitmap object with grey scale data and then display it using a PictureBox - it just shows as a big red cross.

So what can we do?

One answer, although it is a bit of a cheat, is to simply use the Format16bppRgb555 format which is supported. This treats a 16-bit value so that 5 bits are used for each of R,G and B and the topmost bit is ignored.

We can use the same technique as explained in the previous chapter to move the data into a Bitmap.

This is a quick and easy way to get a false color display of the depth data but it isn't particularly good as the B channel isn't used because of the bottom three bits always being zero.

We can improve it slightly by shifting each depth value by 3 bits to the right. The reason is that the color is determined by low order 15 bits but the depth field is the high 13-bits. This means that the top depth bit and the low order three color bits aren't used. Shifting three bits to the right matches the bits up in a much better way

pixelData[i] =(short)(((ushort) pixelData[i])>>3);

We have to make sure that we perform a logical shift right hence the casts.

This shift has to be performed on each of the elements so we need a for loop: