Click here to Skip to main content
15,913,316 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How can I Select Pixels(any particular area on Mouse Movement) From a Bitmap Image.
The Bitmap Image in Question Is Loaded on a Picture Box at run time, and Operation on this Bitmap is done through Graphics object.

Do You have this answer?????
Posted

Something like this might work for you:

C#
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 of pixel under cursor grabbed here
                Color pixel = bitmap.GetPixel(
                   (int)(windowX * bitmap.Width / controlWidth),
                   (int)(windowY * bitmap.Height / controlHeight));
                System.Diagnostics.Debug.WriteLine(pixel);
            }

            protected override void OnResize(EventArgs eventargs)
            {
                base.OnResize(eventargs);
                Invalidate();
            }

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

        public Form1()
        {
            ImageControl control = new ImageControl();
            control.Dock = DockStyle.Fill;
            Controls.Add(control);
            this.PerformLayout();
        }

        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());
        }
    }
}
 
Share this answer
 
I agree with Fredrik, however I will use different wording:

a PictureBox does not help at all here; you can as well paint the image on a simple Panel.

The main problem is your image may or may not be zoomed, stretched, rescaled, whatever you call it. So it all boils down to finding the bitmap coordinates that correspond to your mouse position. For non-rotated images, the relation is linear and one-dimensional, i.e. both x and y satisfy a transformation of the form z(shown) = coefficient * z(intended) + offset

Stuffing the extremes (top left and bottom right) into this, will give you the correct coefficients and offsets.

If you want to avoid all scaling effects, don't use a background image, simply add a Graphics.DrawImage() to the Panel's paint handler which paints the image without scaling, then the mouse position within the panel corresponds directly to the x,y indices into the bitmap (as long as you don't leave the drawn image).

:)
 
Share this answer
 

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