C# Pixel Colour Help

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • HackerOverload
    New Member
    • Feb 2008
    • 3

    C# Pixel Colour Help

    Okay, well I am trying to detect when a program is done, by detecting the colour change of the pixels.

    See, when the program is done, it has gets a big blue button at the bottom to exit it. And i want my C# program to detect when it is done, and exit that program.

    I have absolutely no idea on how to do this. I hope you guys have answers =)
  • kenobewan
    Recognized Expert Specialist
    • Dec 2006
    • 4871

    #2
    What have you tried so far? Please provide more info. Thanks.

    Comment

    • HackerOverload
      New Member
      • Feb 2008
      • 3

      #3
      Well, i have tried using X/Y coordinates, but i don't really know how to use those either. Preferably pixel detection though.

      I'll post my source code here for something that i did do, but its not really that close to what i want...

      Code:
      using System;
      using System.Collections.Generic;
      using System.ComponentModel;
      using System.Data;
      using System.Drawing;
      using System.Text;
      using System.Windows.Forms;
      using System.Runtime.InteropServices;
      
      namespace WindowsApplication1
      {
          public partial class Form1 : Form
          {
              public Form1()
              {
                  InitializeComponent();
              }
              private Bitmap myBitmap;
      
              public class Win32APICall
              {
                  [DllImport("gdi32.dll", EntryPoint = "DeleteDC")]
                  public static extern IntPtr DeleteDC(IntPtr hdc);
      
                  [DllImport("gdi32.dll", EntryPoint = "DeleteObject")]
                  public static extern IntPtr DeleteObject(IntPtr hObject);
      
                  [DllImport("gdi32.dll", EntryPoint = "BitBlt")]
                  public static extern bool BitBlt(IntPtr hdcDest, int nXDest,
                      int nYDest, int nWidth, int nHeight, IntPtr hdcSrc,
                      int nXSrc, int nYSrc, int dwRop);
      
                  [DllImport("gdi32.dll", EntryPoint = "CreateCompatibleBitmap")]
                  public static extern IntPtr CreateCompatibleBitmap(IntPtr hdc,
                      int nWidth, int nHeight);
      
                  [DllImport("gdi32.dll", EntryPoint = "CreateCompatibleDC")]
                  public static extern IntPtr CreateCompatibleDC(IntPtr hdc);
      
                  [DllImport("gdi32.dll", EntryPoint = "SelectObject")]
                  public static extern IntPtr SelectObject(IntPtr hdc, IntPtr hgdiobjBmp);
      
                  [DllImport("user32.dll", EntryPoint = "GetDesktopWindow")]
                  public static extern IntPtr GetDesktopWindow();
      
                  [DllImport("user32.dll", EntryPoint = "GetDC")]
                  public static extern IntPtr GetDC(IntPtr hWnd);
      
                  [DllImport("user32.dll", EntryPoint = "GetSystemMetrics")]
                  public static extern int GetSystemMetrics(int nIndex);
      
                  [DllImport("user32.dll", EntryPoint = "ReleaseDC")]
                  public static extern IntPtr ReleaseDC(IntPtr hWnd, IntPtr hDC);
      
                  public static Bitmap GetDesktop()
                  {
                      int screenX;
                      int screenY;
                      IntPtr hBmp;
                      IntPtr hdcScreen = GetDC(GetDesktopWindow());
                      IntPtr hdcCompatible = CreateCompatibleDC(hdcScreen);
      
                      screenX = GetSystemMetrics(0);
                      screenY = GetSystemMetrics(1);
                      hBmp = CreateCompatibleBitmap(hdcScreen, screenX, screenY);
      
                      if (hBmp != IntPtr.Zero)
                      {
                          IntPtr hOldBmp = (IntPtr)SelectObject(hdcCompatible, hBmp);
                          BitBlt(hdcCompatible, 0, 0, screenX, screenY, hdcScreen, 0, 0, 13369376);
      
                          SelectObject(hdcCompatible, hOldBmp);
                          DeleteDC(hdcCompatible);
                          ReleaseDC(GetDesktopWindow(), hdcScreen);
      
                          Bitmap bmp = System.Drawing.Image.FromHbitmap(hBmp);
      
                          DeleteObject(hBmp);
                          GC.Collect();
      
                          return bmp;
                      }
      
                      return null;
                  }
              }
      
              private void Form1_Load(object sender, EventArgs e)
              {
      
              }
      
              private void label1_MouseDown(object sender, MouseEventArgs e)
              {
                  myBitmap = Win32APICall.GetDesktop();
              }
      
              private void label1_MouseUp(object sender, MouseEventArgs e)
              {
                  Color myColor = myBitmap.GetPixel(MousePosition.X, MousePosition.Y);
                  label1.BackColor = myColor;
              }
          }
      }
      And with that, you would just click the label, and hold you mouse, and drag it onto a different color, and the label would grab that color. But...how do i "detect" a color?

      Comment

      • HackerOverload
        New Member
        • Feb 2008
        • 3

        #4
        Why is nobody posting....='(

        Comment

        Working...