C# pictureBox1变换后如何得到XY
我有一张画在图B上的点列表 pictureBox1已被转换 现在,我想得到当我悬停在任何绘制的点上时绘制的点的XY坐标 当我将鼠标悬停在pictureBox1上时,我得到的是pictureBox的XY,而不是经过变换的XY 你能帮我到火车站吗 谢谢C# pictureBox1变换后如何得到XY,c#,graphics,picturebox,scaletransform,C#,Graphics,Picturebox,Scaletransform,我有一张画在图B上的点列表 pictureBox1已被转换 现在,我想得到当我悬停在任何绘制的点上时绘制的点的XY坐标 当我将鼠标悬停在pictureBox1上时,我得到的是pictureBox的XY,而不是经过变换的XY 你能帮我到火车站吗 谢谢 private void pictureBox1_Paint(object sender, PaintEventArgs e) { int height = pictureBox1.ClientSize.Height
private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
int height = pictureBox1.ClientSize.Height / 2;
int width = pictureBox1.ClientSize.Width / 2;
//=====
//scale
//=====
e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
e.Graphics.TranslateTransform(-width, -height);
e.Graphics.ScaleTransform(2f, 2f);
//===========
//draw center
//===========
e.Graphics.DrawLine(new Pen(Color.Black, 0.5f), new Point(width - 2, height), new Point(width + 2, height));
e.Graphics.DrawLine(new Pen(Color.Black, 0.5f), new Point(width, height - 2), new Point(width, height + 2));
//===========
//draw points
//===========
foreach (var p in Points)
{
Point[] pts = new Point[] { new Point(p.X, p.Y) };
Rectangle rc = new Rectangle(pts[0], new Size(1, 1));
e.Graphics.DrawRectangle(Pens.Red, rc);
}
}
您可以创建具有必要变换的矩阵,并通过多重变换(…)将其应用于pictureBox1_Paint(…):
然后,您可以使用矩阵::TransformPoints(…)获得变换的XY您可以创建具有必要变换的矩阵,并通过多重变换(…)将其应用于pictureBox1_Paint(…):
然后您可以使用Matrix::TransformPoints(…)来获得转换的XY作为@Vitaly答案的变体,您可以这样做: 转换
图形
对象后,可以将其转换矩阵e.Graphics.Transform
保存在变量中:
Matrix matrix = null;
private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
int height = pictureBox1.ClientSize.Height / 2;
int width = pictureBox1.ClientSize.Width / 2;
//=====
//scale
//=====
e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
e.Graphics.TranslateTransform(-width, -height);
e.Graphics.ScaleTransform(2f, 2f);
matrix = e.Graphics.Transform; // save the transformation matrix!
...
这是必要的,因为在Paint
事件之后,Transformation数据会丢失!
请注意,函数不能很好地用于此目的,因为它只将状态放在堆栈上以一次,这意味着它不会以持久方式保存这些数据
稍后,您可以使用矩阵
和此功能使用相同的矩阵变换点
,或反转变换,例如,对于鼠标坐标:
PointF transformed(Point p0, bool forward)
{
Matrix m = matrix.Clone();
if (!forward) m.Invert();
var pt = new Point[] { p0 };
m.TransformPoints(pt);
return pt[0];
}
现在我的MouseMove
事件显示了原始和重新转换的位置:
private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
label1.Text = e.Location + " <-> " + transformed(e.Location, false) ;
}
这首先清除所有转换,然后通过调用转换函数在相同位置绘制更大的矩形
请注意,这也适用于旋转的图形对象。(虽然上一次测试没有绘制旋转的较大矩形,但只是移动到了正确的位置。)
还请注意,我返回PointF
,以便在使用分数进行缩放时获得更好的精度。您可以使用Point.Round
(或Point.Truncate
)获取点
请务必查看:它们包含您使用过的数字:
float scaleX = matrix.Elements[0];
float scaleY = matrix.Elements[3];
float transX = matrix.Elements[4];
float transY = matrix.Elements[5];
最后:这是非常值得研究的 作为@Vitaly答案的变体,您可以这样做:
转换图形
对象后,可以将其转换矩阵e.Graphics.Transform
保存在变量中:
Matrix matrix = null;
private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
int height = pictureBox1.ClientSize.Height / 2;
int width = pictureBox1.ClientSize.Width / 2;
//=====
//scale
//=====
e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
e.Graphics.TranslateTransform(-width, -height);
e.Graphics.ScaleTransform(2f, 2f);
matrix = e.Graphics.Transform; // save the transformation matrix!
...
这是必要的,因为在Paint
事件之后,Transformation数据会丢失!
请注意,函数不能很好地用于此目的,因为它只将状态放在堆栈上以一次,这意味着它不会以持久方式保存这些数据
稍后,您可以使用矩阵
和此功能使用相同的矩阵变换点
,或反转变换,例如,对于鼠标坐标:
PointF transformed(Point p0, bool forward)
{
Matrix m = matrix.Clone();
if (!forward) m.Invert();
var pt = new Point[] { p0 };
m.TransformPoints(pt);
return pt[0];
}
现在我的MouseMove
事件显示了原始和重新转换的位置:
private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
label1.Text = e.Location + " <-> " + transformed(e.Location, false) ;
}
这首先清除所有转换,然后通过调用转换函数在相同位置绘制更大的矩形
请注意,这也适用于旋转的图形对象。(虽然上一次测试没有绘制旋转的较大矩形,但只是移动到了正确的位置。)
还请注意,我返回PointF
,以便在使用分数进行缩放时获得更好的精度。您可以使用Point.Round
(或Point.Truncate
)获取点
请务必查看:它们包含您使用过的数字:
float scaleX = matrix.Elements[0];
float scaleY = matrix.Elements[3];
float transX = matrix.Elements[4];
float transY = matrix.Elements[5];
最后:这是非常值得研究的 pictureBox1已经被转换了你的意思是图像的东西被缩放了吗?您只需按照缩放和转换数字进行操作,或者执行相同的操作,或者执行相反的操作,这取决于您是否有要处理相同的点,或者单击已转换的像素得到的点:x2=x1/2f-宽度图片Box1已转换您是否指的是正在缩放的图像?您只需要按照缩放和转换数字进行操作,或者执行相同的操作,或者执行相反的操作,这取决于您是否有要处理相同的点,或者单击已转换的像素得到的点:x2=x1/2f-宽度