Click here to Skip to main content
15,667,885 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
What I want to do is display an image in a window, and when the user clicks somewhere on that image, based on the color of the pixel at that position, something happens.

What i tried to do is something like...throw a panel on a windows form, use the on paint method to draw a bitmap on that panel, from an image on my disk, and then use the mouse click event for the panel along with the getPixel method for the bitmap. The problem is the coordinates of the mouse pointer don't match the coordinates for the pixels in the image..something like I can click somewhere inside the panel, on the image...and I even if i click inside a blue area, the color returned is that for an area somewhere above or to the left of the blue area...

What can I do?


I figured out that the problem comes from the coordinate system. This is of course because the bitmap has a fixed 400x400, and the cursor coordinates are global. So when clicking somewhere in the form, where the pixel for 20x200 should be, the mouse coordinates are something like 300x700, hence the problem.

Any ideas on how to overcome this obstacle?
Updated 23-Mar-10 1:36am

The Control::PointToClient [^] method looks promising...
Share this answer
Sounds like you might have an issue with scaling.
If you are drawing the image using a different size that the source Bitmap, the you need to scale the coordinates accordingly.

Something like this might work for you:

using System;
using System.Drawing;
using System.Windows.Forms;

namespace GetPixelExample
    public class Form1 : Form
        private class ImageControl : Panel
            private Bitmap bitmap = new Bitmap("C:\\alien.jpg");

            public ImageControl()
                MouseMove += new MouseEventHandler(HandleMouseMove);

            private void HandleMouseMove(object sender, MouseEventArgs e)
                // Scale the window coordinates to the bitmap coordinates
                double windowX = e.X;
                double windowY = e.Y;

                double controlWidth = Width;
                double controlHeight = Height;
                double imageWidth = bitmap.Width;
                double imageHeight = bitmap.Height;

                Color pixel = bitmap.GetPixel(
                   (int)(windowX * bitmap.Width / controlWidth), 
                   (int)(windowY * bitmap.Height / controlHeight));

            protected override void OnResize(EventArgs eventargs)

            protected override void  OnPaintBackground(PaintEventArgs pevent)
                 pevent.Graphics.DrawImage(bitmap, Bounds);

        public Form1()
            ImageControl control = new ImageControl();
            control.Dock = DockStyle.Fill;

        static void Main()
            Application.Run(new Form1());
Share this answer
cuneyd761 4-Aug-10 11:35am    
Reason for my vote of 2
not bad

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900