About Me

Sunday, June 15, 2014

On screen color picker

image

Picking color from a web page.

Color them is playing a key role inside any application, it helps user to recognize rather remember certain information like Errors, Notifications and Information. A recent project that I had to work on (A Dashboard), had a number of different colors specified from the design. And also each and every client meeting ended up with list of new color suggestion for example “Can you pick a color similar to this one/that one/darker version of this …..”. Picking a color from a image is easy if you have tools like Photoshop, but In my Dev machine I don’t have one, Even if I had it takes bit of extra memory JUST TO PICK A COLOR CODE.

Some time ago I visited a page from MSDN Forum which is about developing a screen pixel color reader similar to this one How do I test the color of a certain pixel or point.

With the help of it I have developed following Color Picker Application which will capture a screen pixel and copies the color code to Clipboard.

How to use

1. Run the ColorPicker.exe from …\ColorPicker\bin\Release\

2. Once it opened place the mouse cursor inside the ColorPicker Window and press and hold the mouse button.

3. Release the mouse button on anywhere on the screen to reveal color.

Code Explanation

        [DllImport("user32.dll")]
static extern IntPtr GetDC(IntPtr hwnd);

[
DllImport("user32.dll")]
static extern Int32 ReleaseDC(IntPtr hwnd, IntPtr hdc);

[
DllImport("gdi32.dll")]
static extern uint GetPixel(IntPtr hdc, int nXPos, int nYPos);

public Form1()
{
InitializeComponent();
}

private void lblColor_MouseDown(object sender, MouseEventArgs e)
{
var x = e.X;
var y = e.Y;
}

/// <summary>
///
Get the pixel color once mouse button released
/// </summary>
/// <param name="x">
x position of the cursor</param>
/// <param name="y">
y position of the cursor</param>
/// <returns>
Color</returns>
static public System.Drawing.Color GetPixelColor(int x, int y)
{
IntPtr hdc = GetDC(IntPtr.Zero);
uint pixel = GetPixel(hdc, x, y);
ReleaseDC(
IntPtr.Zero, hdc);
Color color = Color.FromArgb((int)(pixel & 0x000000FF),
(
int)(pixel & 0x0000FF00) >> 8,
(
int)(pixel & 0x00FF0000) >> 16);
return color;
}

/// <summary>
///
Load the selected pixel's color in to background label,
/// and code in to text box at the bottom
/// </summary>
private void lblColor_MouseUp(object sender, MouseEventArgs e)
{
this.Cursor = new Cursor(Cursor.Current.Handle);

lblColor.BackColor = GetPixelColor(
Cursor.Position.X, Cursor.Position.Y);
txtColor.Text = HexConverter(lblColor.BackColor);
Clipboard.SetText(txtColor.Text);
}

/// <summary>
///
Converts color in to Hex Format
/// </summary>
/// <param name="c"></param>
/// <returns></returns>
private static String HexConverter(System.Drawing.Color c)
{
return "#" + c.R.ToString("X2") + c.G.ToString("X2") + c.B.ToString("X2");
}

Download Code Here

1 comment:

  1. Techtalkz: On Screen Color Picker >>>>> Download Now

    >>>>> Download Full

    Techtalkz: On Screen Color Picker >>>>> Download LINK

    >>>>> Download Now

    Techtalkz: On Screen Color Picker >>>>> Download Full

    >>>>> Download LINK

    ReplyDelete