http://support.microsoft.com/kb/892462

To Read This,

Microsoft Knowledge Base Article

This article contents is Microsoft Copyrighted material.
©2005-©2007 Microsoft Corporation. All rights reserved.Terms of Use | Trademarks




Article ID: 892462 - Last Review: May 31, 2007 - Revision: 1.5

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

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.

+ Recent posts