Advertisement
Guest User

C# ImageSearch

a guest
Apr 5th, 2020
900
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.89 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10. using System.Diagnostics;
  11. using System.Threading;
  12. using System.Runtime.InteropServices;
  13. using AutoItX3Lib;
  14.  
  15. namespace Automate
  16. {
  17.     public partial class Form1 : Form
  18.     {
  19.         private static Point[] points = new Point[] {
  20.             new Point(415, 661),
  21.             new Point(1575, 457),
  22.             new Point(415, 661),
  23.             new Point(415, 661),
  24.             new Point(1575, 457),
  25.             new Point(1575, 457),
  26.         };
  27.  
  28.         Autoit Cautoit = new Autoit();
  29.         public int numFailure;
  30.  
  31.         public Form1()
  32.         {
  33.             InitializeComponent();
  34.         }
  35.  
  36.  
  37.         [DllImport("ImageSearchDLL.dll")]
  38.         public static extern IntPtr ImageSearch(int x, int y, int right, int bottom, [MarshalAs(UnmanagedType.LPStr)]string imagePath);
  39.  
  40.         public static string[] UseImageSearch(string imgPath, string tolerance)
  41.         {
  42.             imgPath = "*" + tolerance + " " + imgPath;
  43.  
  44.             IntPtr result = ImageSearch(0, 0, 1920, 1080, imgPath);
  45.             string res = Marshal.PtrToStringAnsi(result);
  46.  
  47.             if (res[0] == '0') return null;
  48.  
  49.             string[] data = res.Split('|');
  50.  
  51.             int x; int y;
  52.             int.TryParse(data[1], out x);
  53.             int.TryParse(data[2], out y);
  54.  
  55.             return data;
  56.         }
  57.  
  58.         private void button1_Click(object sender, EventArgs e)
  59.         {
  60.             Stopwatch timer = new Stopwatch();
  61.             timer.Start();
  62.             var numberOfFailures = 0;
  63.             while (timer.Elapsed.TotalSeconds < 10000)
  64.             {
  65.  
  66.                     foreach (Point point in points)
  67.                     {
  68.                         if (!Cautoit.myResult(point, CalculateCounter(numberOfFailures)))
  69.                             numberOfFailures++;
  70.                         else
  71.                             numberOfFailures = 0;
  72.                     }
  73.                
  74.             }
  75.             timer.Stop();
  76.         }
  77.  
  78.         private int CalculateCounter(int numberOfFailures) => (int)Math.Pow(2, numberOfFailures);
  79.  
  80.     }
  81.  
  82.     public struct Point
  83.     {
  84.         public int x;
  85.         public int y;
  86.         public Point(int x_, int y_)
  87.         {
  88.             x = x_;
  89.             y = y_;
  90.         }
  91.     }
  92. }
  93. namespace Automate
  94. {
  95.     public class Autoit
  96.     {
  97.         [DllImport("ImageSearchDLL32.dll")]
  98.         public static extern IntPtr ImageSearch(int x, int y, int right, int bottom, [MarshalAs(UnmanagedType.LPStr)]string imagePath);
  99.         public static string[] UseImageSearch(string imgPath, string tolerance)
  100.         {
  101.             imgPath = "*" + tolerance + " " + imgPath;
  102.  
  103.             IntPtr result = ImageSearch(0, 0, 1920, 1080, imgPath);
  104.             string res = Marshal.PtrToStringAnsi(result);
  105.  
  106.             if (res[0] == '0') return null;
  107.  
  108.             string[] data = res.Split('|');
  109.  
  110.             int x; int y;
  111.             int.TryParse(data[1], out x);
  112.             int.TryParse(data[2], out y);
  113.  
  114.             return data;
  115.         }
  116.  
  117.         AutoItX3 auto = new AutoItX3();
  118.  
  119.         public void mClick(string ClickSide, int x, int y, int manyClick, int Speed)
  120.         {
  121.             auto.MouseClick(ClickSide, x, y, manyClick, Speed);
  122.         }
  123.  
  124.         public void mSleep(int millisecondi)
  125.         {
  126.             auto.Sleep(millisecondi);
  127.         }
  128.  
  129.  
  130.         public bool myResult(Point point, int Counter)
  131.         {
  132.             Autoit Cautoit = new Autoit();
  133.             string image = (@"C:\winningNumber.bmp");
  134.             string[] result = UseImageSearch(image, "30");
  135.             Cautoit.mClick("LEFT", point.x, point.y, Counter, 1);
  136.             Cautoit.mSleep(15000);
  137.  
  138.             return result != null;
  139.         }
  140.     }
  141. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement