在 C# 中刷新 PictureBox
我创建了一个矩形列表并将它们绘制在 PictureBox
中。在代码的其他地方,我们从列表中删除了一些矩形,但是当我调用 PictureBox.Refresh()
时,它显示了之前的结果:所有矩形。
我尝试创建图片的克隆,并一一重新绘制所有矩形,但它有同样的问题。
请给我一些关于如何绘制当前矩形列表的想法。
Rectangle r = lanes[i];//lanes is list of rectangles
Pen pen = new Pen(Color.Red, 2);
Graphics g = pictureBox1.CreateGraphics();
g.DrawRectangle(pen, r);
I created a list of rectangles and draw them in a PictureBox
. Elsewhere in the code we remove some rectangles from the list, but when I call PictureBox.Refresh()
it shows the previous result: all the rectangles.
I tried creating a clone of the picture, and repainting all the rectangles one by one, but it has the same problem.
Please, can you give me some ideas on How to paint the current rectangles-list.
Rectangle r = lanes[i];//lanes is list of rectangles
Pen pen = new Pen(Color.Red, 2);
Graphics g = pictureBox1.CreateGraphics();
g.DrawRectangle(pen, r);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您没有提供足够的信息,我不知道“它没有提供正确的信息”是什么意思。
上面绘制矩形的代码应该放入控件的 Paint 事件处理程序中。而当你想让图片框重画时,调用Invalidate方法(你可能还需要调用Update方法)。
You haven't provided enough information, and I have no idea what "it does not give the correct one" means.
Your code above that draws the rectangles should go in the control's Paint event handler. And when you want the picture box to redraw, call the Invalidate method (you may also need to call the Update method).
出于兴趣,我继续实施了这个,或多或少是我认为应该做的。
这是我的代码。
此代码的发布不附带任何保证(明示或暗示)。都是你的了。用它做任何你喜欢做的事。无论发生什么,这都不是我的问题!
干杯。基思.
Out of interest I went-ahead and implemented this, more or less as I think it should be done.
Here's my code.
This code is published with NO warranties (explicit or implied). It's all yours. Do whatever you like with it. Just whatever happens, IT'S NOT MY PROBLEM!
Cheers. Keith.