async void ButtonPlayKiosk_Click(object sender, RoutedEventArgs e) { // -- Auto snap feature -- if (this.mediaCapture == null || !this.isPreviewActive) { return; } // get media stream properties from the capture device VideoEncodingProperties previewProperties = this.mediaCapture.VideoDeviceController.GetMediaStreamProperties(MediaStreamType.VideoPreview) as VideoEncodingProperties; for (int i = 0; i < maxFrameCount; ++i) { // create a single preview frame using the specified format VideoFrame videoFrameType = new VideoFrame(BitmapPixelFormat.Bgra8, (int)previewProperties.Width, (int)previewProperties.Height); VideoFrame previewFrame = await this.mediaCapture.GetPreviewFrameAsync(videoFrameType); SoftwareBitmap previewBitmap = previewFrame.SoftwareBitmap; previewFrame.Dispose(); previewFrame = null; if (previewBitmap != null) { int currImageIndex = (this.lastImageIndex + 1) % this.maxFrameCount; // check if previously captured frame should be released SoftwareBitmap existingBitmap = this.bitmapFrames[currImageIndex]; if (existingBitmap != null) { existingBitmap.Dispose(); } // set the current captured bitmap frame this.bitmapFrames[currImageIndex] = previewBitmap; // create image source, needed to assign to xaml Image element SoftwareBitmapSource imageSource = new SoftwareBitmapSource(); await imageSource.SetBitmapAsync(previewBitmap); // check if current xaml Image has previous image source associated Image currImage = (Image)this.stackPanelImages.Children[currImageIndex]; if (currImage.Source != null) { SoftwareBitmapSource releaseImageSource = (SoftwareBitmapSource)currImage.Source; releaseImageSource.Dispose(); currImage.Source = null; } // set current Image element bitmap source currImage.Source = imageSource; // update the last set image index this.lastImageIndex = currImageIndex; } await WaitMethod(this.frameDuration); } }
void CleanImageSource() { if (_sbs != null) { image.Source = null; _sbs.Dispose(); _softwareBitmap.Dispose(); _sbs = null; } }