WPF如何显示Image.Source(BitmapSource)像素位置? [英] WPF how to show an Image.Source (BitmapSource) pixel position?

查看:710
本文介绍了WPF如何显示Image.Source(BitmapSource)像素位置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我们假设我有一张图像以比例方式显示其来源,我该如何使用 MouseMove事件可在标签或文本块中显示光标所在的像素位置?

Let's suppose I've got an image which shows its source in a scaled way, how could i use a MouseMove event to show in a label or textblock the pixel position in which the cursor is?

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

(I need the pixel coordinates not the coordinates of the image relative to its size)

谢谢.

推荐答案

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

You can find out the actual pixel height and width from the ImageSource.

    ImageSource imageSource = image.Source;
    BitmapImage bitmapImage = (BitmapImage) imageSource ;

现在,既然图像已显示在图像控件中.您可以轻松地将鼠标位置映射到像素"比例.

Now since you got the image displayed in Image control. You can easily map the mouse position to the Pixel scale.

pixelMousePositionX = e.GetPosition(image).X * bitmapImage.PixelWidth/image.Width;
pixelMousePositionY = e.GetPosition(image).Y * bitmapImage.PixelHeight/image.Height;

玩得开心

乔比·乔伊

这篇关于WPF如何显示Image.Source(BitmapSource)像素位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆