如何知道鼠标位于pictureBox
的哪个像素上(坐标)?
如何知道鼠标位于pictureBox
的哪个像素上(坐标)?
private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
Text = String.Format("X: {0}; Y: {1}", e.X, e.Y);
}
您在这里有X和Y的位置。
如果您的图像已经被缩放和/或移动,请记住必须对这些坐标应用转换。
明确一点:如果您的图像已经放置在(x0,y0)
并且使用zf
进行了缩放(请记住,zf<1表示减小),像素坐标将会是
px = (e.X - x0) / zoom;
py = (e.Y - y0) / zoom;
// Declare a Bitmap
Bitmap mybitmap;
// Load Picturebox image to bitmap
mybitmap = new Bitmap(pictureBox1.Image);
// In the mouse move event
var pixelcolor = mybitmap.GetPixel(e.X, e.Y);
// Displays R / G / B Color
pixelcolor.ToString()
Mouse类中有一个静态方法,允许您获取鼠标指针相对于另一个元素的位置。请查看Mouse.GetPosition(UIElement)。
以下是使用方法。
Point point = Mouse.GetPosition(pictureBox);
Debug.WriteLine("X: " + point.X +"\n Y: "+ point.Y);