0

Is there any way to take a snapshot of rendered html, i.e from webBrowser control and make an image object of the output?

is it possible by code besides using webbrowser, bcoz i think it needs to be visible on screen before the source is render as output.

i have html string which contains some picture text and table. of height of almost 700px, could be less or more on other scenario.

please guide me on this.

ADi
  • 219
  • 1
  • 5
  • 17
  • Just to clarify, you want to make an image of something without having to render a visual of it? – Gayot Fow Jan 07 '14 at 15:31
  • basically i just need an image for the html's output, whatever means, my guess was to take snapshot of `webBrowser` and remove the `webBrowser` and add the image. i just don't know if possible and if yes how. but other than this add remove `webBrowser` approach is much appreciated – ADi Jan 07 '14 at 15:36

1 Answers1

0

Use a RenderTargetBitmap instance. This allows you to render a control without displaying it on screen. Be careful to give it a width and height ;)

http://msdn.microsoft.com/en-us/library/system.windows.media.imaging.rendertargetbitmap%28v=vs.110%29.aspx

Sample from MSDN

using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Globalization;

namespace SDKSample
{
    public partial class RenderTargetBitmapExample : Page
    {
        public RenderTargetBitmapExample()
        {

        Image myImage = new Image();
        FormattedText text = new FormattedText("ABC",
                new CultureInfo("en-us"),
                FlowDirection.LeftToRight,
                new Typeface(this.FontFamily, FontStyles.Normal, FontWeights.Normal, new FontStretch()),
                this.FontSize,
                this.Foreground);

        DrawingVisual drawingVisual = new DrawingVisual();
        DrawingContext drawingContext = drawingVisual.RenderOpen();
        drawingContext.DrawText(text, new Point(2, 2));
        drawingContext.Close();

        RenderTargetBitmap bmp = new RenderTargetBitmap(180, 180, 120, 96, PixelFormats.Pbgra32);
        bmp.Render(drawingVisual);
        myImage.Source = bmp;

        // Add Image to the UI
        StackPanel myStackPanel = new StackPanel();
        myStackPanel.Children.Add(myImage);
        this.Content = myStackPanel;
        }
    }
}
Gusdor
  • 14,001
  • 2
  • 52
  • 64
  • i am trying but don't quite understand what code to change with drawingContext.DrawText(text, new Point(2, 2)); to, i have `wb` object of `webBrowswe` can u plz exlplain on it – ADi Jan 07 '14 at 16:16
  • This is a working example but really all your need is the code starting at `RenderTargetBitmap bmp =` up to `bmp.Render(...`. Replace `drawingVisual` with _any visual at all_, including webbrowser. – Gusdor Jan 07 '14 at 16:28
  • got it working but its giving strange output, it only shows 200px height of image while the actual is 700px. well looking into it. thanks – ADi Jan 07 '14 at 17:03
  • @Bsienn that's bizarre. How big are you making the `RenderTargetBitmap`? – Gusdor Jan 08 '14 at 08:02
  • here is the code i tried and image in this question. plz chk http://stackoverflow.com/questions/20978618/visualtreehelper-getdrawing-not-returning-complete-image-wpf – ADi Jan 08 '14 at 08:25