【问题标题】:C# using ImageSearch from AutoItC# 使用 AutoIt 中的 ImageSearch
【发布时间】:2020-02-08 20:15:19
【问题描述】:

我正在尝试将 AutoIt 中的 DLL 与 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);

有什么想法吗?非常感谢

【问题讨论】:

  • AutoItImageSearch函数有以下参数:ImageSearch("patterntosearch",where,x,y,tolerance),其中patterntosearch是文件名(bmp或png),where是0或1(返回中点或左上角)角)和tolerance,一个介于0(完全匹配)和255(?不确定)之间的值,以接受一些颜色偏移。 xy 接收找到的位置。

标签: c# autoit-c#-wrapper


【解决方案1】:

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

使用示例:

ImageSearch('checkImage.bmp', 0, 100, 500, 0)

【讨论】:

    【解决方案2】:

    我有一个System.BadImageFormatException,因为我使用的是 32 位 .dll 并且我的项目设置为 x64

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

    CustomApi.cs

    using System;
    using System.Runtime.InteropServices;
    using System.Windows.Forms;
    
    namespace MyApp
    {
        public static class CustomApi
        {
            //The .dll must be in the same folder as the app
            //You can also use [DllImport("C:\\full\\path\\to\\dll.dll")]
            [DllImport("_ImageSearch_x64.dll")]
            private static extern IntPtr ImageSearch(int x, int y, int right, int bottom, [MarshalAs(UnmanagedType.LPStr)] string imagePath);
    
    
            public static ImageSearchResult SearchImage(string imgFullPath, int tolerance = 0)
            {
                //Don't delete the space
                imgFullPath = $"*{tolerance} {imgFullPath}";
    
                var result = ImageSearch(1, 1, Screen.PrimaryScreen.WorkingArea.Width, Screen.PrimaryScreen.WorkingArea.Height, imgFullPath);
                var res = Marshal.PtrToStringAnsi(result);
    
                if (string.IsNullOrEmpty(res) || res[0] == '0')
                    return null;
    
                var data = res.Split('|');
                int.TryParse(data[1], out var x);
                int.TryParse(data[2], out var y);
    
                return new 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

    public static async Task<ImageSearchResult> FindImage(string imageFullPath, int timeOut = 15)
            {
                
                return await Task.Run(() =>
                {
                    ImageSearchResult result = null;
    
                    for (int i = 0; i < timeOut; i++)
                    {
                        result = CustomApi.SearchImage(imageFullPath, tolerance: 0);
                        if (result != null)
                            break;
    
                        Thread.Sleep(1000);
                    }
    
                    return result;
                });
    
            }
    
    
            //Put this wherever you want
            var result = await FindImage(@"C:\my_image.bmp"); //Must be .bmp
            if (result == null)
                Console.WriteLine("c non");
            else
                Console.WriteLine($"Found : X:{result.X} Y:{result.Y}");
    

    here下载.dlls

    另外,将它们上传到ZippyShare以防万一

    【讨论】:

      猜你喜欢
      • 2022-08-24
      • 1970-01-01
      • 2011-12-11
      • 2019-12-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多