Recently, I was facing an issue with a third-party Image Viewer to apply multiple operations before redrawing the images.
Third-party control was not allowed to suspend and resume the painting of images inherently. After searching for various solutions, I found a solution to fix this issue using the windows message “WM_SETREDRAW“ from an application to Windows to allow changes in that control to be redrawn or to prevent changes in that control from being redrawn.
public class UIDrawController
{
[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(Control ctrl)
{
SendMessage(ctrl.Handle, WM_SETREDRAW, false, 0);
}
public static void ResumeDrawing(Control ctrl)
{
SendMessage(ctrl.Handle, WM_SETREDRAW, true, 0);
ctrl.Refresh();
}
}
Example Usage:
public ImageViewer : UserControl
{
public void LoadDocument(string filename)
{
UIDrawController.SuspendDrawing(this);
OpenImage(filename);
FitToWidth();
Rotate90();
ApplyImageFilters();
UIDrawController.ResumeDrawing(this);
}
}