2015-02-10 90 views
2

我需要帮助找到用户双击图像上的位置。C#在矩阵变换图像上的光标位置

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

缩放和平移工程的喜欢神韵(基于ZoomPicBox by Bob Powell) 我得到一个位置,但是它总关从那里我点击,它似乎的一个因素是关闭,但我无法弄清楚它是什么。 我从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); 
    } 

下面发现双击事件代码,我已经尝试过自己(矩阵前注释掉),但有相同的效果,Vector2评论是一个参考的是油漆事件

​​

变焦倍率设置

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语句。

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; 

回答

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稍微复杂的方式!

+0

经过测试你的代码后,我得到了与我的代码相同的结果,结果显示我在使用**点**时尝试绘制到**像素**。 – 2015-02-10 12:02:26

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(); 
    } 

我的问题是我用的,而不是像素

grD.PageUnit = GraphicsUnit.Point; 

点,但我是想画上一个像素。