Repository navigation
Expand file tree
/
Copy pathScreenCaptureService.cs
More file actions
68 lines (57 loc) · 2.45 KB
/
Copy pathScreenCaptureService.cs
File metadata and controls
68 lines (57 loc) · 2.45 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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
using System.IO;
namespace LightTranslate;
public sealed class CaptureRegion
{
public string ScreenDeviceName { get; set; } = string.Empty;
public int X { get; set; }
public int Y { get; set; }
public int Width { get; set; }
public int Height { get; set; }
public bool IsUsable => Width >= 8 && Height >= 8;
}
public sealed record CaptureSelectionResult(string ImagePath, CaptureRegion Region);
public static class ScreenCaptureService
{
public static CaptureSelectionResult CaptureRegion(CaptureRegion region)
{
if (!region.IsUsable)
throw new InvalidOperationException("还没有可重复使用的截图区域");
var screen = System.Windows.Forms.Screen.AllScreens.FirstOrDefault(candidate =>
candidate.DeviceName.Equals(region.ScreenDeviceName, StringComparison.OrdinalIgnoreCase))
?? System.Windows.Forms.Screen.FromPoint(new System.Drawing.Point(region.X, region.Y));
var requested = new System.Drawing.Rectangle(region.X, region.Y, region.Width, region.Height);
var clipped = System.Drawing.Rectangle.Intersect(requested, screen.Bounds);
if (clipped.Width < 8 || clipped.Height < 8)
throw new InvalidOperationException("上次选区已不在当前屏幕范围内,请重新框选");
using var bitmap = new System.Drawing.Bitmap(
clipped.Width,
clipped.Height,
System.Drawing.Imaging.PixelFormat.Format32bppArgb);
using (var graphics = System.Drawing.Graphics.FromImage(bitmap))
{
graphics.CopyFromScreen(
clipped.Left,
clipped.Top,
0,
0,
clipped.Size,
System.Drawing.CopyPixelOperation.SourceCopy);
}
var path = CreateTemporaryPath();
bitmap.Save(path, System.Drawing.Imaging.ImageFormat.Png);
return new CaptureSelectionResult(path, new CaptureRegion
{
ScreenDeviceName = screen.DeviceName,
X = clipped.X,
Y = clipped.Y,
Width = clipped.Width,
Height = clipped.Height
});
}
public static string CreateTemporaryPath()
{
var directory = Path.Combine(Path.GetTempPath(), "LightTranslate");
Directory.CreateDirectory(directory);
return Path.Combine(directory, $"capture-{DateTime.Now:yyyyMMdd-HHmmss-fff}-{Guid.NewGuid():N}.png");
}
}