C#根据桌面上文字出现的位置在屏幕上移动光标。

问题描述 投票:0回答:1

因此,我已经搜索,似乎无法找到合适的资源或工具,我想做什么,所以我想我会寻求一些帮助。以下是我想做的事情。

我正在做一个.Net核心的应用程序,它将模拟一些键击,这一切都很好,但我想也根据某些文字出现在屏幕上的位置移动鼠标,然而,我将无法控制文字出现在哪里的应用程序,所以我需要以某种方式,要么是流式桌面视图,并使用一些东西来分析它,或者不断地每10秒左右拍摄一个屏幕截图,并分析该屏幕截图是否有文字,然后将鼠标移动到文字的那个位置,并模拟左键。

我对截图的方法不是很有信心,因为我不知道如何得到鼠标坐标,所以我感觉我需要把桌面流输入到某个东西中,以便得到我分析的图像,然后在上面叠加自己的透明叠加,以便把光标移动到合适的位置。

我希望这样做是有意义的,并感谢任何库或任何推荐的东西来做这件事。我知道如何在屏幕上移动光标和左键,只是在google上没有找到合适的库来分析桌面屏幕的实时情况,并根据搜索条件得到坐标。

谢谢大家了。

c# .net .net-core core
1个回答
1
投票

根据应用程序的不同,你可能想尝试一些方法。

例如,使用 应用洞察力UI自动化如果你有exe文件,那么你也许可以直接捕获文本。

如果你有exe文件,那么你也许可以用类似以下的方法来反编译它 点点滴滴 然后使用Visual Studio的实时调试功能来访问文本。

你也许可以通过一个工具检查正在运行的进程的堆,如 HeapMemView.

光学字符识别可能是一个值得考虑的选择。在这种情况下 宇宙魔方 是一个值得考虑的开源OCR引擎。可以配合截图来提取文字。


1
投票

于是我想出了我需要做的事情。试图匹配文本和OCR被证明是不可能的,因为文本被分层到一个图像中,他们积极地试图防止检测,这是因为它的视频游戏,他们不希望自动化,我猜。所以我做的是确定我会一直知道按钮的样子,所以我拍了一张按钮的截图,现在我每隔10秒就拍一张游戏的截图,然后搜索该图像为我的按钮模板图像。如果图片有匹配的,那么我就生成坐标,然后把鼠标送到那里,然后点击它。下面是我使用的代码,你需要AForge的库来使用这段代码。

Bitmap sourceImage = (Bitmap)Bitmap.FromFile(@"C:\testjpg.jpg");
Bitmap template = (Bitmap)Bitmap.FromFile(@"C:\buttonjpg.jpg");
// create template matching algorithm's instance

//Resize value
var resizePercent = 0.4;

//Resizing images to increase process time. Please note this will result in skewed coordinates. 
//You must divide the coordinates by your resizePercent to get native coordinates taken from screen shots.
sourceImage = new ResizeBicubic((int)(sourceImage.Width * resizePercent), (int)(sourceImage.Height * resizePercent)).Apply(sourceImage);
template = new ResizeBicubic((int)(template.Width * resizePercent), (int)(template.Height * resizePercent)).Apply(template);

// (set similarity threshold to 0.951f = 95.1%)
ExhaustiveTemplateMatching tm = new ExhaustiveTemplateMatching(0.951f);
// find all matchings with specified above similarity

TemplateMatch[] matchings = tm.ProcessImage(sourceImage, template);
// highlight found matchings

BitmapData data = sourceImage.LockBits(
     new Rectangle(0, 0, sourceImage.Width, sourceImage.Height),
     ImageLockMode.ReadWrite, sourceImage.PixelFormat);
foreach (TemplateMatch m in matchings)
{

    Drawing.Rectangle(data, m.Rectangle, Color.White);

    Console.WriteLine(m.Rectangle.Location.ToString());

    var x = m.Rectangle.Location.X;
    var y = m.Rectangle.Location.Y;

    //Fixing the coordinates to reflex the origins of the original screenshot, not the resized one.
    var xResized = (int)(x / resizePercent);
    var yResized = (int)(y / resizePercent);

    // do something else with matching
}
sourceImage.UnlockBits(data);

我注意到我的坐标通常偏离死角约4.5%-8%(按钮仍然会被点击,但我希望有一个完美的中心点击),所以对我来说,我正在计算一个坐标范围,在这个范围内不是很多,以确保我想点击的按钮被点击,而不是错过,只是让程序点击所有潜在的坐标。

所以我希望这能帮助到别人!

亲切的问候。

亚伦

© www.soinside.com 2019 - 2024. All rights reserved.