将Kinect ColorImageFrame转换为Bitmap

0xD*_*EEF 9 .net c# xna aforge kinect

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

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

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);

    ...
}
Run Code Online (Sandbox Code Playgroud)

Bot*_*000 11

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

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