我需要帮助找到用户双击图像上的位置。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;
经过测试你的代码后,我得到了与我的代码相同的结果,结果显示我在使用**点**时尝试绘制到**像素**。 – 2015-02-10 12:02:26