3

is there a way to render ViewResult or PartialViewResult as an image?

I have tried to get ViewResult as string and I got a string containing html as it should be, but I need to render that html to image. If it is possible - with styles and images.

I have an idea to get some browser output for this html on server and capture result to an image, but how it can be done in practice I don't know at this time. If you have any ideas, please, help.

Please do not suggest any third party components. Just tell if it is impossible to do by standart .NET classes.

Thank you

2 Answers 2

8

See How to render an image using the WebBrowser control

public class HtmlToBitmapConverter 
{ 
    private const int SleepTimeMiliseconds = 5000; 

    public Bitmap Render(string html, Size size) 
    { 
        var browser = CreateBrowser(size); 

        browser.Navigate("about:blank"); 
        browser.Document.Write(html); 

        return GetBitmapFromControl(browser, size); 
    } 

    public Bitmap Render(Uri uri, Size size) 
    { 
        var browser = CreateBrowser(size); 

        NavigateAndWaitForLoad(browser, uri, 0); 

        return GetBitmapFromControl(browser, size); 
    } 

    public void NavigateAndWaitForLoad(WebBrowser browser, Uri uri, int waitTime) 
    { 
        browser.Navigate(uri); 
        var count = 0; 

        while (browser.ReadyState != WebBrowserReadyState.Complete) 
        { 
            Thread.Sleep(SleepTimeMiliseconds); 

            Application.DoEvents(); 
            count++; 

            if (count > waitTime / SleepTimeMiliseconds) 
            { 
                break; 
            } 
        } 
    } 

    private WebBrowser CreateBrowser(Size size) 
    { 
        return 
            new WebBrowser 
            { 
                ScrollBarsEnabled = false, 
                ScriptErrorsSuppressed = true, 
                Size = size 
            }; 
    } 

    private Bitmap GetBitmapFromControl(WebBrowser browser, Size size) 
    { 
        var bitmap = new Bitmap(size.Width, size.Height); 

        NativeMethods.GetImage(browser.Document.DomDocument, bitmap, Color.White); 
        return bitmap; 
    } 
}

Then it's as easy as

var image = new HtmlToBitmapConverter()
                .Render(new Uri(urlTextBox.Text), pictureBox.Size);
0

Sorry for my previous suggestion. It was not correct, really. Now I understand what you want. But if you don't like the idea to use the third-party solutions the only way is to write your own parser and it's not easy task. Or you could try to use WebBrowser class and its method DrawToBitmap how it's described here http://englestone.blogspot.com/2009/06/generate-screenshot-previews-with-c.html

2
  • That's not correct, he's not asking to download an image. He wants to render the web page as an image, then download it. The downloading is easy, the rendering less so. Dec 21, 2011 at 20:29
  • Oh, really? I still don't understand. You mean the task is return of the image for example if view has tables the image should include tables?
    – Kath
    Dec 21, 2011 at 21:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.