3

I am making a program that detects a sector of the screen to perform an action that I require and I am doing it through screenshot reviewing pixel by pixel to find the sector that I want to analyze and compare with the change that I want but I have a problem, I use a timer to taking screenshot every 20 times per second and the sentences in charge of taking the screenshot end in an exception that I don't understand at all, it works fine at times until the exception "ArgumentsException" appears and the message "Parameters are not valid", so I do not know what may be happening, I am supposed to be sending the correct parameters and I am even setting the sentences to null thinking that something was left out but no, the same thing continues and I do not understand why.

Now, I don't know if there is any other way to directly detect and without taking screenshots the pixels of the sector that I want to find since I see that there is a problem regarding the sentences to capture an image from the screen.

The code I use to take screenshots is:

screenCapture = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height, PixelFormat.Format32bppRgb);
g = Graphics.FromImage(screenCapture);
g.CopyFromScreen(Screen.PrimaryScreen.Bounds.X, Screen.PrimaryScreen.Bounds.Y, 0, 0, screenCapture.Size, CopyPixelOperation.SourceCopy);

Sometimes the problem is in the bitmap, other times it is the graphics that come out with the same problem, so can you recommend me for the purpose that I require?

ProgrammingLlama
  • 36,677
  • 7
  • 67
  • 86
  • 1
    Using psychic debugging here: if you are getting an `ArgumentException` with `"Parameters not valid"`, on code like you are showing there's a good chance you are passing a rectangle that is not completely contained on the screen. Please show the stack associated with the exception, the parameters passed to the failing call, and the exact wording of the exception message. Being able to debug random exceptions is about step 5 (of about 100) on your path to becoming a somewhat capable developer. – Flydog57 Jun 29 '20 at 01:05
  • Have you wrapped your code and put breakpoint on catch section? It should provide informative messages to tell which one of your arguments is invalid. – Louis Go Jun 29 '20 at 01:06
  • Does this answer your question? [How to get screen pixel color the fastest way (C#)](https://stackoverflow.com/questions/27160747/how-to-get-screen-pixel-color-the-fastest-way-c) – TnTinMn Jun 29 '20 at 14:37
  • Update: I tried to replicate the error that I was giving but I did not succeed, it turns out that I turned off my laptop and the next day the software as magic had been solved but in reality I did not understand why the parameters that I sent before were not valid and it turns out that now it does not generate such an error, it may have been a compilation error or something similar, but thanks to all who responded. – RoAlex OsMu Jul 03 '20 at 20:44

1 Answers1

1

Without taking a screenshot you can use the Win32 API GetPixel: https://www.pinvoke.net/default.aspx/gdi32/getpixel.html

  using System;
  using System.Drawing;
  using System.Runtime.InteropServices;
  sealed 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;
      }
   }

Alternatively consider Blit's as they're cheaper than screenshots, C# - Faster Alternatives to SetPixel and GetPixel for Bitmaps for Windows Forms App

Jeremy Thompson
  • 61,933
  • 36
  • 195
  • 321