Microsoft KB Archive/892462

= How to use Visual C# to obtain the color of the pixel that is referenced by the mouse pointer =

Article ID: 892462

Article Last Modified on 5/31/2007

-

APPLIES TO


 * Microsoft .NET Framework 1.1

-





INTRODUCTION
This article describes how to use Microsoft Visual C# to obtain the color of the pixel that is referenced by the mouse pointer.



MORE INFORMATION
To obtain the color of the pixel that is referenced by the mouse pointer by using Visual C#, follow these steps:
 * 1) In Microsoft Visual Studio .NET 2003, create a new Visual C# Windows application.
 * 2) Add a Label control that is named label1 to capture the pixel color.
 * 3) Create a class that contains the Dlllmport statements that are required to call the Microsoft Windows GDI functions, and then capture an image that represents the current desktop.
 * 4) Associate the MouseDown event of the label with an event handler that is named label1_MouseDown.
 * 5) In the label1_MouseDown event handler, capture an image of the current desktop.
 * 6) Associate the MouseUp event of the label with an event handler that is named label1_MouseUp.
 * 7) In the label1_MouseUp event handler, retrieve the color that is associated with the current pixel location of the mouse pointer, and then set the BackColor of label1 to the color of the mouse pointer pixel.

The following steps are detailed steps to obtain the color of the pixel that is referenced by the mouse pointer:  In Visual Studio .NET 2003, create a new Visual C# Windows application. Add one Windows Forms Label control to Form1.cs. Click the label1 control, and then change the Text property to an empty string. Change the BorderStyle property to FixedSingle. In the Solution Explorer, right-click Form1.cs, and then click View Code.  Add the following Using statements to the top of the Form1.cs source code. using System.Runtime.InteropServices; Note This step adds the required references to call the InteropServices functions and methods.   Add the following Bitmap variable to Form1.cs at the start of the Form1 class. private Bitmap myBitmap; </li>  Add the following Win32APICall class to Form1.cs after the Form1 class. public class Win32APICall {   [DllImport(&quot;gdi32.dll&quot;,EntryPoint=&quot;DeleteDC&quot;)] public static extern IntPtr DeleteDC(IntPtr hdc);

[DllImport(&quot;gdi32.dll&quot;,EntryPoint=&quot;DeleteObject&quot;)] public static extern IntPtr DeleteObject(IntPtr hObject);

[DllImport(&quot;gdi32.dll&quot;,EntryPoint=&quot;BitBlt&quot;)] public static extern bool BitBlt(IntPtr hdcDest,int nXDest,       int nYDest,int nWidth,int nHeight,IntPtr hdcSrc,        int nXSrc,int nYSrc,int dwRop);

[DllImport (&quot;gdi32.dll&quot;,EntryPoint=&quot;CreateCompatibleBitmap&quot;)] public static extern IntPtr CreateCompatibleBitmap(IntPtr hdc,        int nWidth, int nHeight);

[DllImport (&quot;gdi32.dll&quot;,EntryPoint=&quot;CreateCompatibleDC&quot;)] public static extern IntPtr CreateCompatibleDC(IntPtr hdc);

[DllImport (&quot;gdi32.dll&quot;,EntryPoint=&quot;SelectObject&quot;)] public static extern IntPtr SelectObject(IntPtr hdc,IntPtr hgdiobjBmp);

[DllImport(&quot;user32.dll&quot;, EntryPoint=&quot;GetDesktopWindow&quot;)] public static extern IntPtr GetDesktopWindow;

[DllImport(&quot;user32.dll&quot;,EntryPoint=&quot;GetDC&quot;)] public static extern IntPtr GetDC(IntPtr hWnd);

[DllImport(&quot;user32.dll&quot;,EntryPoint=&quot;GetSystemMetrics&quot;)] public static extern int GetSystemMetrics(int nIndex);

[DllImport(&quot;user32.dll&quot;,EntryPoint=&quot;ReleaseDC&quot;)] 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; } } Note This step adds the variables, the structures, and the DllImport statements that are required to call unmanaged Windows GDI API functions. </li>  Add the following code to the MouseDown event for the label that you created in step 2 in the Form1.cs class. private void label1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e) { myBitmap = Win32APICall.GetDesktop; } </li>  Add the following code to the MouseUp event for the label that you created in step 2 in the Form1.cs class. private void label1_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e) { Color myColor = myBitmap.GetPixel(MousePosition.X,MousePosition.Y); label1.BackColor = myColor; } </li> Press CTRL+F5 to run the solution.</li> Click and hold the mouse button down in the label, drag the mouse pointer over the desktop, and then release the mouse button to capture the color.</li></ol>

<div class="references_section">