-
Notifications
You must be signed in to change notification settings - Fork 0
/
MainPage.xaml.cs
151 lines (125 loc) · 5.34 KB
/
MainPage.xaml.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
using Microsoft.Graphics.Canvas;
using Microsoft.Graphics.Canvas.UI.Xaml;
using System;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using System.Threading.Tasks;
using Windows.ApplicationModel.DataTransfer;
using Windows.Foundation;
using Windows.Graphics.DirectX;
using Windows.Graphics.Display;
using Windows.Graphics.Imaging;
using Windows.Media.Editing;
using Windows.Media.MediaProperties;
using Windows.Storage;
using Windows.Storage.Pickers;
using Windows.Storage.Streams;
using Windows.UI;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Media.Imaging;
namespace VideoBug
{
public sealed partial class MainPage : Page
{
public MainPage()
{
InitializeComponent();
}
private void ExportScreenshot(object sender, RoutedEventArgs e)
{
ExportScreenshotAsync(Colours);
}
private void CopyScreenshot(object sender, RoutedEventArgs e)
{
CopyScreenshotAsync(Colours);
}
private void ExportVideo(object sender, RoutedEventArgs e)
{
ExportVideoAsync(Colours);
}
public async void ExportScreenshotAsync(UIElement content)
{
FileSavePicker picker = new FileSavePicker()
{
SuggestedFileName = "Screenshot",
SuggestedStartLocation = PickerLocationId.Desktop,
CommitButtonText = "Save Screenshot"
};
picker.FileTypeChoices.Add("PNG Image", new string[] { ".png" });
var file = await picker.PickSaveFileAsync();
if (file != null)
{
await RenderContentToRasterStreamAsync(content, await file.OpenAsync(FileAccessMode.ReadWrite));
}
}
public static async Task RenderContentToRasterStreamAsync(UIElement content, IRandomAccessStream stream)
{
var renderTargetBitmap = new RenderTargetBitmap();
await renderTargetBitmap.RenderAsync(content);
var pixelBuffer = await renderTargetBitmap.GetPixelsAsync();
var pixels = pixelBuffer.ToArray();
var displayInformation = DisplayInformation.GetForCurrentView();
var encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.PngEncoderId, stream);
encoder.SetPixelData(BitmapPixelFormat.Bgra8,
BitmapAlphaMode.Premultiplied,
(uint)renderTargetBitmap.PixelWidth,
(uint)renderTargetBitmap.PixelHeight,
displayInformation.RawDpiX,
displayInformation.RawDpiY,
pixels);
await encoder.FlushAsync();
}
public static async void CopyScreenshotAsync(UIElement content)
{
var stream = new InMemoryRandomAccessStream();
await RenderContentToRasterStreamAsync(content, stream);
var dataPackage = new DataPackage();
dataPackage.SetBitmap(RandomAccessStreamReference.CreateFromStream(stream));
Clipboard.SetContent(dataPackage);
}
public async void ExportVideoAsync(UIElement content)
{
var picker = new FileSavePicker()
{
SuggestedFileName = "Video",
SuggestedStartLocation = PickerLocationId.Desktop,
CommitButtonText = "Save Video"
};
picker.FileTypeChoices.Add("H.264 video", new string[] { ".mp4" });
StorageFile file = await picker.PickSaveFileAsync();
if (file != null)
{
await RenderContentToVideo(file, content);
}
}
public async Task RenderContentToVideo(StorageFile file, UIElement content)
{
var renderTargetBitmap = new RenderTargetBitmap();
await renderTargetBitmap.RenderAsync(content);
var pixelBuffer = await renderTargetBitmap.GetPixelsAsync();
var pixels = pixelBuffer.ToArray();
var rendertarget = new CanvasRenderTarget(CanvasDevice.GetSharedDevice(), 400, 400, 96);
rendertarget.SetPixelBytes(pixels, 0, 0, renderTargetBitmap.PixelWidth, renderTargetBitmap.PixelHeight);
var composition = new MediaComposition();
composition.Clips.Add(MediaClip.CreateFromSurface(rendertarget, TimeSpan.FromSeconds(0)));
composition.Clips.Add(MediaClip.CreateFromSurface(rendertarget, TimeSpan.FromSeconds(1)));
var profile = MediaEncodingProfile.CreateMp4(VideoEncodingQuality.HD1080p);
await composition.RenderToFileAsync(file, MediaTrimmingPreference.Fast, profile);
}
private void RedrawCanvas(CanvasControl sender, CanvasDrawEventArgs args)
{
args.DrawingSession.FillRectangle(0, 0, 200, 50, Colors.Red);
args.DrawingSession.FillRectangle(0, 50, 200, 50, Colors.Green);
args.DrawingSession.FillRectangle(0, 100, 200, 50, Colors.Blue);
var colours = new byte[]
{
255, 0, 0, 0,
0, 255, 0, 0,
0, 0, 255, 0
};
CanvasBitmap bitmap = CanvasBitmap.CreateFromBytes(args.DrawingSession, colours, 1, 3, DirectXPixelFormat.R8G8B8A8UIntNormalized);
args.DrawingSession.DrawImage(bitmap, new Rect(0, 150, 200, 150));
}
}
}