Mentioned
In
|
 |
 |
 |
 |
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 pointerThis article describes how to use Microsoft Visual C# to
obtain the color of the pixel that is referenced by the mouse
pointer. To obtain the color of the pixel that is referenced by the
mouse pointer by using Visual C#, follow these steps:
- In Microsoft Visual Studio .NET 2003, create a new Visual
C# Windows application.
- Add a Label control that is named label1 to capture the pixel
color.
- 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.
- Associate the MouseDown event of the label with an event handler that is named
label1_MouseDown.
- In the label1_MouseDown event handler, capture an image of the current
desktop.
- Associate the MouseUp event of the label with an event handler that is named
label1_MouseUp.
- 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.
- 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. - 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();
} - 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;
} - Press CTRL+F5 to run the solution.
- 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.
For more information, visit the following Microsoft
Developer Network (MSDN) Web sites:
APPLIES TO- Microsoft .NET Framework 1.1
| kbcolor kbgdi kbhowto kbinfo KB892462 |
Community Feedback System
Very often, it takes hours to solve a problem. Very often, you've looked high
and low, and have tried a lot of solutions. When you finally found it, chances
are, it was because someone else helped you. Here's your chance to give back.
Use our community feedback tool to let others know what worked for you and what
didn't.
Please also understand that the community feedback system is not warranted to be
correct, it's simply a system that we've built to let people try and help each
other. If something in a feedback response doesn't make sense to you, or you're
not comfortable making changes that the feedback talks about (like registry
edits), please consult a professional.
Thank you for using kbAlertz.com Feedback System.
-- Scott Cate
(Optional) Name
(Optional)
Public URL Or Email
Comments
No
HTML -- Text Only Please
|
 |
 |
 |
 |
 |
 |
 |
| |