Advertisement
ibrahim_elsakka

C# Image Scanner Sample

Feb 12th, 2023
817
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using ImageScanner.Properties;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.ComponentModel;
  5. using System.Data;
  6. using System.Diagnostics;
  7. using System.Drawing;
  8. using System.Linq;
  9. using System.Text;
  10. using System.Threading.Tasks;
  11. using System.Windows.Forms;
  12.  
  13. namespace ImageScanner
  14. {
  15.     public partial class Form1 : Form
  16.     {
  17.         public Form1()
  18.         {
  19.             InitializeComponent();
  20.         }
  21.  
  22.         private void btnScan_Click(object sender, EventArgs e)
  23.         {
  24.             Stopwatch sw = new Stopwatch();
  25.             sw.Start();
  26.  
  27.             Bitmap screenCapture = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height);
  28.  
  29.             Graphics g = Graphics.FromImage(screenCapture);
  30.  
  31.             g.CopyFromScreen(Screen.PrimaryScreen.Bounds.X,
  32.                              Screen.PrimaryScreen.Bounds.Y,
  33.                              0, 0,
  34.                              screenCapture.Size,
  35.                              CopyPixelOperation.SourceCopy);
  36.  
  37.             Bitmap myPic = Resources.Capture;
  38.  
  39.             bool isInCapture = IsInCapture(myPic, screenCapture);
  40.  
  41.             sw.Stop();
  42.  
  43.             TimeSpan ts = sw.Elapsed;
  44.         }
  45.  
  46.         private bool IsInCapture(Bitmap searchFor, Bitmap searchIn)
  47.         {
  48.             for (int x = 0; x < searchIn.Width; x++)
  49.             {
  50.                 for (int y = 0; y < searchIn.Height; y++)
  51.                 {
  52.                     bool invalid = false;
  53.                     int k = x, l = y;
  54.                     for (int a = 0; a < searchFor.Width; a++)
  55.                     {
  56.                         l = y;
  57.                         for (int b = 0; b < searchFor.Height; b++)
  58.                         {
  59.                             if (searchFor.GetPixel(a, b) != searchIn.GetPixel(k, l))
  60.                             {
  61.                                 invalid = true;
  62.                                 break;
  63.                             }
  64.                             else
  65.                                 l++;
  66.                         }
  67.                         if (invalid)
  68.                             break;
  69.                         else
  70.                             k++;
  71.                     }
  72.                     if (!invalid)
  73.                         return true;
  74.                 }
  75.             }
  76.             return false;
  77.         }
  78.     }
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement