Welcome to MSDN Blogs Sign in | Join | Help

Lester's WPF blog


Simple, easy & beautiful

News

Capturing Frame content

Some time back I got a question about capturing the contents of a frame and it seemed easy, but every time I tried capturing the frame i was getting a blank image. The trick here is that the frame loads the content asynchronously - the simplest thing to do is wait for some time :) ...

The frame is an HwndHost that hosts an ActiveX control to load the web content. So the first thing to do is get hold of the HwndHost and then wait for a few seconds and you have it..

for (Visual v = TheFrame; v != null; v = VisualTreeHelper.GetChild(v, 0) as Visual)

{

      hwndHost = v as HwndHost;

      if (hwndHost != null)

      {

           break;

      }

}

       Now create a DispatcherTimer and wait till it triggers

            DispatcherTimer timer = new DispatcherTimer();

            timer.Tick += TheFrameAfterLoaded;

            timer.Interval = TimeSpan.FromSeconds(5);

            timer.Tag = hwndHost;

            timer.Start();

  

        void TheFrameAfterLoaded(object sender, EventArgs e)

        {

            DispatcherTimer timer = sender as DispatcherTimer;

            HwndHost hwndHost = (HwndHost) timer.Tag;

             timer.Stop();

Image image = new Image();

             image.Source = new DrawingImage(VisualTreeHelper.GetDrawing(TheFrame));

        }

Project is attached :)

Share this post
Posted: Monday, February 26, 2007 4:31 PM by llester
Attachment(s): FrameImage.zip

Comments

jaimer said:

Lester, this does not work for me w/ frame ... [get empty capture].. works for me with other controls..

Running on Vista ...  

# March 8, 2007 2:14 AM

Neil Mosafi said:

Did you try subscribing to the ContentRendered event instead?

# March 25, 2007 12:31 PM

llester said:

its likely that the content is not getting rendered... you could possibly increase the time.

# March 26, 2007 3:55 PM
New Comments to this post are disabled
Page view tracker