Repository navigation
Expand file tree
/
Copy pathProgram.cs
More file actions
48 lines (38 loc) · 1.6 KB
/
Copy pathProgram.cs
File metadata and controls
48 lines (38 loc) · 1.6 KB
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
using ThatCapture;
const int CaptureX = 0;
const int CaptureY = 0;
const int CaptureWidth = 320;
const int CaptureHeight = 240;
const string OutDir = "out";
Directory.CreateDirectory(OutDir);
var capture = new ScreenCapture();
Console.WriteLine($"Platform: {capture.Platform}");
var result = await capture.CaptureAreaAsync(CaptureX, CaptureY, CaptureWidth, CaptureHeight);
if (result is not CaptureResult.Ok(var frame))
{
var err = ((CaptureResult.Err)result).Error;
Console.WriteLine($"FAIL capture failed: {err}");
return;
}
Console.WriteLine($"Frame: {frame.Width}x{frame.Height}, stride={frame.Stride}, format={frame.Format}");
Console.WriteLine();
await RunTest("PNG (default)", () => frame.ToPng(), Path.Combine(OutDir, "out.png"));
await RunTest("JPEG q=85 (default)", () => frame.ToJpeg(), Path.Combine(OutDir, "out_q85.jpg"));
await RunTest("JPEG q=60", () => frame.ToJpeg(quality: 60), Path.Combine(OutDir, "out_q60.jpg"));
await RunTest("WebP lossy (default)", () => frame.ToWebp(), Path.Combine(OutDir, "out_lossy.webp"));
await RunTest("WebP lossless", () => frame.ToWebp(lossless: true), Path.Combine(OutDir, "out_lossless.webp"));
Console.WriteLine();
Console.WriteLine($"Output files written to: {Path.GetFullPath(OutDir)}");
static async Task RunTest(string label, Func<byte[]> encode, string path)
{
try
{
var bytes = encode();
await File.WriteAllBytesAsync(path, bytes);
Console.WriteLine($" OK {label,-28} {bytes.Length,8:N0} bytes → {path}");
}
catch (Exception ex)
{
Console.WriteLine($" FAIL {label,-28} {ex.Message}");
}
}