居中后获取ImageBox中图像的坐标?
Get coordinates of image in ImageBox after centering?
我已将图像加载到 ImageBox 中。 ImageBox 锚定在顶部、底部、左侧和右侧。此外,SizeMode
属性设置为 CenterImage
.
此外,我的表单设置为加载时最大化。
我需要获取图像的左上角坐标。图片在窗体最大化时调整大小,同时居中,使坐标发生变化。
我说得有道理吗?我怎样才能找到这些坐标?
通过将其 width/height 除以 2 找到图片框的中心。从这些 x/y 值中减去其中图像 width/height 的一半:
private void pictureBox1_SizeChanged(object sender, EventArgs e)
{
pictureBox1.Invalidate();
}
private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
Point pt = new Point(pictureBox1.Width / 2 - pictureBox1.Image.Width / 2, pictureBox1.Height / 2 - pictureBox1.Image.Height / 2);
Rectangle rc = new Rectangle(pt, pictureBox1.Image.Size);
e.Graphics.DrawRectangle(Pens.Red, rc);
}
我已将图像加载到 ImageBox 中。 ImageBox 锚定在顶部、底部、左侧和右侧。此外,SizeMode
属性设置为 CenterImage
.
此外,我的表单设置为加载时最大化。
我需要获取图像的左上角坐标。图片在窗体最大化时调整大小,同时居中,使坐标发生变化。
我说得有道理吗?我怎样才能找到这些坐标?
通过将其 width/height 除以 2 找到图片框的中心。从这些 x/y 值中减去其中图像 width/height 的一半:
private void pictureBox1_SizeChanged(object sender, EventArgs e)
{
pictureBox1.Invalidate();
}
private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
Point pt = new Point(pictureBox1.Width / 2 - pictureBox1.Image.Width / 2, pictureBox1.Height / 2 - pictureBox1.Image.Height / 2);
Rectangle rc = new Rectangle(pt, pictureBox1.Image.Size);
e.Graphics.DrawRectangle(Pens.Red, rc);
}