矩阵变换图像上的C#光标位置

时间:2015-02-10 08:50:20

标签: c#

我需要有关查找用户双击的图像位置的帮助。

我可以从MouseEventArgs获取控件上的位置,但需要将其转换为图像尺寸。使用新的自定义控件对图像进行了缩放和平移。

缩放和平移工作带来魅力(基于ZoomPicBox by Bob Powell) 我得到一个位置但是从我点击的位置是Total Off,它看起来像是一个因素,但我不知道它是什么。 Double Click事件代码,我自己尝试过(在矩阵之前注释掉),但效果相同,Vector2 Comment是我从Xna

找到的引用
    protected override void OnMouseDoubleClick(MouseEventArgs e)
    {
        if (e != null)
        {
            if (e.Button == MouseButtons.Left)
            {
                //_ImgDoubleClick.X = (int)(e.Location.X / this.Zoom) - this.AutoScrollPosition.X;
                //_ImgDoubleClick.Y = (int)(e.Location.Y / this.Zoom) - this.AutoScrollPosition.Y;

                using (Matrix mx = new Matrix(_zoom, 0, 0, _zoom, 0, 0))
                {
                    mx.Translate(this.AutoScrollPosition.X / _zoom, this.AutoScrollPosition.Y / _zoom);

                    //Vector2 worldPosition = Vector2.Transform(mousePosition, Matrix.Invert(viewMatrix));
                    mx.Invert();
                    Point worldPosition = VectorTransform(e.Location, mx);

                    _ImgDoubleClick.X = worldPosition.X;
                    _ImgDoubleClick.Y = worldPosition.Y;
                }
            }

        }            


        base.OnMouseDoubleClick(e);
    }

    //-------------------------------------
    private Point VectorTransform(Point vector, Matrix matrix)
    {
        //var tempX = (matrix.M11 * vector.X) + (matrix.M21 * vector.Y) + matrix.M31;
        //var tempY = (matrix.M12 * vector.X) + (matrix.M22 * vector.Y) + matrix.M32;

        int tempX = (int)((matrix.Elements[0] * vector.X) + (matrix.Elements[2] * vector.Y) + matrix.Elements[4]);
        int tempY = (int)((matrix.Elements[1] * vector.X) + (matrix.Elements[3] * vector.Y) + matrix.Elements[5]);

        return new Point(tempX, tempY);
    }

以下是On paint Event

protected override void OnPaint(PaintEventArgs e)
{
//if no image, don't bother
if (_image == null)
{
    base.OnPaintBackground(e);
    return;
}
//Set up a zoom matrix
using (Matrix mx = new Matrix(_zoom, 0, 0, _zoom, 0, 0))
{
    if (e != null)
    {
        //now translate the matrix into position for the scrollbars
        mx.Translate(this.AutoScrollPosition.X / _zoom, this.AutoScrollPosition.Y / _zoom);
        //use the transform
        e.Graphics.Transform = mx;
        //and the desired interpolation mode
        e.Graphics.InterpolationMode = _interpolationMode;
        //Draw the image ignoring the images resolution settings.
        e.Graphics.DrawImage(_image, new Rectangle(0, 0, this._image.Width, this._image.Height), 0, 0, _image.Width, _image.Height, GraphicsUnit.Pixel);
    }
}
//mx.Dispose();
base.OnPaint(e);
}

缩放系数设置

    public float Zoom
    {
        get
        {
            return _zoom;
        }
        set
        {
            if (value < 0 || value < 0.00001)
                value = 0.00001f;
            _zoom = value;
            UpdateScaleFactor();
            Invalidate();
        }
    }

    //----------------------------------
    /// <summary>
    /// Calculates the effective size of the image
    ///after zooming and updates the AutoScrollSize accordingly
    /// </summary>
    private void UpdateScaleFactor()
    {
        if (_image == null)
            this.AutoScrollMinSize = this.Size;
        else
        {
            this.AutoScrollMinSize = new Size(
              (int)(this._image.Width * _zoom + 0.5f),
              (int)(this._image.Height * _zoom + 0.5f)
              );
        }
    }

来自我控制的使用声明。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.ComponentModel;

2 个答案:

答案 0 :(得分:1)

看起来我是对的Allong我的错误就是我在图像上的重点,工作代码。

    private void zoomPicBox1_MouseDoubleClick(object sender, MouseEventArgs e)
    {
        int x = zoomPicBox1.ImgDoubleClick.X;
        int y = zoomPicBox1.ImgDoubleClick.Y;
        using (Graphics grD = Graphics.FromImage(_bmp))
        //using (Graphics grD = Graphics.FromImage(zoomPicBox1.Image))
        {
            grD.PageUnit = GraphicsUnit.Pixel;
            grD.DrawEllipse(Pens.Black, x - 4, y - 4, 8, 8);
            grD.DrawEllipse(Pens.Black, x - 3, y - 3, 6, 6);
            grD.DrawEllipse(Pens.Black, x - 2, y - 2, 4, 4);
            grD.DrawEllipse(Pens.Black, x - 1, y - 1, 2, 2);
        }
        this.zoomPicBox1.Invalidate();
    }

我的问题是我使用的是Points而不是Pixel

grD.PageUnit = GraphicsUnit.Point;

但我试图在一个像素上画画。

答案 1 :(得分:0)

不确定您的代码。 (现在我学习的时间有点太长了。)

但是我使用了两个函数让Point遵循Matrix转换或将其还原为:

    static public PointF reLocation(PointF pt)
    {
        PointF[] eLocations = new PointF[] { pt };
        Matrix RM = ScaleMatrix.Clone();
        RM.Invert();
        RM.TransformPoints(eLocations);
        return eLocations[0];
    }

    static public PointF unreLocation(PointF pt)
    {
        PointF[] eLocations = new PointF[] { pt };
        Matrix RM = ScaleMatrix.Clone();
        RM.TransformPoints(eLocations);
        return eLocations[0];
    }

请注意使用TransformPoints时需要稍微复杂的数组!