Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Get the Color based on  a cursor click

Posted on 2009-04-01
5
Medium Priority
?
490 Views
Last Modified: 2012-05-06
Is there a way to get the color that the mouse was pointing at when clicked.
I have a picturebox with a background image in it. I wish to know the color of the spot at which the cursor was clicked with the image.

Thanks,
0
Comment
Question by:SRabidou
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
5 Comments
 
LVL 64

Accepted Solution

by:
Fernando Soto earned 2000 total points
ID: 24044188
Hi SRabidou;

When you create the Image for the pictureBox create it as a Bitmap. This will allow you to use the Bitmap.GetPixel method to get the color under the cursor when clicked. See code snippet below.

Fernando
private void Form1_Load(object sender, EventArgs e)
{
    // Load the Image into a Bitmap object
    Bitmap bmap = new Bitmap(Image.FromFile(@"C:\Temp\imageDownloaded.jpg"));
    // Assign the Bitmap to the PictureBox.Image property
    pictureBox1.Image = bmap;
}
 
private void pictureBox1_MouseClick(object sender, MouseEventArgs e)
{
    // Cast PictureBox.Image property to Bitmap and get the color 
    // of the pixel from the GetPixel method of the Bitmap
    Color color = ((Bitmap)pictureBox1.Image).GetPixel(e.X, e.Y);
}

Open in new window

0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 24045490
That may not work properly if the image is being STRETCHED as the coords in the form will not match the coords in the bitmap...

You can use the GetPixel() API, though, to get the color directly off the SCREEN:
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
 
        [StructLayout(LayoutKind.Sequential)]
        public struct COLORRGBA
        {
            public byte r;
            public byte g;
            public byte b;
            public byte a;
        }
 
        [DllImport("user32.dll", EntryPoint = "GetDesktopWindow")]
        public static extern IntPtr GetDesktopWindow();
        [DllImport("user32.dll")]
        static extern IntPtr GetWindowDC(IntPtr ptr);
        [DllImport("gdi32.dll")]
        static extern COLORRGBA GetPixel(IntPtr hdc, int x, int y);
        [DllImport("user32.dll")]
        static extern int ReleaseDC(IntPtr hwnd, IntPtr hdc);
 
        private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
        {
            IntPtr deskWnd = GetDesktopWindow();
            IntPtr deskHDC = GetWindowDC(deskWnd);
            COLORRGBA c = GetPixel(deskHDC, Cursor.Position.X, Cursor.Position.Y);
            ReleaseDC(deskWnd, deskHDC);
            label1.Text = Color.FromArgb(c.a,c.r,c.g,c.b).ToString();
        }
 
    }

Open in new window

0
 
LVL 64

Expert Comment

by:Fernando Soto
ID: 24046601
Idle_Mind is correct if the picturebox SizeMode is not set to Normal it will give wrong values or throw an exception with the X or Y values fall outsize the image size.
0
 

Author Closing Comment

by:SRabidou
ID: 31565536
Great solution. I'm not expecting to have the "streching issue" that Idle Mind referred to but that soultion was well done as well but a little difficult to understand.
0
 
LVL 64

Expert Comment

by:Fernando Soto
ID: 24071608
Glad I was able to help.  ;=)
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…

715 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question