简体   繁体   English

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

[英]How to save a WriteableBitmap obejct retained from Kinect v2 to an image file in Windows Store apps

I have a windows app c# program that via Kinect v2 Infrared camera detects the users head location. 我有一个Windows应用程序c#程序,该程序通过Kinect v2红外摄像头检测到用户头部的位置。

The infrared data is saved in a WriteableBitmap, and I want to save the detected head as an image file on my HD. 红外数据保存在WriteableBitmap中,我想将检测到的头部保存为HD上的图像文件。

I have tried a number of solutions such as this with no luck. 我已经尝试了很多这样的解决方案, 但是都没有运气。

This is my head tracking code as it is. 这是我的头部追踪代码。 irBitmap is my WritableBitmap which I want to save as a image file once the head has been detected. 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); //       



                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }

I would appreciate any advice/help/suggestion on how to save a WritableBitmap as a image file on my hard drive. 对于如何将WritableBitmap作为图像文件保存在硬盘上的任何建议/帮助/建议,我将不胜感激。

I've used following code for a similar project. 我已将以下代码用于类似的项目。 Here, data is the raw image stream from kinect. 在这里,数据是来自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);

Thanks for your answer. 感谢您的回答。 I managed to save the image using the following code: 我设法使用以下代码保存图像:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM