10

I have this code:
Window w = // something
w.Loaded += // some code to take a screenshot of the window
w.Show();

The Loaded event fires before the window is fully loaded and I get this image: Window screenshot

I could add a Thread.Sleep or something after w.Show() and before the screenshot but I need to run this piece of code for hundreds of windows.
My question is: Is there another event that fires when the window is fully loaded? or some way to achieve this without putting the thread to sleep.

Thanks

Orlando Osorio
  • 3,116
  • 7
  • 29
  • 52
  • @H.B. I need a screenshot of every window in my project – Orlando Osorio Jan 16 '12 at 21:52
  • 8
    @H.B. this is really not the point of his question and the question makes sense even for 2 windows. I wouldn't like to wait 4 seconds for 2 screenshots, and I sure as hell wouldn't like to be uncertain if i'm going to get then if I use too low wait period. And sometimes you may need (god forbid) 200 screenshots of same window with different data. I understand that architectural concerns are not to be ignored, but problems like this one are not rare and people cant fix them by redesigning and rewriting large code bases. In my view question is very reasonable. – Nikola Radosavljević Jan 16 '12 at 22:12
  • @Nikola: I never said anything about the question... – H.B. Jan 16 '12 at 22:22

1 Answers1

25

Looking into Object Lifetime Events article in MSDN you can find:

The Loaded event is raised before the final rendering, but after the layout system has calculated all necessary values for rendering. Loaded entails that the logical tree that an element is contained within is complete, and connects to a presentation source that provides the HWND and the rendering surface.

You should try some alternative events which are not exactly suited for your need. You should try in following order:

Take note that these events may fire multiple times during lifetime of your window, so write your application with that in mind.

WPF is data driven UI architecture and it's not very pleasant to work with in a way we are used to in WinForms. As someone smarter than me once said, WPF makes hard things trivial and trivial things hard.

Nikola Radosavljević
  • 6,871
  • 32
  • 44
  • same result with the first and the third, the second one throws an exception – Orlando Osorio Jan 16 '12 at 22:12
  • How do you mean it throws exception? Does exception happen in your handler or in .NET framework? Does result of other two look exactly the same or is window chrome properly loaded? Also, try [RenderTargetBitmap.Render](http://msdn.microsoft.com/en-us/library/system.windows.media.imaging.rendertargetbitmap.render.aspx) to render your window to a bitmap. Create instance of the RTB class, create instance of your window, no need to show it, and pass the window to Render method. – Nikola Radosavljević Jan 16 '12 at 22:23
  • The RenderTargetBitmap works for a control but when i try to use it with a Window it only prints a blank image... I was going to make it another SO question :P ... about the exception: fixed it but still same result – Orlando Osorio Jan 16 '12 at 22:29
  • :) You could try calling Arrange method on the window before trying to render. – Nikola Radosavljević Jan 16 '12 at 22:41