user983924
user983924

Reputation: 393

PictureBox paint event executing twice

I added a picture box to a WinForms form and added a paint event. When I debug it, I see that the paint event is called twice.

How can I fix it?

Upvotes: 0

Views: 2723

Answers (2)

David Heffernan
David Heffernan

Reputation: 613262

The picture box control is a relatively loose wrapper around Windows paint cycles and the WM_PAINT event.

The WM_PAINT event is a queued message that the system places in your applications message queue whenever it deems that a window's drawing surface is invalid. This can happen for many reasons:

  • Another window is dragged over the top of your window (although modern windows use buffering to alleviate this).
  • You invalidate the window by calling Invalidate which ultimately calls the Win32 InvalidateRect function.
  • System wide theme preferences changed.
  • Etc. etc.

So, the natural conclusion of this is that your picture box paint handler needs to be written under the assumption that it will be called multiple times.

Upvotes: 4

albertjan
albertjan

Reputation: 7817

The paint event is fired on every redraw which is quite often. The code in your paint event handler should be able to cope with it being called n-times.

Upvotes: 2

Related Questions