110,534
社区成员
发帖
与我相关
我的任务
分享
//MessageBox.Show(e.X.ToString(), e.Y.ToString());//显示X坐标
获取坐标值,然后在picturebox上画矩形,为什么矩形画的位置偏差好大?求大神指导,谢谢了。 if (e.X > 330 && e.X < 360 && e.Y > 140 && e.Y < 165)
{
Graphics g = Graphics.FromImage(pictureBox1.Image);
Pen mpen = new Pen(Color.Red, 4);
g.DrawRectangle(mpen,330, 140, 50, 50);
this.pictureBox1.Refresh();
}
MessageBox.Show(e.X.ToString(), e.Y.ToString());
int Left =357 * pictureBox1.BackgroundImage.Left / pictureBox1.Width;
int Top = 192 * pictureBox1.BackgroundImage.Height / pictureBox1.Height;
int Width = 95 * pictureBox1.BackgroundImage.Width / pictureBox1.Width;
int Height = 75 * pictureBox1.BackgroundImage.Height / pictureBox1.Height;
if (e.X > 357 && e.X < 452 && e.Y > 192 && e.Y < 267) {
Graphics g = Graphics.FromImage(pictureBox1.Image);
Pen mpen = new Pen(Color.Red, 4);
g.DrawRectangle(mpen,Left, Top, Width, Height);
this.pictureBox1.Refresh();
}
你说的是这个意思吧。private bool drawRect = false;
private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
if (drawRect)
{
Pen mpen = new Pen(Color.Red, 4);
e.Graphics.DrawRectangle(mpen, 330, 140, 50, 50);
}
}
private void pictureBox1_MouseClick(object sender, MouseEventArgs e)
{
if (e.X > 330 && e.X < 360 && e.Y > 140 && e.Y < 165)
{
drawRect = true;
this.pictureBox1.Invalidate();
}
}
/// <summary>
/// 坐标缩放,返回符合图片大小的坐标
/// </summary>
/// <param name="r">画图框坐标</param>
/// <param name="x">画板宽</param>
/// <param name="y">画板高</param>
/// <param name="width">图片实际宽</param>
/// <param name="height">图片实际高</param>
/// <returns></returns>
private Rectangle ResizeRect(Rectangle r)
{
int Left = r.Left * frmBackGround.BackgroundImage.Width / frmBackGround.Width;
int Top = r.Top * frmBackGround.BackgroundImage.Height / frmBackGround.Height;
int Width = r.Width * frmBackGround.BackgroundImage.Width / frmBackGround.Width;
int Height = r.Height * frmBackGround.BackgroundImage.Height / frmBackGround.Height;
r = new Rectangle(Left, Top, Width, Height);
return r;
}