在PICTUREBOX画图超出边界!

gabsgabs 2014-07-12 06:24:30
按照下面代码出现了这种情况
请问为什么画图会超出边界啊,应该是刚刚好才对。
private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
Graphics g = e.Graphics;
int x1 = pictureBox1.Location.X;
int x2 = pictureBox1.Location.X + pictureBox1.Size.Width / 2;
int x3 = pictureBox1.Location.X + pictureBox1.Size.Width;
int y1 = pictureBox1.Location.Y;
int y2 = pictureBox1.Location.Y + pictureBox1.Size.Height / 2;
int y3 = pictureBox1.Location.Y + pictureBox1.Size.Height;

Pen p = new Pen(Color.Red, 1);
g.DrawLine(p, new Point(x1, y1), new Point(x3, y1));
g.DrawLine(p, new Point(x1, y2), new Point(x3, y2));
g.DrawLine(p, new Point(x1, y3), new Point(x3, y3));
g.DrawLine(p, new Point(x1, y1), new Point(x1, y3));
g.DrawLine(p, new Point(x2, y1), new Point(x2, y3));
g.DrawLine(p, new Point(x3, y1), new Point(x3, y3));

}
...全文
262 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
Ptrtoptr 2014-07-13
  • 打赏
  • 举报
回复
在picturebox里画线,坐标相对于picturebox的(0,0), 你得到的坐标是相对于窗口坐标(0,0), 在g里用Translatetransform之类的坐标系转换,你试试,具体怎么搞,我也记不起来了
xingeddx 2014-07-13
  • 打赏
  • 举报
回复
楼主,你加这个是什么情况, pictureBox1.Location.X ?这个值是picturebox在父窗体的位置, pictureBox1.Size.Width;已经到了picturebox的最右边了,再加个那个位置超过边界也正常了
gabsgabs 2014-07-12
  • 打赏
  • 举报
回复
请指点,我觉得没错啊。。就是坐标平移
devmiao 2014-07-12
  • 打赏
  • 举报
回复
坐标计算错误,为什么你觉得“刚刚好”。

110,500

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧