Windows Forms PictureBox rendering help


I’m developing a simple tool to quickly test animations, and I’m using Windows Forms with PictureBox for rendering.

Currently I can render the animations, but the animation is “laggy” or “jumpy”. I noticed then that the timer skips or there’s small freeze in the rendering.

When I debugged the code and compared elapsed and animDuration, I noticed that the timer skips or there’s small freeze in the Windows Form’s rendering. Strangely it happens always about the same time…

What causes this? Only thing I can think of is that Windows Forms rendering doesn’t work in what I’m trying to achieve, but if it is so what else can I use for quick and simple real-time rendering in Windows Forms?

Console output:

elapsed: 16, dur: 200

elapsed: 17, dur: 200

elapsed: 18, dur: 200

<skip happens>

elapsed: 139, dur: 200

elapsed: 143, dur: 200

elapsed: 144, dur: 200



elapsed: 42, dur: 200

elapsed: 43, dur: 200

elapsed: 45, dur: 200

<skip happens again>

elapsed: 139, dur: 200

elapsed: 140, dur: 200

elapsed: 142, dur: 200

elapsed: 143, dur: 200


// Init

pictureBox.Paint += new PaintEventHandler(pictureBox_OnPaint);

void pictureBox_OnPaint(object sender, PaintEventArgs e)


if (animation.isPlaying)


double elapsed = stopwatch.ElapsedMilliseconds - lastMilliseconds;

if (elapsed >= animDuration)


lastMilliseconds = stopwatch.ElapsedMilliseconds;

animation.Update(); // update row and column



if (pictureBox.Image != null)



Rectangle destRect = new Rectangle(0, 0, animation.frameWidth, animation.frameHeight);

Rectangle srcRect = new Rectangle(animation.row * animation.frameWidth, animation.col * animation.frameHeight, animation.frameWidth, animation.frameHeight);

e.Graphics.DrawImage(pictureBox.Image, destRect, srcRect, GraphicsUnit.Pixel);




submitted by /u/Trouoveiacco
[link] [comments]

Leave a Reply