MultiSourceFrameReader myReader;
// open reader for frame source, specify which streams to be used
myReader = mySensor.OpenMultiSourceFrameReader(FrameSourceTypes.Body | FrameSourceTypes.Depth | FrameSourceTypes.Color);
myReader.MultiSourceFrameArrived += MultiSouceFrameArrived;
private void MultiSouceFrameArrived(object sender, MultiSourceFrameArrivedEventArgs e)
{
...
}
var frame = e.FrameReference.AcquireFrame();
using (BodyFrame bdFrame = frame.BodyFrameReference.AcquireFrame())
{
...
}
byte[] colorFramePixels = new byte[fd.Width * fd.Height * 4];
frame.CopyConvertedFrameDataToArray(colorFramePixels, ColorImageFormat.Bgra);
frame.CopyRawFrameDataToArray(colorFramePixels);
FrameDescription fd = sensor.InfraredFrameSource.FrameDescription;
irBitmap = new WriteableBitmap(fd.Width, fd.Height);
Create a bitmap to store the pixel data.
private WriteableBitmap colorBitmap;
this.colorBitmap = new WriteableBitmap(this.sensor.ColorStream.FrameWidth, this.sensor.ColorStream.FrameHeight, 96.0, 96.0, PixelFormats.Bgr32, null);
A WriteableBitmap is a Windows Presentation Foundation (WPF) construct, available in .NET Framework 4, that allows access to the bitmap bits. You could create a Bitmap every frame, but you will get much better performance creating a WriteableBitmap only when the pixel format changes, such as when you start the application.
After getting the color data and saving it in the WriteableBitmap, draw the WriteableBitmap.
if (colorFrame != null)
{
// Write the pixel data into our bitmap
this.colorBitmap.WritePixels(
new Int32Rect(0, 0, this.colorBitmap.PixelWidth, this.colorBitmap.PixelHeight),
this.colorPixels,
this.colorBitmap.PixelWidth * sizeof(int),
0);
}
Use the WriteableBitmap.WritePixels method to copy the data from the local array to the WriteableBitmap.