Windows Forms’ most versatile control is arguably the PictureBox. By overriding the OnPaint method, the PictureBox can be used to recreate the functionality of most other controls. One challenge, however, is using the PictureBox for animations or graphics applications that require a quick frame rate. Below are a few methods for pulling the maximum possible FPS from the C# PictureBox control.
The first and easiest technique is to enable DoubleBuffering. A necessity for any graphics application, double buffering removes stutter and flickering during animation. Without double buffering, the PictureBox will often turn completely white for a split second while the new image is rendered. Setting this property to true will result in an immediate performance improvement and enable a lower required frame rate.
The next technique is to analyze the code and remove all performance-intensive operations in the render loop (OnPaint method). All Bitmap instantiation should occur outside the loop, or Bitmaps should be buffered and only created in the loop when absolutely necessary. If possible, Bitmaps should be reused so that they do not need to be re-declared, and large files should be loaded during initial Program load, and not during execution.
Next, one of the more challenging optimizations is to implement clipping. Often the entire image does not need to be redrawn for animation, since only a small “clipping window” changes. The developer needs to analyze the code and see which redraws can be narrowed down to small portions, and using buffers, redraw only those specific areas which require invalidation in the current render loop.
Finally, once all of these optimizations have been integrated, the next step is to look for memory and execution bottlenecks. Red Gate provides an excellent set of tools for this with the ANTS Memory Profiler and ANTS Performance Profiler. These tools enable analysis of exactly how long each instruction takes to execute, and how much memory is used by which objects, at each snapshot of program execution. They will show exactly where memory leaks might occur, and the location of the slowest lines of code in the rendering loop. This final task will challenge the developer to create even more optimization using advanced techniques.
On a last note, it is a good idea to run the full Windows Updates sequence and make sure each client computer has the latest versions of the .NET framework and any other Windows components. A variety of rendering issues could be a result of errors on Microsoft’s side in the actual framework.
These five techniques – double buffering, render loop optimization, clipping, third-party code analysis, and Windows Updates, will likely solve the performance bottleneck in the rendering sequence, and create seamless animation for your C# PictureBox custom control.
Written by Andrew Palczewski
About the Author
Andrew Palczewski is CEO of apHarmony, a Chicago software development company. He holds a Master's degree in Computer Engineering from the University of Illinois at Urbana-Champaign and has over ten years' experience in managing development of software projects.