问题描述
因此,我最近制作了一个程序,该程序可以在屏幕上找到特定图像并返回其位置,实际上我在stackoverflow上找到了这部分代码,它使用的是AutoIT3中的ImageSearch.dll.
So I have recently made a program which finds specific image on the screen and returns the location of it, I actually found this part of the code on stackoverflow, which is using ImageSearch.dll from AutoIT3.
它工作得很好,但是,缺少一件事,我不知道该怎么做.我的意思是宽容.这是其功能的原始定义:
It works pretty well, however, there's one thing missing and I have no idea how to do it. I mean tolerance. This is the original definition of what it does:
"; $tolerance - 0 for no tolerance (0-255). Needed when colors of
; image differ from desktop. e.g GIF"
即使有一些差异,基本上也可以找到图像.
Basically allows to find the image even if there are a few differences.
这是我得到的代码:
DllImport("ImageSearch.dll")]
public static extern IntPtr ImageSearch(int x, int y, int right, int bottom, [MarshalAs(UnmanagedType.LPStr)]string imagePath);
public static string[] UseImageSearch(string imgPath)
{
IntPtr result = ImageSearch(0, 0, Screen.PrimaryScreen.WorkingArea.Right, Screen.PrimaryScreen.WorkingArea.Bottom, imgPath);
string res = Marshal.PtrToStringAnsi(result);
if (res[0] == '0') return null;
string[] data = res.Split('|');
int x; int y;
int.TryParse(data[1], out x);
int.TryParse(data[2], out y);
return data;
}
我想以某种方式使宽容度保持原样.那可能吗?感谢您的帮助!
And I'd like to somehow make the tolerance work as it is in the original. Is that possible? Thanks for any help!
推荐答案
我实际上是想做同样的事情,所以我想我会发布如何做的.我在UseImageSearch()中添加了第二个参数string tolerance
,并在tolerance
字符串后附加了前缀*,并在imgPath字符串后加空格,然后将新字符串传递给DLL,并返回了字符串[ ]并获得理想的结果.
I was actually wanting to do the same thing so I figured I would post how I did it. I added a second parameter, string tolerance
, to UseImageSearch() and appended the tolerance
string, prefixed with an * and postfixed with a space to the imgPath string, I passed the new string to the DLL and it returned a string[] with the desired results.
using System;
using System.Runtime.InteropServices;
using System.Windows;
我提到了这些家伙
[DllImport(@"C:\ImageSearchDLL.dll")]
public static extern IntPtr ImageSearch(int x, int y, int right, int bottom, [MarshalAs(UnmanagedType.LPStr)]string imagePath);
public static string[] UseImageSearch(string imgPath, string tolerance)
{
imgPath = "*" + tolerance + " " + imgPath;
IntPtr result = ImageSearch(0, 0, 1920, 1080, imgPath);
string res = Marshal.PtrToStringAnsi(result);
if (res[0] == '0') return null;
string[] data = res.Split('|');
int x; int y;
int.TryParse(data[1], out x);
int.TryParse(data[2], out y);
return data;
}
然后我使用图像路径和所需的公差级别0-255调用UseImageSearch
Then I called UseImageSearch using the image path and the desired tolerance level 0-255
string image = (@"C:\Capture.PNG");
string[] results = UseImageSearch(image, "30");
if (results == null)
{
MessageBox.Show("null value bro, sad day");
}
else
{
MessageBox.Show(results[1] + ", " + results[2]);
}
它按预期执行.
这篇关于C#ImageSearch DLL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!