【问题标题】:C# Console Application Screen Capture a rectangle around the mouseC# 控制台应用程序屏幕捕获鼠标周围的矩形
【发布时间】: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


    【解决方案1】:

    就问题一而言,您希望箭头光标位于屏幕截图的中心,这会显得相当直截了当。如果屏幕截图的大小为 100x100,那么当您获得光标的 Point 时,您可能希望将 X 值 -50 和 Y 值 -50 移动。基本上是屏幕截图矩形大小的一半宽度和高度。这会将光标放在屏幕截图矩形的中心。

    您可能要查找的唯一内容是光标的 XY 位置是否小于 50,在这种情况下,我们只需将值设置为 0。将这些更改添加到 lpPoint 应该可以解决这个。像……

    lpPoint.X = lpPoint.X >= 50 ? lpPoint.X - 50 : 0;
    lpPoint.Y = lpPoint.Y >= 50 ? lpPoint.Y - 50 : 0;
    

    对于第二个问题,我只能假设这可能与显示器的分辨率不同有关。我认为它仍然有效,因为它似乎在我的小型测试中有效。然而,这是我的猜测。您能否展示两台显示器的屏幕截图,显示您所描述的内容?

    【讨论】:

    • 对于问题 #2,我知道是什么导致了问题,但我只是不知道如何解决它。这是我的显示设置监视器 1 屏幕分辨率 = 1366 x 768 监视器 1 比例和布局 = 100% 监视器 2 屏幕分辨率 = 1920 X 1080 监视器 2 比例和布局 = 125% 因为监视器 #2 具有不同的比例设置,所以它会脱离屏幕捕获。
    猜你喜欢
    • 2020-12-05
    • 1970-01-01
    • 2013-11-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-26
    相关资源
    最近更新 更多