Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the colour of a pixel at X,Y using c#?

Tags:

c#

api

How do I get the color of a pixel at X,Y using c#?

As for the result, I can convert the results to the color format I need. I am sure there is an API call for this.

For any given X,Y on the monitor, I want to get the color of that pixel.

like image 993
MichaelICE Avatar asked Apr 15 '09 18:04

MichaelICE


People also ask

How do you determine the color of a pixel?

With the get() function we can read the color of any pixel in our program window. We can specify which pixel we are interested in by using x and y coordinates as parameters. For example, color mycolor = get(100, 200); would grab the color of pixel 100, 200 and put that color into the mycolor variable.

Which function returns the color of pixel present at location x Y?

getpixel() function in C h contains getpixel() function which returns the color of pixel present at location (x, y).

How is the Colour of each pixel stored?

Pixel-oriented palettes store all of the pixel color data as contiguous bits within each element of the array. As we noted above, in an RGB palette, each element in the palette consists of a triplet of values.


1 Answers

To get a pixel color from the Screen here's code from Pinvoke.net:

  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;       }    } 
like image 165
CLaRGe Avatar answered Sep 30 '22 02:09

CLaRGe