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
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