1

So im wondering, is there a batch plugin like "NirCMD" that alows me to search for pixels with a certain color on screen? Like a If-statement.

I know it works with autoit3 but I'd like to do it with batch.

Thanks.

Kevin Mueller
  • 628
  • 3
  • 10
  • 23
  • what do you mean? searching for pixels with a certain color? – npocmaka Oct 04 '17 at 15:23
  • Yeah like a If statement:If [Location of Pixel] = [Color of Pixel] goto whatever – Kevin Mueller Oct 04 '17 at 15:33
  • 2
    Which screen, your monitor? or within a particular window of some running program? Also your question as it currently stands is not within the scope of this forum, please edit it accordingly. – Compo Oct 04 '17 at 16:13
  • It actually does not matter that much, but it would be better in a particular window of a running program. – Kevin Mueller Oct 04 '17 at 16:43

1 Answers1

1

Stolen from here.Save the folowing as bat (eg. getPixelColor.bat):

 // 2>nul||@goto :batch
/*
:batch
@echo off
setlocal

:: find csc.exe
set "csc="
for /r "%SystemRoot%\Microsoft.NET\Framework\" %%# in ("*csc.exe") do  set "csc=%%#"

if not exist "%csc%" (
   echo no .net framework installed
   exit /b 10
)

if not exist "%~n0.exe" (
   call %csc% /nologo /warn:0 /out:"%~n0.exe" "%~dpsfnx0" || (
      exit /b %errorlevel% 
   )
)
%~n0.exe %*
endlocal & exit /b %errorlevel%

*/

  using System;
  using System.Drawing;
  using System.Runtime.InteropServices;

  public class Win32
  {
      [DllImport("user32.dll")]
      static extern IntPtr GetDC(IntPtr hwnd);

      [DllImport("user32.dll")]
      static extern Int32 ReleaseDC(IntPtr hwnd, IntPtr hdc);

      [DllImport("gdi32.dll")]
      static extern uint GetPixel(IntPtr hdc, int nXPos, int nYPos);

      static public System.Drawing.Color GetPixelColor(int x, int y)
      {
       IntPtr hdc = GetDC(IntPtr.Zero);
       uint pixel = GetPixel(hdc, x, y);
       ReleaseDC(IntPtr.Zero, hdc);
       Color color = Color.FromArgb((int)(pixel & 0x000000FF),
                    (int)(pixel & 0x0000FF00) >> 8,
                    (int)(pixel & 0x00FF0000) >> 16);
       return color;
      }

       public static void Main(string[] args)
       {
          int X= Int32.Parse(args[0]);
          int Y=Int32.Parse(args[1]);
          System.Console.WriteLine(GetPixelColor(X,Y));
       }
   }

to get the color at the desired pixel:

for /f "skip=1 tokens=* delims=" %%C in ('getPixelColor.bat  100 150') do (
    set "pixelColor=%%C"
)
echo %pixelColor%
if "%pixelColor%" EQU "Color [A=255, R=248, G=248, B=248]" echo same as mine

Mind that there are no checks in the C# code (non numeric input,pixel put of screen bound and etc.) so you'll have to validate the input by yourself.And this handles only the primary monitor - if you need the secondary ones you can do additional search in stackover flow and alter the code.

To iterate all pixels you'll to get the resolution of the monitor.For windows8 and newer you can use:

wmic path Win32_VideoController get VideoModeDescription /format:value

For older windows versions:

wmic desktopmonitor get screenheight, screenwidth /format:value

Then you need to nested for loops to iterate all pixels on the screen.

npocmaka
  • 55,367
  • 18
  • 148
  • 187