【发布时间】:2022-01-06 03:00:02
【问题描述】:
我正在尝试根据鼠标的 X、Y 位置截屏一个矩形。我的代码有几个问题:
1.)我希望鼠标光标位于矩形的中心(我不需要屏幕截图中的光标)。
2.) 屏幕截图在第一台显示器上看起来不错,但在第二台显示器上屏幕截图似乎有偏移。偏移的原因是监视器 #2 的 Scale And Layout 设置为 125%。
对此的任何帮助将不胜感激。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using System.Windows;
using System.Drawing;
using System.Drawing.Imaging;
namespace Test
{
public static class MouseScreenCapture
{
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
internal static extern bool GetCursorPos(out POINT pt);
[StructLayout(LayoutKind.Sequential)]
public struct POINT
{
public int X;
public int Y;
public static implicit operator System.Drawing.Point(POINT point)
{
return new System.Drawing.Point(point.X, point.Y);
}
}
static void Main(string[] args)
{
Start();
}
private static System.Threading.Timer _timer = null;
public static void Start()
{
_timer = new System.Threading.Timer(TimerCallback, null, 0, 5000);
}
private static void TimerCallback(Object o)
{
GetMousePositionAndScreenCapture();
}
public static void GetMousePositionAndScreenCapture()
{
POINT lpPoint ;
GetCursorPos(out lpPoint);
System.Drawing.Size sz = new System.Drawing.Size();
sz.Height = 100;
sz.Width = 100;
var bounds = new Rectangle(lpPoint, sz);
var image = new Bitmap(bounds.Width, bounds.Height);
using (var graphics = Graphics.FromImage(image))
{
graphics.CopyFromScreen(new System.Drawing.Point(bounds.Left, bounds.Top), System.Drawing.Point.Empty, bounds.Size);
}
image.Save(@"C:\temp\newss.jpeg", ImageFormat.Jpeg);
}
}
}
【问题讨论】:
标签: c# console-application screen-capture