1

I've written a custom control to render images, since the default PictureBox led to a lot of lag. But I couldn't get rid of the stutter that happens when a new image is loaded.

The control is double buffered and uses the Paint event to draw the image manually:

Image img;

void ImageDrawer_Paint(object sender, PaintEventArgs e)
{
    e.Graphics.PixelOffsetMode = PixelOffsetMode.Half;
    e.Graphics.InterpolationMode = InterpolationMode.NearestNeighbor;

    var rect = new Rectangle(0, 0, img.Width, img.Height);

    e.Graphics.DrawImage(img, rect);
}

The problem is that I'm trying to draw a video feed with about 30fps. This leads to the entire form lagging like crazy. When I set it to 1fps it's smooth, but when moving the window I can clearly feel a stutter every second.

Is there a faster way to draw an image to the screen? If I could get smooth performance with 30fps, that would be perfect.

Zelos
  • 107
  • 6
  • 1
    What size is the image? Maybe you can speed up drawing by making sure it has precalculated values, ie the `Format32bppPArgb` format. – TaW Nov 27 '21 at 08:54
  • The size of the image actually doesn't seem to have that big of an impact. I've just tried it with 5120x5120 and there's a noticable stutter, which should be expected at that size. But 100x100 has a similar stutter, just a tiny bit shorter. – Zelos Nov 27 '21 at 08:58
  • 1
    _size of the image actually doesn't seem to have that big of an impact_ that would be rather amazing. How do you control the process? – TaW Nov 27 '21 at 09:05
  • There's nothing else really going on. I just set the Image to the image I get from the camera API and that's all. But I did try it by pre-loading a lot of images into memory and then setting img in sequence every few ms. Still led to the exact same stutter, which makes me think the problem is the drawing itself. – Zelos Nov 27 '21 at 09:20
  • 1
    See [Image sequence to video stream?](https://stackoverflow.com/questions/9744026/image-sequence-to-video-stream). – dr.null Nov 27 '21 at 09:57
  • 1
    Did you try to check the pixelformat? Do you use a Timer? – TaW Nov 27 '21 at 10:22
  • @dr.null I'm not sure if the extensions mentioned in that question are of any help. I don't have actual video files, just Bitmaps. And the rendering is what's creating the stutter. The solutions seem to also just use DrawGraphics, which I already do. – Zelos Nov 27 '21 at 10:33
  • @TaW I'm not sure what I'm supposed to do about the PixelFormat. Doesn't make any difference if I'm using 8bitindexed (what the camera is giving me) or the one you mentioned. I am using a timer, yes. I just set it to 33ms to get 30fps. – Zelos Nov 27 '21 at 10:35
  • 2
    The precalculated images can be drawn to the screen a lot faster, at least in theory. But since you use a different image each time, it may not really help, since a new image must be created for display interally anyway. Note that a Timer will not be really smooth as it won't guarantee its timing (can't be faster but may well be slower than Interval) and it is not synch'ed with the monitor. Winforms is not well suited for animation.# – TaW Nov 27 '21 at 11:04
  • 1
    You gonna need to be using a DirectX-based lib to render that amount of images/frames per second in WinForms. The link above intro you to some libs and examples. You can't stream or render video using a pbox and timer and you have seen what happens if you do so. Google for DirectShow (old) and the newer libs. – dr.null Nov 27 '21 at 11:09
  • 1
    I'll take a look at those libs again, thank you. – Zelos Nov 27 '21 at 13:03

0 Answers0