开发者

Paint event handler stops executing after a few iterations

开发者 https://www.devze.com 2022-12-11 20:25 出处:网络
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 a

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 enco开发者_运维知识库untered this problem before?


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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号