WPF如何显示Image.Source(BitmapSource)像素位置?

Mar*_*rio 1 wpf pixel coordinates mousemove

假设我有一个以缩放方式显示其来源的图像,我如何使用MouseMove事件在标签或文本块中显示光标所在的像素位置?

(我需要像素坐标而不是图像相对于其大小的坐标)

提前致谢.

Job*_*Joy 6

您可以从ImageSource中找出实际的像素高度和宽度.

    ImageSource imageSource = image.Source;
    BitmapImage bitmapImage = (BitmapImage) imageSource ;
Run Code Online (Sandbox Code Playgroud)

现在,因为您在Image控件中显示了图像.您可以轻松地将鼠标位置映射到像素比例.

pixelMousePositionX = e.GetPosition(image).X * bitmapImage.PixelWidth/image.Width;
pixelMousePositionY = e.GetPosition(image).Y * bitmapImage.PixelHeight/image.Height;
Run Code Online (Sandbox Code Playgroud)

玩得开心

Jobi Joy