我的问题是类似于 如何得到正确位置的像素鼠坐标吗, 与加入的告诫,像是一种潜在的 TransformedBitmap
,其中转和转可以适用,仍然返回的素坐标的原始图像。
我 Window
's设计看起来是这样的:
<DockPanel>
<Label DockPanel.Dock="Bottom" Name="TheLabel" />
<Image DockPanel.Dock="Top" Name="TheImage" Stretch="Uniform" RenderOptions.BitmapScalingMode="NearestNeighbor" MouseMove="TheImage_MouseMove" />
</DockPanel>
和代码隐藏这样的:
public MainWindow()
{
InitializeComponent();
const int WIDTH = 4;
const int HEIGHT = 3;
byte[] pixels = new byte[WIDTH * HEIGHT * 3];
pixels[0] = Colors.Red.B;
pixels[1] = Colors.Red.G;
pixels[2] = Colors.Red.R;
pixels[(WIDTH * (HEIGHT - 1) + (WIDTH - 1)) * 3 + 0] = Colors.Blue.B;
pixels[(WIDTH * (HEIGHT - 1) + (WIDTH - 1)) * 3 + 1] = Colors.Blue.G;
pixels[(WIDTH * (HEIGHT - 1) + (WIDTH - 1)) * 3 + 2] = Colors.Blue.R;
BitmapSource bs = BitmapSource.Create(WIDTH, HEIGHT, 96.0, 96.0, PixelFormats.Bgr24, null, pixels, WIDTH * 3);
TheImage.Source = bs;
}
private void TheImage_MouseMove(object sender, MouseEventArgs e)
{
Point p = e.GetPosition(TheImage);
if (TheImage.Source is TransformedBitmap tb)
TheLabel.Content = tb.Transform.Inverse.Transform(new Point(p.X * tb.PixelWidth / TheImage.ActualWidth, p.Y * tb.PixelHeight / TheImage.ActualHeight)).ToString();
else if (TheImage.Source is BitmapSource bs)
TheLabel.Content = new Point(p.X * bs.PixelWidth / TheImage.ActualWidth, p.Y * bs.PixelHeight / TheImage.ActualHeight).ToString();
}
当徘徊在右下角(我蓝色的容易跟踪)的非改变的图像,你正确地看到坐标的(~4,~3),这些图像方面。
然而,一旦申请转换,例如改变 TheImage.Source = bs;
要 TheImage.Source = new TransformedBitmap(bs, new RotateTransform(90.0));
悬停在蓝色的,而不是让(-4中,~0).
我认为一个可以看的矩阵的实际价值的变换,并确定如何调整这一点在所有各种情况,但它似乎是应该有一个更简单的解决方案使用的逆转变。
Image
在一个Grid
其中有一个背景和移动MouseMove
事件来的Grid
,素坐标已不再准确当的外部界限的图像,因为它只能提供答案,从0..宽度和0..高度。 任何想法如何解决这个问题?