. NET implementation of any point in the screen color pickup

Source: Internet
Author: User

found that many software colors are very pleasing to the eye, but do not know the specific color value is how much, a good reference. So I want to do a screen to pick the color of things. But this thing is purely for technical purposes, with QQ can get to this color value, even the brush can, but more complex. Put the effect up first. As shown in Figure 1.

Figure 1 Color Picker (very earthy)

To do this is to solve two problems as follows:

1. Global Mouse hook: Do this thing the only one technical difficulties, although the Internet has a variety of C + + DLL implementation, but always feel for a hook, in C # project to build a C + + project feel very uncomfortable. I finally found a way to create a new project. (described below.) )

2. Gets the color value of the point where the mouse is located. For experienced people, this is not a problem. People without experience, Google, is not really a problem.

Microsoft's help document swears that "Global hooks are not supported in the. NET framework". I don't know how many people get around. NET to implement the global hook. But some people do not believe this evil, made a C # implementation of the DLL does not need the global hook (see this article). Although limited to mouse, keyboard hooks, but also enough to use.

The first problem is solved. Let's look at the second question. The screen takes color. Google a little bit to know, with the Graphics object CopyFromScreen method is OK. But graphics is something in the form and there are no graphics objects in WPF. Of course, there can be another API call, but the total feeling of direct call API is not good. So think about it, or add a reference to System.Drawing in this WPF project. and wrap it up into a class. The code is as follows.


1using System.Windows;
2using System.Windows.Media;
3using Bitmap = System.Drawing.Bitmap;
4using Graphics = System.Drawing.Graphics;
5using Size = System.Drawing.Size;
7namespace colorpicker
9 internal class Pointcolorpicker
private static Bitmap Cach E = new Bitmap (1, 1);
private static Graphics Tempgraphics = Graphics.fromimage (cache);
/**////<summary>///Gets the Color from the "screen" at the
given point.
///<param name= "point" ></param>//////
<returns></retu rns> public
static Color getcolorfrompoint-
Tempgraphics.copyfromscree n (int) point. X, (int) point. Y, 0, 0, New Size (1, 1));
Return to
cache. GetPixel (0, 0). Upgrade ();

Complete code Download

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.