I have created an application. It is a little bit heavy application and i hv tried to minimize the number of functionalities in OnPaint() methods of the various forms. But still my application takes time to repaint the view after it is restored from minimized state. Is there any method to save the view state and other stuff in memory so that we can avoid calling RePaint everytime the app is restored?Im using .NET 2.0 for the application

Dani AI

Generated

Short summary tied to the thread: is seeing long redraw stalls after restoring because expensive work is running repeatedly inside many OnPaint handlers. asked for the code; instead of pasting every OnPaint, switch the painting pattern: keep OnPaint tiny (just blit a cached image) and do the heavy work once (or on a background thread) into an off‑screen buffer that you reuse on restores.

Practical plan (order of work)

  • Measure: temporarily log timings inside OnPaint to find the hot paths (layout, file I/O, parsing, complex geometry).
  • Move non-drawing work out of OnPaint. Do layout/measurement and I/O ahead of time or on a background thread; keep OnPaint only for drawing.
  • Factor painting into a single RenderTo(Graphics g) method. Use it to render into a Bitmap/BufferedGraphics cache and to render directly if cache is missing.
  • Cache a Bitmap sized to the client area; in OnPaint just DrawImageUnscaled(cache). Rebuild the cache only when content changes, on resize, or when DPI changes.
  • Use WinForms painting styles to avoid background erases and flicker.

Minimal pattern example

public MyControl()
{
    SetStyle(ControlStyles.UserPaint | ControlStyles.AllPaintingInWmPaint | ControlStyles.OptimizedDoubleBuffer, true);
    UpdateStyles();
}

protected override void OnPaint(PaintEventArgs e)
{
    if (cachedBitmap != null)
        e.Graphics.DrawImageUnscaled(cachedBitmap, Point.Empty);
    else
        RenderTo(e.Graphics); // fallback until cache exists
}

Important cautions

  • Dispose and recreate the cached Bitmap on resize or DPI change to avoid leaks.
  • Heavy geometry/math can run on a background thread, but create/assign GDI+ Bitmaps on the UI thread (marshal with BeginInvoke/Invoke). See the Windows Forms threading guidance.
  • Do not rely on screen capture of a minimized window; keep your own off‑screen render. For very large scenes consider tiled or incremental caches, or evaluate WPF/Direct2D if hardware-accelerated rendering is needed.

References: Control.SetStyle, , BufferedGraphics class, .

Recommended Answers

All 2 Replies

Show me your OnPaint(..) methods..

The application contains many OnPaint(). For each control i require it to have a paint method. For example: the menustrip control, title bar etc...
My query is whether i can avoid the operations being done in OnPaint() except for the first time and when a application is minimized i save the view and load from memory itself instead of repainting.?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.