Search code examples
c#.netscreenshot

Is Winforms Painting done synchronous or asychronous?


I have a form with three panels. The top panel contains listboxes, the middle panel contains a grid and the bottom panel contains a grid. I need to take a screenshot of the bottom grid but sometimes there are more rows in the grid and you need to scroll to see every row. I want to make the bottom grid the size of the form before I take the screenshot and can do this by setting .Visible to false for the two top panels. My problem is that the screenshot is taken before the form has redrawn itself to show the grid as the size of the form. How can I guarantee that the form has redrawn/repainted before I execute the code to take the screenshot?

I'm using the answer of ArsenMkrt from Capture screenshot of active window? to take the screenshot.

EDIT: The grid is an Infragistics UltraGrid.


Solution

  • Call the form's Update() method. If there are any pending paints then you can be sure they'll be performed and the form is fully drawn. Definitely the case here, hiding the panels requires the form to redraw its background.

    This only works for your own form, not for a window owned by another process. Using the form's DrawToBitmap() method usually works too (no synchronization required), but not all child controls support it. Notably RichTextBox and WebBrowser as well as many other ActiveX controls don't implement the underlying Windows message correctly.