C#C
C#3y ago
Cringel

❔ Trying to return

using System;
using System.Drawing;
using System.Windows.Forms;
using AForge.Imaging;
using AForge.Imaging.Filters;
using Image = System.Drawing.Image;

namespace Program_ui_test
{
    public partial class Form1 : Form
    {
        
        private Bitmap savedImage;
        
        public Form1()
        {
            InitializeComponent();
            
            // Load the saved image you want to match
            savedImage = (Bitmap)Image.FromFile("C:\\Users\\kingk\\RiderProjects\\Program ui test\\Program ui test\\Icon.png");
        }
        
        public FindMatchingImage()
        {
            // Capture the screen
            Bitmap screenshot = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height);
            Graphics g = Graphics.FromImage(screenshot);
            g.CopyFromScreen(0, 0, 0, 0, screenshot.Size);
            
            // Apply grayscale filter to both images
            Grayscale grayscaleFilter = new Grayscale(0.2125, 0.7154, 0.0721);
            Bitmap processedSavedImage = grayscaleFilter.Apply(savedImage);
            Bitmap processedScreenshot = grayscaleFilter.Apply(screenshot);

            // Find the difference between the images
            ExhaustiveTemplateMatching tm = new ExhaustiveTemplateMatching(0.8f);
            TemplateMatch[] matches = tm.ProcessImage(processedScreenshot, processedSavedImage);

            if (matches.Length > 0)
            {
                // Get the center coordinates of the matched region
                Rectangle matchRect = matches[0].Rectangle;
                int centerX = matchRect.X + matchRect.Width / 2;
                int centerY = matchRect.Y + matchRect.Height / 2;

                // Display the center coordinates
                // MessageBox.Show($"Match found at ({centerX}, {centerY})");
                Console.WriteLine($"Match found at ({centerX}, {centerY})");
                return (centerX, centerY);
            }
Was this page helpful?