矩阵变换图像上的C#光标位置
我需要帮助找到用户双击的图像上的位置 我可以从MouseEventArgs获得控件上的位置,但需要将其转换为图像尺寸。使用新的自定义控件对图像进行了缩放和平移 缩放和平移的效果就像一种魅力(基于) 我得到一个位置,但它是完全关闭从我点击的地方,它似乎是关闭了一个因素,但我无法计算它是什么。 双击事件代码,我自己也尝试过(在矩阵之前注释掉),但效果相同,Vector2注释是我从中找到的参考 下面是关于油漆的活动矩阵变换图像上的C#光标位置,c#,C#,我需要帮助找到用户双击的图像上的位置 我可以从MouseEventArgs获得控件上的位置,但需要将其转换为图像尺寸。使用新的自定义控件对图像进行了缩放和平移 缩放和平移的效果就像一种魅力(基于) 我得到一个位置,但它是完全关闭从我点击的地方,它似乎是关闭了一个因素,但我无法计算它是什么。 双击事件代码,我自己也尝试过(在矩阵之前注释掉),但效果相同,Vector2注释是我从中找到的参考 下面是关于油漆的活动 protected override void OnPaint(PaintEventA
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)
);
}
}
不确定你的代码。(我现在学习时间有点太长了。)
但是这里有两个函数我用来让一个点
跟随矩阵
变换或将其还原回来:
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];
}
注意使用
转换点所需的数组有点复杂 看来我是对的,我的错误是在图像上画出了点,工作代码
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;
但是我试着在像素上画画。在测试了你的代码后,我得到了与我的代码相同的结果,结果证明我在尝试画画像素时使用了点。
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;