public ControlExample() { this.InitializeComponent(); Substitutions = new List <ControlExampleSubstitution>(); ControlPresenter.RegisterPropertyChangedCallback(ContentPresenter.PaddingProperty, ControlPaddingChangedCallback); }
public async void TakeScreenshotWithDelay() { // 3 second countdown for (int i = 3; i > 0; i--) { ScreenshotStatusTextBlock.Text = i.ToString(); await Task.Delay(1000); } ScreenshotStatusTextBlock.Text = "Image captured"; // AppRecordingManager is desktop-only, and its use here is quite hacky, // but it is able to capture popups (though not theme shadows). bool isAppRecordingPresent = ApiInformation.IsTypePresent("Windows.Media.AppRecording.AppRecordingManager"); if (!isAppRecordingPresent) { // Better than doing nothing TakeScreenshot(); } else { var manager = AppRecordingManager.GetDefault(); if (manager.GetStatus().CanRecord) { var result = await manager.SaveScreenshotToFilesAsync( ApplicationData.Current.LocalFolder, "appScreenshot", AppRecordingSaveScreenshotOption.HdrContentVisible, manager.SupportedScreenshotMediaEncodingSubtypes); if (result.Succeeded) { // Open the screenshot back up var screenshotFile = await ApplicationData.Current.LocalFolder.GetFileAsync("appScreenshot.png"); using (var stream = await screenshotFile.OpenAsync(FileAccessMode.Read)) { var decoder = await BitmapDecoder.CreateAsync(stream); // Find the control in the picture GeneralTransform t = ControlPresenter.TransformToVisual(Window.Current.Content); Point pos = t.TransformPoint(new Point(0, 0)); ; if (!CoreApplication.GetCurrentView().TitleBar.ExtendViewIntoTitleBar) { // Add the height of the title bar, which I really wish was programmatically available anywhere. pos.Y += 32.0; } // Crop the screenshot to the control area var transform = new BitmapTransform() { Bounds = new BitmapBounds() { X = (uint)(Math.Ceiling(pos.X)) + 1, // Avoid the 1px window border Y = (uint)(Math.Ceiling(pos.Y)) + 1, Width = (uint)ControlPresenter.ActualWidth - 1, // Rounding issues -- this avoids capturing the control border Height = (uint)ControlPresenter.ActualHeight - 1 } }; var softwareBitmap = await decoder.GetSoftwareBitmapAsync( decoder.BitmapPixelFormat, BitmapAlphaMode.Ignore, transform, ExifOrientationMode.IgnoreExifOrientation, ColorManagementMode.DoNotColorManage); // Save the cropped picture var file = await ApplicationData.Current.LocalFolder.CreateFileAsync(GetBestScreenshotName(), CreationCollisionOption.ReplaceExisting); using (var outStream = await file.OpenAsync(FileAccessMode.ReadWrite)) { BitmapEncoder encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.PngEncoderId, outStream); encoder.SetSoftwareBitmap(softwareBitmap); await encoder.FlushAsync(); } } // Delete intermediate file await screenshotFile.DeleteAsync(); } } } await Task.Delay(1000); ScreenshotStatusTextBlock.Text = ""; }