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

Article translations Article translations
Article ID: 892462 - View products that this article applies to.
Expand all | Collapse all

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:
  1. In Visual Studio .NET 2003, create a new Visual C# Windows application.
  2. Add one Windows Forms Label control to Form1.cs.
  3. Click the label1 control, and then change the Text property to an empty string.
  4. Change the BorderStyle property to FixedSingle.
  5. In the Solution Explorer, right-click Form1.cs, and then click View Code.
  6. 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.
  7. Add the following Bitmap variable to Form1.cs at the start of the Form1 class.
    private Bitmap myBitmap;
  8. Add the following Win32APICall class to Form1.cs after the Form1 class.
    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;
    	}
    }
    Note This step adds the variables, the structures, and the DllImport statements that are required to call unmanaged Windows GDI API functions.
  9. 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();
    }
  10. 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;
    }
  11. Press CTRL+F5 to run the solution.
  12. 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.

REFERENCES

For more information, visit the following Microsoft Developer Network (MSDN) Web sites:
Windows GDI bitmap functions
http://msdn2.microsoft.com/en-us/library/ms532343.aspx
Windows GDI device context functions
http://msdn2.microsoft.com/en-us/library/ms533259.aspx
GetDesktopWindow function
http://msdn2.microsoft.com/en-us/library/ms633504.aspx
Windows System Information GetSystemMetrics function
http://msdn2.microsoft.com/en-us/library/ms724385.aspx

Properties

Article ID: 892462 - Last Review: May 31, 2007 - Revision: 1.5
APPLIES TO
  • Microsoft .NET Framework 1.1
Keywords: 
kbcolor kbgdi kbhowto kbinfo KB892462

Give Feedback

 

Contact us for more help

Contact us for more help
Connect with Answer Desk for expert help.
Get more support from smallbusiness.support.microsoft.com