This repository has been archived by the owner on May 15, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 501
/
Copy pathScreenshot.uwp.cs
68 lines (55 loc) · 2.22 KB
/
Screenshot.uwp.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
using System;
using System.IO;
using System.Runtime.InteropServices.WindowsRuntime;
using System.Threading.Tasks;
using Windows.Graphics.Imaging;
using Windows.Storage.Streams;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Media.Imaging;
namespace Xamarin.Essentials
{
public static partial class Screenshot
{
internal static bool PlatformIsCaptureSupported =>
true;
static async Task<ScreenshotResult> PlatformCaptureAsync()
{
var element = Window.Current?.Content as FrameworkElement;
if (element == null)
throw new InvalidOperationException("Unable to find main window content.");
var bmp = new RenderTargetBitmap();
// NOTE: Return to the main thread so we can access view properties such as
// width and height. Do not ConfigureAwait!
await bmp.RenderAsync(element);
// get the view information first
var width = bmp.PixelWidth;
var height = bmp.PixelHeight;
// then potentially move to a different thread
var pixels = await bmp.GetPixelsAsync().AsTask().ConfigureAwait(false);
return new ScreenshotResult(width, height, pixels);
}
}
public partial class ScreenshotResult
{
readonly byte[] bytes;
public ScreenshotResult(int width, int height, IBuffer pixels)
{
Width = width;
Height = height;
bytes = pixels?.ToArray() ?? throw new ArgumentNullException(nameof(pixels));
}
internal async Task<Stream> PlatformOpenReadAsync(ScreenshotFormat format)
{
var f = format switch
{
ScreenshotFormat.Jpeg => BitmapEncoder.JpegEncoderId,
_ => BitmapEncoder.PngEncoderId
};
var ms = new InMemoryRandomAccessStream();
var encoder = await BitmapEncoder.CreateAsync(f, ms).AsTask().ConfigureAwait(false);
encoder.SetPixelData(BitmapPixelFormat.Bgra8, BitmapAlphaMode.Ignore, (uint)Width, (uint)Height, 96, 96, bytes);
await encoder.FlushAsync().AsTask().ConfigureAwait(false);
return ms.AsStreamForRead();
}
}
}