Ignacio Soler Garcia
Ignacio Soler Garcia

Reputation: 21855

How to know when a control (or window) has been rendered (drawn) in WPF?

I need to store the content of a Window into an image, save it and close the window. If I close the window on the Loaded event the image contains the Window with some items are drawn ok, some others are only half drawn or distorted while others are not on the image.

If I put a timer and close the window after a certain amount of time (something between 250ms and 1sec depending on the complexity of the window) the images are all ok.

Looks like the window needs some time to completely render itself. Is there a way to know when this rendering has been done to avoid using a Timer and closing the window when we know it has completed its rendering?

Thanks.

Upvotes: 5

Views: 12584

Answers (4)

Paul McCarthy
Paul McCarthy

Reputation: 890

I know this is an old question but this might help a frustrated searcher (like I often am)

What worked for me is DispatcherTimer and Yield so it waits and gives the Render thread time to do the actual screen updates. In the debugger you can put a breakpoint at 'await longOperation()' and see the label has already been updated.

// update label you want user to see before longOperation starts
await UIFinishRender();
await longOperation();

public async Task UIFinishRender()
{
    // want to wait for render thread finish drawing
    RenderCount = 0;    // private int

    DispatcherTimer timer = new DispatcherTimer();
    timer.Interval = TimeSpan.FromMilliseconds(1);
    timer.Tick += timer_Tick;
    timer.Start();

    while (RenderCount < 30)
    {
        await Dispatcher.Yield();
    }
}

void timer_Tick(object sender, EventArgs e)
{
    RenderCount++;
}

Upvotes: -2

Joe Sonderegger
Joe Sonderegger

Reputation: 804

I used the method on SizeChanged.

public partial class MyUserControl: UserControl
{

    public MyUserControl()
    {
        InitializeComponent();
        SizeChanged += UserControl_DoOnce; //register
    }

    private void UserControl_DoOnce(object sender, SizeChangedEventArgs e)
    {
        if (ActualHeight > 0)//Once the object has size, it has been rendered.
        {
              SizeChanged -= UserControl_DoOnce; //Unregister so only done once
        }
    }
}

This is the only method that I found to work reliably from the control without referencing the Window.

Upvotes: 0

Dtex
Dtex

Reputation: 2623

I think you are looking for the ContentRendered event

Upvotes: 12

Pank
Pank

Reputation: 690

I had the similar problem in the application I am working, I solved it by using following code, try it and let me know if it helps.

 using (new HwndSource(new HwndSourceParameters())
                   {
                       RootVisual =
                           (VisualTreeHelper.GetParent(objToBeRendered) == null
                                ? objToBeRendered
                                : null)
                   })
        {
            // Flush the dispatcher queue
            objToBeRendered.Dispatcher.Invoke(DispatcherPriority.SystemIdle, new Action(() => { }));

            var renderBitmap = new RenderTargetBitmap(requiredWidth, requiredHeight,
                                                      96d*requiredWidth/actualWidth, 96d*requiredHeight/actualHeight,
                                                      PixelFormats.Default);

            renderBitmap.Render(objToBeRendered);
            renderBitmap.Freeze();                

            return renderBitmap;
        }

Upvotes: 2

Related Questions