C#使用AutoIt中的ImageSearch

C#使用AutoIt中的ImageSearch,c#,autoit-c#-wrapper,C#,Autoit C# Wrapper,我正在尝试将AutoIt的这一功能与C#(ImageSearch)结合使用 DLL用于检测屏幕上的给定图像 这是我的代码: [DllImport("ImageSearchDLL.dll")] private static extern IntPtr ImageSearch(int x, int y, int right, int bottom, [MarshalAs(UnmanagedType.LPStr)]string imagePath); public static String[] U

我正在尝试将AutoIt的这一功能与C#(ImageSearch)结合使用

DLL用于检测屏幕上的给定图像

这是我的代码:

[DllImport("ImageSearchDLL.dll")]
private static extern IntPtr ImageSearch(int x, int y, int right, int bottom, [MarshalAs(UnmanagedType.LPStr)]string imagePath);

public static String[] UseImageSearch(string imgPath)
{
    int right = Screen.PrimaryScreen.WorkingArea.Right;
    int bottom = Screen.PrimaryScreen.WorkingArea.Bottom;

    IntPtr result = ImageSearch(0, 0, right, bottom, imgPath);
    String res = Marshal.PtrToStringAnsi(result);

    if (res[0] == '0') return null;//not found

    String[] data = res.Split('|');
    //0->found, 1->x, 2->y, 3->image width, 4->image height;        

    // Then, you can parse it to get x and y:
    int x; int y;
    int.TryParse(data[1], out x);
    int.TryParse(data[2], out y);

    return data;
}
因此,在此行调用ImageSearch函数时,我得到了System.BadImageFormatException

IntPtr result = ImageSearch(0, 0, right, bottom, imgPath);

有什么想法吗?非常感谢

您得到的是
System.BadImageFormatException
,因为您为第一个参数传递了一个零,它应该是图像的文件名

用法示例:


ImageSearch('checkImage.bmp',0100500,0)
我有一个
系统。BadImageFormatException
,因为我使用的是32bit.dll,而我的项目被设置为x64

当我将.dll更改为64位时,一切正常。 下面是我如何使用它(异步模式):

CustomApi.cs

namespace MyApp
{
    public class ImageSearchResult
    {
        public int X { get; set; }
        public int Y { get; set; }

        public ImageSearchResult(int x, int y)
        {
            X = x;
            Y = y;
        }
    }
}
使用系统;
使用System.Runtime.InteropServices;
使用System.Windows.Forms;
名称空间MyApp
{
公共静态类CustomApi
{
//.dll必须与应用程序位于同一文件夹中
//您也可以使用[DllImport(“C:\\full\\path\\to\\dll.dll”)]
[DllImport(“\u ImageSearch\u x64.dll”)]
私有静态外部IntPtr ImageSearch(intx、inty、intright、intbottom[marshallas(UnmanagedType.LPStr)]字符串imagespath);
公共静态图像SearchResult SearchImage(字符串imgFullPath,int-tolerance=0)
{
//不要删除空格
imgFullPath=$“*{tolerance}{imgFullPath}”;
var结果=图像搜索(1,1,Screen.PrimaryScreen.WorkingArea.Width,Screen.PrimaryScreen.WorkingArea.Height,imgFullPath);
var res=Marshal.PtrToStringAnsi(结果);
if(string.IsNullOrEmpty(res)| | res[0]=“0”)
返回null;
var data=res.Split(“|”);
int.TryParse(数据[1],输出变量x);
int.TryParse(数据[2],输出变量y);
返回新的ImageSearchResult(x,y);
}
}
}
ImageSearchResult.cs

namespace MyApp
{
    public class ImageSearchResult
    {
        public int X { get; set; }
        public int Y { get; set; }

        public ImageSearchResult(int x, int y)
        {
            X = x;
            Y = y;
        }
    }
}
MainForm.cs

namespace MyApp
{
    public class ImageSearchResult
    {
        public int X { get; set; }
        public int Y { get; set; }

        public ImageSearchResult(int x, int y)
        {
            X = x;
            Y = y;
        }
    }
}
公共静态异步任务FindImage(字符串imageFullPath,int timeOut=15) { 返回等待任务。运行(()=> { ImageSearchResult=null; for(int i=0;i 已从下载.dll


此外,将它们上载到“以防万一”

AutoIt
ImageSearch
函数具有以下参数:
ImageSearch(“patterntosearch”,其中,x,y,公差)
,其中
patterntosearch
是文件名(bmp或png),
其中
是0或1(返回中点或左上角)和
公差
,介于0(精确匹配)和255(不确定)之间的值,以接受一定量的颜色偏移
x
y
接收找到的位置。