0

i am trying to create image from webBrowser control's output. the following is what i have tried. and somewhat achieved what i want, but problem is the image is not of full height, it gets trims after some height.

In the picture you can see there are 4 rows but the image is 2 n half rows. please guide.

HTML 1-4 is in webBrowser, and after space 1-3 is image created from code below.

enter image description here

here is code:

    private void WebBrowser1_LoadCompleted(object sender, NavigationEventArgs e)
    {
        var wb = (WebBrowser)sender;
        var htmlDoc = wb.Document as mshtml.HTMLDocument;
        if (htmlDoc == null || htmlDoc.body == null) return;
        var body = (mshtml.IHTMLElement2)htmlDoc.body;
        wb.Height = body.scrollHeight;
        wb.Width = body.scrollWidth;

        var imgScreen = new Image
        {
            Width = body.scrollWidth,
            Height = body.scrollHeight,
            Source = new DrawingImage(VisualTreeHelper.GetDrawing(wb))
        };

        // Add Image to the UI
        StackPanel1.Children.Add(imgScreen);
    }

Also tried the following code same result:

        DrawingVisual vis = new DrawingVisual();
        DrawingContext cont = vis.RenderOpen();
        cont.DrawDrawing(VisualTreeHelper.GetDrawing(wb));
        cont.Close();

        RenderTargetBitmap rtb = new RenderTargetBitmap(body.scrollWidth, body.scrollHeight, 96d, 96d, PixelFormats.Default);
        rtb.Render(vis);
        imgScreen1.Source = rtb;
ADi
  • 219
  • 1
  • 5
  • 17

1 Answers1

1

The WebBrowser control is really just a wrapper around an ActiveX control, so will not render off-screen in the same way that a regular UIElement would.

MSDN Article

The WebBrowser control internally instantiates the native WebBrowser ActiveX control.

Fortunately, it looks like there is a blog already working around the limitations of this control:

Yishai Galatzer

Matt
  • 2,682
  • 1
  • 17
  • 24