Debugging the Painting code of a control - stepping through System.Drawing code?

gb 66 Reputation points
2024-05-11T18:18:22.0666667+00:00

I'm working on a custom control that has a fairly hard-to-diagnose bug in its Painting/Drawing code, which is using GDI+ System.Drawing tools. This is in a sub-method being called from an overridden OnPaintBackground method. It would be much easier if I could step through the different loops of the painting code and see the results as they happen, however, it seems like the output (my custom control sitting in the Designer of an instance of devenv.exe that's been launched with the debugger attached) is not immediately drawn to the control as the debugger steps through the various Graphics.Draw* calls. The control remains in a frozen state with the previous area of the screen remaining drawn on top of it, until I've allowed the breakpoint to continue about three times. Then the method is no longer called, and the newly-drawn brackground has been applied to the control.

I've tried calling Graphics.Flush(FlushIntention.Sync), however stepping over this does not seem to apply anything graphically to the control.

Please let me know if there is any way to force some kind of render-pass on the control after making a .Draw call, or if I need to allow the OnPaintBackground call to finish before everything is applied to the control's surface, and there's no way to accomplish what I want to do. Thank you in advance!

Windows Forms
Windows Forms
A set of .NET Framework managed libraries for developing graphical user interfaces.
1,904 questions
{count} votes

Accepted answer
  1. Viorel 118.2K Reputation points
    2024-05-11T19:11:27.5666667+00:00

    Maybe the DoubleBuffered property of control and form should be set to false.

    1 person found this answer helpful.
    0 comments No comments

2 additional answers

Sort by: Most helpful
  1. Q&A Assist
    2024-05-11T18:18:22.0766667+00:00
    AI generated content. This question contains an answer created with AI from Q&A Assist. Learn more

    When debugging custom control painting code, it can be difficult to see the results of Graphics.Draw* calls in real-time. One option is to allow the OnPaintBackground call to finish before everything is applied to the control's surface. Another option is to use the Invalidate method to force a redraw of the control after making a Draw call. It is recommended to avoid calling OnPaint directly from the control and instead use Invalidate or a method that invokes Invalidate. Additionally, to ensure that the control is redrawn every time it is resized, add the following line to the constructor of your control:

    SetStyle(ControlStyles.ResizeRedraw, true);
    

    References:

    0 comments No comments

  2. gb 66 Reputation points
    2024-05-11T18:53:38.91+00:00

    I think I've just found my answer, no need for any code hacks.

    StackOverflow: How to debug GDI+ in c#?

    CodeProject: Graphics Debugger Visualizer

    VS 2008 support seems sketchy at best. For Visual Studio 2022, I found:

    Marketplace: KGy SOFT Image DebuggerVisualizers x64

    A free (and open source) extension that lets you view objects like Graphics and see its progress at every step of execution.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.