Paint 事件处理程序在几次迭代后停止执行
我有一个 Windows 窗体,可以循环显示以幻灯片形式显示在窗体上的图像。我这样做的方法是让一个面板控制它所在窗体的大小,并添加一个事件处理程序来绘制内存中存在的图像对象。
void panel_Paint(object sender, PaintEventArgs e)
{
if (_bShowImage)
{
Point leftCorner = new Point((this.Bounds.Width / 2) - (_image.Width / 2), (this.Bounds.Height / 2) - (_image.Height / 2));
e.Graphics.DrawImage(_image, leftCorner);
_bShowImage = false;
}
}
当加载新图像并由 _image 引用时,我强制面板重绘:
_bShowImage = true;
_panel.Refresh();
紧接着,图像被处理并从全局变量中取消引用:
_image.Dispose();
_image = null;
我已经看到它工作了一段时间,比如说 5 次迭代,那么 panel_Paint() 处理程序不会被调用。我使用 2-3 个 JPG 进行显示,我知道它们没有损坏,因为它们在前 x 次显示得很好。我在面板的 Refresh() 方法周围放置了调试线,该方法执行良好。就好像对处理程序的调用已被删除。以前有人遇到过这个问题吗?
I've got a Windows Form that circulates through images which are displayed on the form as a slideshow. The way I'm doing this is to have a Panel control the size of the form it resides in, and add an event handler that draws an Image object that exists in memory.
void panel_Paint(object sender, PaintEventArgs e)
{
if (_bShowImage)
{
Point leftCorner = new Point((this.Bounds.Width / 2) - (_image.Width / 2), (this.Bounds.Height / 2) - (_image.Height / 2));
e.Graphics.DrawImage(_image, leftCorner);
_bShowImage = false;
}
}
When a new Image is loaded and referenced by _image, I'm forcing the Panel to redraw:
_bShowImage = true;
_panel.Refresh();
Immediately afterwards, the image is disposed and the dereferenced from the global variable:
_image.Dispose();
_image = null;
I've seen that it works for a while, say 5 iterations, then the panel_Paint() handler is not being called. I'm using 2-3 JPG's for the display and I know they're not corrupted as they are shown fine for the first x times. I've put debug lines around the Refresh() method of the panel which execute fine. It's as if the call to the handler has been dropped. Has anyone encountered this problem before?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这实在是太倒退了。要么像现在一样使用绘制事件处理程序。这很好(我说它比图片框更好),但是你需要删除 _bShowImage 和 _image.Dispose 东西。相反,您应该在使用新图像启动之前处理 _image。但在那之前。
或者,如果您绝对必须在绘制后立即处理 _image,那么您应该使用 Panel.CreateGraphics 来获取可用于立即绘制 _image 并删除事件的 Graphichs 对象。
就目前情况而言——这真是令人困惑。另外:.Invalidate() 是您几乎总是想要的,而不是 .Refresh()。自 VB6 时代以来,这一直是许多人的观念中的一个问题。
This is so completely backwards. Either you use a paint event handler like now. It's just fine (I say it's better than a picturebox) but then you need to drop that _bShowImage and _image.Dispose stuff. You should instead dispose the _image before you power it up with a new one. But not until that.
Or, if you absolutley must dispose the _image right after it's painted, then you should instead use Panel.CreateGraphics to get a Graphichs object you can use to immediately draw the _image and drop the event.
As it stands - it is just darn confusing. Also: .Invalidate() is what you almost always want -not .Refresh(). That's just something that got stuck in many minds since the VB6 era.
将图片放入图片框中并以这种方式循环浏览它们,这样您就不必每次都强制重新绘制整个窗口,这不是更聪明吗?
只是一个想法……
托尼
Wouldn't it be smarter to put your pictures in a picture box and loop through them in that way, so that you don't force a repaint on the whole window each time?
just a thought...
Tony