C#在pictureBox图像上面画小矩形框

困爆 2016-12-04 03:15:46
界面上面是两个pictureBox,我需要用鼠标在上面画矩形框,可以画多个。
我现在可以在上面画,但是我在另外一个上面画的时候,另外一个界面上面的框就会消失,我不知道怎么回事。
我现在不知道如何做了,有做过类似的大神请帮帮忙,非常感谢,比较急。万分感谢。
...全文
2971 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
困爆 2016-12-04
  • 打赏
  • 举报
回复
引用 1 楼 xuzuning 的回复:
绘图要在 Paint 事件中进行
下面是我的代码,现在就是一边可以画矩形可以,我用鼠标在另外一个pictureBox上面拉矩形出来的时候,另外一边的所有矩形就消失了。如果可以麻烦你可以帮我看看吗,真的非常非常感谢您! private void IM0001_Load(object sender, EventArgs e) { #region[画矩形] bmsave = new Bitmap(this.picBefore.Width, this.picBefore.Height); g = Graphics.FromImage(bmsave); gform = picBefore.CreateGraphics(); bmsaveAfter = new Bitmap(this.picAfter.Width, this.picAfter.Height); g1 = Graphics.FromImage(bmsaveAfter); gform1 = picAfter.CreateGraphics(); this.event_Registered(); } private void event_Registered() { this.MouseDown += picBefore_MouseDown; this.MouseUp += picBefore_MouseUp; this.MouseMove += picBefore_MouseMove; } protected override void OnPaint(PaintEventArgs e) { //重绘时显示bitmap base.OnPaint(e); gform.DrawImage(this.bmsave, new Point(0, 0)); } private void picBefore_MouseDown(object sender, MouseEventArgs e) { //记录开始点 this.mousedown = true; this.startpoint = e.Location; } private void picBefore_MouseMove(object sender, MouseEventArgs e) { //记录结束点。绘制到窗口上 if (mousedown) { this.endpoint = e.Location; this.Refresh(); gform.DrawImage(this.bmsave, new Point(0, 0)); Rectangle rect = new Rectangle(); this.rect_play(ref rect, "before"); gform.DrawRectangle(new Pen(Color.Red), rect); } } private void picBefore_MouseUp(object sender, MouseEventArgs e) { //记录结束点。绘制到bitmap上 this.endpoint = e.Location; this.mousedown = false; Rectangle rect = new Rectangle(); this.rect_play(ref rect, "before"); g.DrawRectangle(new Pen(Color.Red), rect); gform.DrawImage(this.bmsave, new Point(0, 0)); } #endregion private void rect_play(ref Rectangle rect, string Picflag) { if (Picflag == "before") { //根据两个点确定矩形的左上角点Location if (this.startpoint.X > this.endpoint.X && this.startpoint.Y < this.endpoint.Y)// { rect.Location = new Point(this.endpoint.X, this.startpoint.Y); } else if (this.startpoint.X < this.endpoint.X && this.startpoint.Y > this.endpoint.Y) { rect.Location = new Point(this.startpoint.X, this.endpoint.Y); } else if (this.startpoint.X > this.endpoint.X && this.startpoint.Y > this.endpoint.Y) { rect.Location = this.endpoint; } else { rect.Location = this.startpoint; } //获取两点的X,Y距离 rect.Width = Math.Abs(this.startpoint.X - this.endpoint.X); rect.Height = Math.Abs(this.startpoint.Y - this.endpoint.Y); if (rect.Width == 0 && rect.Height == 0) { //防止误点的时候进行绘制 } else if (rect.Width == 0) { rect.Width = 1; } else if (rect.Height == 0) { rect.Height = 1; } } }
_明月 2016-12-04
  • 打赏
  • 举报
回复
引用 1 楼 xuzuning 的回复:
绘图要在 Paint 事件中进行
你好,你能够给我讲一讲C#.Net中的框架又是怎么一回事么?
xuzuning 2016-12-04
  • 打赏
  • 举报
回复
绘图要在 Paint 事件中进行

110,552

社区成员

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

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

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