Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GUI.Screenshot.Locate 能否兼容屏幕缩放 #12

Open
cyoukon opened this issue Jan 27, 2024 · 4 comments
Open

GUI.Screenshot.Locate 能否兼容屏幕缩放 #12

cyoukon opened this issue Jan 27, 2024 · 4 comments

Comments

@cyoukon
Copy link

cyoukon commented Jan 27, 2024

GUI.Screenshot.Locate 能否返回一个除以过屏幕缩放比例的坐标。
现在如果屏幕缩放不是100%,匹配到坐标后直接调用 GUI.Mouse.Click,位置会不对。

var win = GUI.Application.FindWindowByTitle("微信")!;
win.Activate();
Thread.Sleep(500);
var allMatches = GUI.Screenshot.LocateAllWithConfidence(GUI.Screenshot.Screenshot(), "Images\\wechatSearch.png", 0.8);
var rect = allMatches.OrderBy(m => m.Confidence).Last();
GUI.Mouse.Click((int?)((rect.Rectangle.X + 50) / 1.5), (int?)(rect.Rectangle.Y / 1.5));
@yangzhongke
Copy link
Owner

你好,你说的屏幕缩放指的是这个设置吗?
image
如果是这个的话,我这里一直是150%,测试一直没问题呀

@cyoukon
Copy link
Author

cyoukon commented Jan 28, 2024

image
你好,是的。
我电脑上测试,缩放改成100%后定位正常,如果是150%的话就要除以1.5。

@cyoukon
Copy link
Author

cyoukon commented Jan 28, 2024

截图的图片分辨率是系统的真实分辨率,而与系统交互需要的是缩放后的分辨率。
试下下面这个就可以明白
C# - How to get real screen resolution in multiple monitors context?
image

@yangzhongke
Copy link
Owner

yangzhongke commented Nov 22, 2024

最近琢磨出来解决方案了,抽时间把它完善。
下面是实验用的代码,是给我自己看的一个笔记:
using System.Drawing.Imaging;
using System.Runtime.InteropServices;

class Program
{
static void Main()
{
Application.SetHighDpiMode(HighDpiMode.PerMonitorV2);
// 获取所有屏幕信息
List screens = new List(Screen.AllScreens);

    // 计算虚拟屏幕的大小
    Rectangle virtualScreen = new Rectangle(
        SystemInformation.VirtualScreen.Left,
        SystemInformation.VirtualScreen.Top,
        SystemInformation.VirtualScreen.Width,
        SystemInformation.VirtualScreen.Height
    );

    // 创建最终拼接的图片
    using (Bitmap finalImage = new Bitmap(virtualScreen.Width, virtualScreen.Height))
    {
        using (Graphics graphics = Graphics.FromImage(finalImage))
        {
            // 填充背景为黑色
            graphics.FillRectangle(Brushes.Black, 0, 0, finalImage.Width, finalImage.Height);

            // 截取每个屏幕并绘制到虚拟屏幕位置
            foreach (Screen screen in screens)
            {
                Bitmap screenCapture = CaptureScreen(screen);
                int x = screen.Bounds.Left - virtualScreen.Left;
                int y = screen.Bounds.Top - virtualScreen.Top;

                graphics.DrawImage(screenCapture, x, y);
                screenCapture.Dispose();
            }
        }

        // 保存拼接的图片
        string filePath = "d:/MultiScreenCapture.png";
        finalImage.Save(filePath, ImageFormat.Png);
        Console.WriteLine($"All screens captured and saved to {filePath}");
    }
}

static Bitmap CaptureScreen(Screen screen)
{
    Bitmap bitmap = new Bitmap(screen.Bounds.Width, screen.Bounds.Height);
    using (Graphics graphics = Graphics.FromImage(bitmap))
    {
        graphics.CopyFromScreen(screen.Bounds.Left, screen.Bounds.Top, 0, 0, screen.Bounds.Size);
    }
    return bitmap;
}

}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants