如何将Kinect v2中保留的WriteableBitmap对象保存到Windows Store应用程序中的图像文件

本文关键字:Windows Store 文件 图像 应用程序 保存 WriteableBitmap Kinect v2 保留 对象 | 更新日期: 2023-09-27 17:49:39

我有一个windows应用程序c#程序,通过Kinect v2红外摄像头检测用户头部位置。

红外数据保存在WriteableBitmap中,我想将检测到的头部保存为HD上的图像文件。

我已经尝试了许多这样的解决方案,但都没有运气。

这是我的头部跟踪代码。irBitmap是我的WritableBitmap,一旦检测到头部,我想将其保存为图像文件。

      void msfr_MultiSourceFrameArrived(MultiSourceFrameReader sender, MultiSourceFrameArrivedEventArgs args)
    {
        using (MultiSourceFrame msf = args.FrameReference.AcquireFrame()) //acquiring frame. 
        {
            if (msf != null) 
            {
                using (BodyFrame bodyFrame = msf.BodyFrameReference.AcquireFrame()) //acquire the body frame now in the msf. 
                {
                    using (InfraredFrame irFrame = msf.InfraredFrameReference.AcquireFrame())
                    {
                        if (bodyFrame != null && irFrame != null)
                        {
                            irFrame.CopyFrameDataToArray(irData);
                            for (int i = 0; i < irData.Length; i++)
                            {
                                byte intensity = (byte)(irData[i] >> 8);
                                irDataConverted[i * 4] = intensity;
                                irDataConverted[i * 4 + 1] = intensity;
                                irDataConverted[i * 4 + 2] = intensity;
                                irDataConverted[i * 4 + 3] = 255;
                            }
                            irDataConverted.CopyTo(irBitmap.PixelBuffer);
                            irBitmap.Invalidate();
                            bodyFrame.GetAndRefreshBodyData(bodies); 
                            bodyCanvas.Children.Clear(); 
                            foreach (Body body in bodies) //now we go trough each of our bodies (in order to look for head) 
                            {
                                if (body.IsTracked) //Chek if the body is tracked 
                                {
                                    Joint headJoint = body.Joints[JointType.Head]; //If body is tracked we will get the head joint
                                    if (headJoint.TrackingState == TrackingState.Tracked) //We need to make sure with this bool that the head is tracked. 
                                    {
                                        DepthSpacePoint dps = sensor.CoordinateMapper.MapCameraPointToDepthSpace(headJoint.Position); //We create a debthspace point so we can draw that into our image. This is drawin in the image. It is the cameraspace point that goes to debthspace.  
                                        Ellipse headCircle = new Ellipse() {Width = 100, Height = 100, Fill =  new SolidColorBrush(Color.FromArgb(255,255,0,0))}; //Ellispe to draw the new heads. 
                                        bodyCanvas.Children.Add(headCircle); //Add the headcircle drawn circle to the canvas.
                                        Canvas.SetLeft(headCircle, dps.X-50); //and now we tell the canvas where to draw this shit. 
                                        Canvas.SetTop(headCircle, dps.Y-50); //       

                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }

我将感谢任何关于如何将WritableBitmap保存为硬盘上的图像文件的建议/帮助/建议。

如何将Kinect v2中保留的WriteableBitmap对象保存到Windows Store应用程序中的图像文件

我在一个类似的项目中使用了以下代码。这里的数据是来自kinect的原始图像流。

MemoryStream ms = new MemoryStream(data);
System.Drawing.Image img = System.Drawing.Image.FromStream(ms);
img.Save("./image" + (++imageSerial) + ".png", System.Drawing.Imaging.ImageFormat.Png);

谢谢你的回答。我使用以下代码保存了图像:

 StorageFile sampleFile = await Windows.Storage.KnownFolders.PicturesLibrary.CreateFileAsync(fileName , CreationCollisionOption.GenerateUniqueName);