diff --git a/Color_source.cs b/Color_source.cs new file mode 100644 index 0000000..7e615c3 --- /dev/null +++ b/Color_source.cs @@ -0,0 +1,230 @@ +//------------------------------------------------------------------------------ +// +// Copyright (c) Microsoft Corporation. All rights reserved. +// +//------------------------------------------------------------------------------ +namespace Microsoft.Samples.Kinect.ColorBasics +{ + using System; + using System.ComponentModel; + using System.Diagnostics; + using System.Globalization; + using System.IO; + using System.Windows; + using System.Windows.Media; + using System.Windows.Media.Imaging; + using Microsoft.Kinect; + + /// + /// Interaction logic for MainWindow + /// + public partial class MainWindow : Window, INotifyPropertyChanged + { + /// + /// Active Kinect sensor + /// + private KinectSensor kinectSensor = null; + + /// + /// Reader for color frames + /// + private ColorFrameReader colorFrameReader = null; + + /// + /// Bitmap to display + /// + private WriteableBitmap colorBitmap = null; + + /// + /// Current status text to display + /// + private string statusText = null; + + /// + /// Initializes a new instance of the MainWindow class. + /// + public MainWindow() + { + // get the kinectSensor object + this.kinectSensor = KinectSensor.GetDefault(); + + sensor1 = kinectSensor.ColorFrameSource.CreateFrameDescription; + + // open the reader for the color frames + this.colorFrameReader = this.kinectSensor.ColorFrameSource.OpenReader(); + + // wire handler for frame arrival + this.colorFrameReader.FrameArrived += this.Reader_ColorFrameArrived; + + // create the colorFrameDescription from the ColorFrameSource using Bgra format + FrameDescription colorFrameDescription = this.kinectSensor.ColorFrameSource.CreateFrameDescription(ColorImageFormat.Bgra); + + // create the bitmap to display + this.colorBitmap = new WriteableBitmap(colorFrameDescription.Width, colorFrameDescription.Height, 96.0, 96.0, PixelFormats.Bgr32, null); + + // set IsAvailableChanged event notifier + this.kinectSensor.IsAvailableChanged += this.Sensor_IsAvailableChanged; + + // open the sensor + this.kinectSensor.Open(); + + // set the status text + this.StatusText = this.kinectSensor.IsAvailable ? Properties.Resources.RunningStatusText + : Properties.Resources.NoSensorStatusText; + + // use the window object as the view model in this simple example + this.DataContext = this; + + // initialize the components (controls) of the window + this.InitializeComponent(); + } + + /// + /// INotifyPropertyChangedPropertyChanged event to allow window controls to bind to changeable data + /// + public event PropertyChangedEventHandler PropertyChanged; + + /// + /// Gets the bitmap to display + /// + public ImageSource ImageSource + { + get + { + return this.colorBitmap; + } + } + + /// + /// Gets or sets the current status text to display + /// + public string StatusText + { + get + { + return this.statusText; + } + + set + { + if (this.statusText != value) + { + this.statusText = value; + + // notify any bound elements that the text has changed + if (this.PropertyChanged != null) + { + this.PropertyChanged(this, new PropertyChangedEventArgs("StatusText")); + } + } + } + } + + /// + /// Execute shutdown tasks + /// + /// object sending the event + /// event arguments + private void MainWindow_Closing(object sender, CancelEventArgs e) + { + if (this.colorFrameReader != null) + { + // ColorFrameReder is IDisposable + this.colorFrameReader.Dispose(); + this.colorFrameReader = null; + } + + if (this.kinectSensor != null) + { + this.kinectSensor.Close(); + this.kinectSensor = null; + } + } + + /// + /// Handles the user clicking on the screenshot button + /// + /// object sending the event + /// event arguments + private void ScreenshotButton_Click(object sender, RoutedEventArgs e) + { + if (this.colorBitmap != null) + { + // create a png bitmap encoder which knows how to save a .png file + BitmapEncoder encoder = new PngBitmapEncoder(); + + // create frame from the writable bitmap and add to encoder + encoder.Frames.Add(BitmapFrame.Create(this.colorBitmap)); + + string time = System.DateTime.Now.ToString("hh'-'mm'-'ss", CultureInfo.CurrentUICulture.DateTimeFormat); + + string myPhotos = Environment.GetFolderPath(Environment.SpecialFolder.MyPictures); + + string path = Path.Combine(myPhotos, "KinectScreenshot-Color-" + time + ".png"); + + // write the new file to disk + try + { + // FileStream is IDisposable + using (FileStream fs = new FileStream(path, FileMode.Create)) + { + encoder.Save(fs); + } + + this.StatusText = string.Format(Properties.Resources.SavedScreenshotStatusTextFormat, path); + } + catch (IOException) + { + this.StatusText = string.Format(Properties.Resources.FailedScreenshotStatusTextFormat, path); + } + } + } + + /// + /// Handles the color frame data arriving from the sensor + /// + /// object sending the event + /// event arguments + private void Reader_ColorFrameArrived(object sender, ColorFrameArrivedEventArgs e) + { + // ColorFrame is IDisposable + using (ColorFrame colorFrame = e.FrameReference.AcquireFrame()) + { + if (colorFrame != null) + { + FrameDescription colorFrameDescription = colorFrame.FrameDescription; + + using (KinectBuffer colorBuffer = colorFrame.LockRawImageBuffer()) + { + this.colorBitmap.Lock(); + + // verify data and write the new color frame data to the display bitmap + if ((colorFrameDescription.Width == this.colorBitmap.PixelWidth) && (colorFrameDescription.Height == this.colorBitmap.PixelHeight)) + { + colorFrame.CopyConvertedFrameDataToIntPtr( + this.colorBitmap.BackBuffer, + (uint)(colorFrameDescription.Width * colorFrameDescription.Height * 4), + ColorImageFormat.Bgra); + + this.colorBitmap.AddDirtyRect(new Int32Rect(0, 0, this.colorBitmap.PixelWidth, this.colorBitmap.PixelHeight)); + } + + this.colorBitmap.Unlock(); + } + } + } + } + + /// + /// Handles the event which the sensor becomes unavailable (E.g. paused, closed, unplugged). + /// + /// object sending the event + /// event arguments + private void Sensor_IsAvailableChanged(object sender, IsAvailableChangedEventArgs e) + { + // on failure, set the status text + this.StatusText = this.kinectSensor.IsAvailable ? Properties.Resources.RunningStatusText + : Properties.Resources.SensorNotAvailableStatusText; + } + } +}