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

FrameImage.zip