How to know when a control (or window) has been rendered (drawn) in WPF?
Solution 1
I think you are looking for the ContentRendered event
Solution 2
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;
}
Ignacio Soler Garcia
I am now acting as a delivery manager focused on the three main pillars of software creation: People, Procedures and Code working mainly with Javascript teams (React / Redux / Node) building applications 100% in the cloud with CI/CD, etc. I am open to proposals, let's talk. Previously I used to be an experienced technical leader commanding .Net technologies, passionate about Agile methodologies and a people person.
Updated on June 05, 2022Comments
-
Ignacio Soler Garcia almost 2 years
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.
-
Ignacio Soler Garcia about 12 yearsUf. Looks like everyone has its own solution to the problem and there aren't two that looks the same. I'll take a look but why are you instantiating a HwndSource that is not used anywhere?
-
Pank about 12 yearsIt forces the parent window to wait till the children gets rendered. This works well for me...!!!
-
Ignacio Soler Garcia about 12 yearsI'll try, anyway the ContentRendered event looks the simplest way to me.
-
Pank about 12 yearsOk, if you are able to achieve it, please post it here, we will get know a solution which might be better than the one I had implemented.
-
Paul McCarthy almost 2 yearswhat if you update the control and want to know if the update has been rendered?
-
Paul McCarthy almost 2 yearsThis only appears to work for when a Window is first rendered (not updates to controls on it)