WinForms drawing performance relies exclusively on CPU processor, single-threaded. This means you will face several performance issues if your form draws pictures, or any other complex/numerous UI elements. So you may want to disable unnecessary redrawing events to optimize the overall performance.
The dirty trick below will give you a huge performance gain by disabling redrawing when resizing occurs in your form. This code below should be placed in your form's class:
protected override void OnResize(EventArgs e)
{
this.SuspendDrawing();
base.OnResize(e);
this.ResumeDrawing();
}
protected override void OnResizeBegin(EventArgs e)
{
this.SuspendDrawing();
base.OnResizeBegin(e);
}
protected override void OnResizeEnd(EventArgs e)
{
base.OnResizeEnd(e);
this.ResumeDrawing();
}
protected override void OnClosing(CancelEventArgs e)
{
this.SuspendDrawing();
base.OnClosing(e);
this.ResumeDrawing();
}
And this is the extension method which does all the trick:
using System;
using System.Runtime.InteropServices;
using System.Windows.Forms;
namespace Example
{
public static class ControlExtensions
{
[DllImport("user32.dll")]
public static extern int SendMessage(IntPtr hWnd, Int32 wMsg, bool wParam, Int32 lParam);
private const int WM_SETREDRAW = 11;
public static void SuspendDrawing(this Control control) => SendMessage(control.Handle, WM_SETREDRAW, false, 0);
public static void ResumeDrawing(this Control control)
{
SendMessage(control.Handle, WM_SETREDRAW, true, 0);
control.Refresh();
}
}
}