简体   繁体   中英

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.

The infrared data is saved in a WriteableBitmap, and I want to save the detected head as an image file on my 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.

      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.

I've used following code for a similar project. Here, data is the raw image stream from 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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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