Search code examples
c#.netwpfrendertargetbitmap

How do I get a Visual (e.g. a UserControl) to be ready for copying by a RenderTargetBitmap?


The question has now been answered in a comment.

Original question:

The following is not enough:

this.Background = Brushes.Yellow;
RenderTargetBitmap bmp = new RenderTargetBitmap(...);
Measure(...);
Arrange(...);
bmp.Render(this);

If the color was different before this code being executed, the color is still the old color instead of changing to yellow.

So what step am I missing?

(It does work if I add an await Task.Delay(100); before rendering it, but seems to be an ugly workaround.)


Solution

  • [posting a temporary answer until the commenter posts their own.]

    The problem was solved in a comment - adding UpdateLayout before rendering the Visual to the bitmap was enough.