将Kinect ColorImageFrame转换为Bitmap

我在XNA上使用Kinect(Microsoft SDK)。 我想使用GRATF进行标记识别

如何将Kinect AForge.Imaging.UnmanagedImage的数据转换为可以使用GRATF处理它们的System.Drawing.Bitmap或AForge.Imaging.UnmanagedImage?

 void kinectSensor_ColorFrameReady(object sender, ColorImageFrameReadyEventArgs e) { Bitmap bitmap = null; ColorImageFrame frame = e.OpenColorImageFrame(); byte[] buffer = new byte[frame.PixelDataLength]; frame.CopyPixelData(buffer); // how to convert the data in buffer to a bitmap? var glyphs = recognizer.FindGlyphs(bitmap); ... } 

您可以在本文中找到答案。
总结一下,这种方法可以解决这个问题:

 Bitmap ImageToBitmap(ColorImageFrame Image) { byte[] pixeldata = new byte[Image.PixelDataLength]; Image.CopyPixelDataTo(pixeldata); Bitmap bmap = new Bitmap(Image.Width, Image.Height, PixelFormat.Format32bppRgb); BitmapData bmapdata = bmap.LockBits( new Rectangle(0, 0, Image.Width, Image.Height), ImageLockMode.WriteOnly, bmap.PixelFormat); IntPtr ptr = bmapdata.Scan0; Marshal.Copy(pixeldata, 0, ptr, Image.PixelDataLength); bmap.UnlockBits(bmapdata); return bmap; }