我怎么把老鼠的坐标素坐标的一个TransformedBitmap?

0

的问题

我的问题是类似于 如何得到正确位置的像素鼠坐标吗, 与加入的告诫,像是一种潜在的 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),这些图像方面。

enter image description here

然而,一旦申请转换,例如改变 TheImage.Source = bs;TheImage.Source = new TransformedBitmap(bs, new RotateTransform(90.0));悬停在蓝色的,而不是让(-4中,~0).

enter image description here

我认为一个可以看的矩阵的实际价值的变换,并确定如何调整这一点在所有各种情况,但它似乎是应该有一个更简单的解决方案使用的逆转变。

.net bitmapsource c# image
2021-11-23 06:19:28
1

最好的答案

1

当一个图像要素呈现TransformedBitmap,该中心点的转变是有效地忽略的位被转移到一个适当的协调范围。

由于这一转变是不应用于transfomation一点,你必须补偿,例如这样的:

var p = e.GetPosition(TheImage);

if (TheImage.Source is BitmapSource bs)
{
    p = new Point(
        p.X * bs.PixelWidth / TheImage.ActualWidth,
        p.Y * bs.PixelHeight / TheImage.ActualHeight);

    if (TheImage.Source is TransformedBitmap tb)
    {
        var w = tb.Source.PixelWidth;
        var h = tb.Source.PixelHeight;

        p = tb.Transform.Inverse.Transform(p);

        p = new Point((p.X + w) % w, (p.Y + h) % h);
    }

    TheLabel.Content = $"x: {(int)p.X}, y: {(int)p.Y}";
}

上述代码但是不能正常工作时转换中心是设置一个值比其他 (0,0).

为了使它的工作对于将具有任意的中心点,你要清楚偏移值的变换矩阵:

var p = e.GetPosition(TheImage);

if (TheImage.Source is BitmapSource bs)
{
    p = new Point(
        p.X * bs.PixelWidth / TheImage.ActualWidth,
        p.Y * bs.PixelHeight / TheImage.ActualHeight);

    if (TheImage.Source is TransformedBitmap tb)
    {
        var w = tb.Source.PixelWidth;
        var h = tb.Source.PixelHeight;
        var t = tb.Transform.Value;
        t.Invert();
        t.OffsetX = 0d;
        t.OffsetY = 0d;

        p = t.Transform(p);

        p = new Point((p.X + w) % w, (p.Y + h) % h);
    }

    TheLabel.Content = $"x: {(int)p.X}, y: {(int)p.Y}";
}
2021-11-23 10:54:45

谢谢! 如果一个嵌入 Image 在一个 Grid 其中有一个背景和移动 MouseMove 事件来的 Grid,素坐标已不再准确当的外部界限的图像,因为它只能提供答案,从0..宽度和0..高度。 任何想法如何解决这个问题?
Craig W

有多种方式。 最简单,附加的事件的处理程序的另一种嵌套的父元素具有相同的尺寸如图像。 否则有方法,如TransformToDescendant或类似。
Clemens

不知道我跟进。 我可以翻译点从父网格图像没有问题。 但是例如用户悬停向右转动的图像,我想y坐标是消极的反映,他们是上述界限的图像在原来的方向。 我应该编辑我的问题或开始一个新的?
Craig W

更好地编写新的一个。
Clemens

其他语言

此页面有其他语言版本

Русский
..................................................................................................................
Italiano
..................................................................................................................
Polski
..................................................................................................................
Română
..................................................................................................................
한국어
..................................................................................................................
हिन्दी
..................................................................................................................
Français
..................................................................................................................
Türk
..................................................................................................................
Česk
..................................................................................................................
Português
..................................................................................................................
ไทย
..................................................................................................................
Español
..................................................................................................................
Slovenský
..................................................................................................................