//------------------------------------------------------------------------------ // // Copyright (c) Microsoft Corporation. All rights reserved. // //------------------------------------------------------------------------------ namespace Microsoft.Samples.AzureKinectBasics { using System; using System.ComponentModel; using System.Diagnostics; using System.Globalization; using System.IO; using System.Threading.Tasks; using System.Windows; using System.Windows.Media; using System.Windows.Media.Imaging; using Microsoft.Azure.Kinect.Sensor; using System.Collections.Generic; /// /// Interaction logic for MainWindow /// public partial class MainWindow : Window, INotifyPropertyChanged { /// /// Azure Kinect sensor /// private readonly Device kinect = null; /// /// Bitmap to display /// private readonly WriteableBitmap bitmap = null; /// /// Current status text to display /// private string statusText = null; /// /// The width in pixels of the color image from the Azure Kinect DK /// private readonly int colorWidth = 0; /// /// The height in pixels of the color image from the Azure Kinect DK /// private readonly int colorHeight = 0; /// /// Status of the application /// private bool running = true; /// /// Initializes a new instance of the MainWindow class. /// public MainWindow() { // Open the default device this.kinect = Device.Open(); // Configure camera modes this.kinect.StartCameras(new DeviceConfiguration { ColorFormat = ImageFormat.ColorBGRA32, ColorResolution = ColorResolution.R1080p, DepthMode = DepthMode.NFOV_2x2Binned, SynchronizedImagesOnly = true }); this.colorWidth = this.kinect.GetCalibration().ColorCameraCalibration.ResolutionWidth; this.colorHeight = this.kinect.GetCalibration().ColorCameraCalibration.ResolutionHeight; this.bitmap = new WriteableBitmap(colorWidth, colorHeight, 96.0, 96.0, PixelFormats.Bgra32, null); this.DataContext = this; 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.bitmap; } } /// /// Gets or sets the current status text to display /// public string StatusText { get { return this.statusText; } set { if (this.statusText != value) { this.statusText = value; 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) { running = false; if (this.kinect != null) { this.kinect.Dispose(); } } /// /// Handles the user clicking on the screenshot button /// /// object sending the event /// event arguments private void ScreenshotButton_Click(object sender, RoutedEventArgs e) { // Create a render target to which we'll render our composite image RenderTargetBitmap renderBitmap = new RenderTargetBitmap((int)CompositeImage.ActualWidth, (int)CompositeImage.ActualHeight, 96.0, 96.0, PixelFormats.Pbgra32); DrawingVisual dv = new DrawingVisual(); using (DrawingContext dc = dv.RenderOpen()) { VisualBrush brush = new VisualBrush(CompositeImage); dc.DrawRectangle(brush, null, new System.Windows.Rect(new Point(), new Size(CompositeImage.ActualWidth, CompositeImage.ActualHeight))); } renderBitmap.Render(dv); BitmapEncoder encoder = new PngBitmapEncoder(); encoder.Frames.Add(BitmapFrame.Create(renderBitmap)); 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-" + time + ".png"); // Write the new file to disk try { 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); } } private async void Window_Loaded(object sender, RoutedEventArgs e) { while (running) { using (Capture capture = await Task.Run(() => { return this.kinect.GetCapture(); })) { this.StatusText = "Received Capture: " + capture.Depth.DeviceTimestamp; this.bitmap.Lock(); var color = capture.Color; var region = new Int32Rect(0, 0, color.WidthPixels, color.HeightPixels); unsafe { using (var pin = color.Memory.Pin()) { this.bitmap.WritePixels(region, (IntPtr)pin.Pointer, (int)color.Size, color.StrideBytes); } } this.bitmap.AddDirtyRect(region); this.bitmap.Unlock(); } } } } }