Click here to Skip to main content
15,889,879 members
Please Sign up or sign in to vote.
4.50/5 (2 votes)
See more:
I want to get rgb value of the image on mouse move event in c sharp using aforge.net api.As when move mouse image then correspondingly it displays the rgb value of that point.
I want to do it using unmanaged image and pointer data. I dont have to use getpixel() method.kindly help me.below is my code.
thanks in advance

private void Form1_MouseMove(object sender, MouseEventArgs e)
        {
            // loading image
            Graphics g = CreateGraphics();
            Bitmap bmp = new Bitmap("bmpimage54.bmp");
            Rectangle r = new Rectangle(0, 0, bmp.Width, bmp.Height);
            g.DrawImage(bmp, r, r, GraphicsUnit.Pixel);
         // getting pixel values on mouse move

            Rectangle sourceRect = new Rectangle(0, 0, bmp.Width, bmp.Height);
            BitmapData imageData = bmp.LockBits(sourceRect,                    ImageLockMode.ReadWrite,PixelFormat.Format24bppRgb);
           UnmanagedImage unmanagedImage = new UnmanagedImage( imageData );
   unsafe
            {
                byte* imgPtr = (byte*)imageData.Scan0.ToPointer();
                byte* ptr;
                for (int i = 0; i < imageData.Height; i++)
                {
                    for (int j = 0; j < imageData.Width; j++)
                    {   
                        imgPtr += 3;
                    }
                    imgPtr += imageData.Stride - imageData.Width * 3;
                }
           }      
             bmp.UnlockBits(imageData);
        }
Posted
Updated 22-Oct-18 16:45pm
v3

why u are applying loop?

once u know the co-ordinates, u can use indexing i.e square brackets to get the value of that particular color

i have loaded an image into picturebox, then using mouse move event of picture box

C#
private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
     Bitmap bmp=new Bitmap(pictureBox1.Image);
     BitmapData bd=bmp.LockBits(new Rectangle(0,0,bmp.Width,bmp.Height),ImageLockMode.ReadOnly,PixelFormat.Format24bppRgb);

     unsafe
     {
         byte* ptr = (byte*)bd.Scan0;
         int x = e.X * 3;
         int y = e.Y * bd.Stride;
         label1.Text = e.Location.ToString();
         label2.Text = ptr[x + y].ToString() + "," + ptr[x + y + 1].ToString() + "," + ptr[x + y + 2].ToString();
     }
}


from cordinates, value of x,y is calculated, then x+y is used to reach the pixel location

x+y gives blue color value
x+y+1 gives green color value
x+y+2 gives red color value
 
Share this answer
 
thank u abdur Rehman. It really solved my problem.
 
Share this answer
 
Comments
dontumindit 17-May-11 12:49pm    
thnxxxxxxxxx
[no name] 18-May-11 9:32am    
sorry it was by mistake. Now you can see it's fully rated.
BitmapData bd;

private void detection_pictureBox_MouseMove(object sender, MouseEventArgs e)
{
    if (detection_pictureBox.Image != null)
    {
        unsafe
        {
            byte* ptr = (byte*)bd.Scan0;
            int x = e.X * 3;
            int y = e.Y * bd.Stride;
            CurrPointerPos_lbl.Text = e.Location.ToString();
            CurrPointerCol_lbl.Text = ptr[x + y].ToString() + "," + ptr[x + y + 1].ToString() + "," + ptr[x + y + 2].ToString();
        }

    }
}

private void detection_pictureBox_MouseEnter(object sender, EventArgs e)
{
    if (detection_pictureBox.Image != null)
    {
        bd = (RunImgProcess.detectionImg).LockBits(new Rectangle(0, 0, RunImgProcess.detectionImg.Width, RunImgProcess.detectionImg.Height), ImageLockMode.ReadOnly, RunImgProcess.detectionImg.PixelFormat);
    }
}

private void detection_pictureBox_MouseLeave(object sender, EventArgs e)
{
    if (detection_pictureBox.Image != null)
    {
        (RunImgProcess.detectionImg).UnlockBits(bd);
    }
}
 
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