49

I would like to create a function in C# that takes a specific webpage and coverts it to a JPG image from within ASP.NET. I don't want to do this via a third party or thumbnail service as I need the full image. I assume I would need to somehow leverage the webbrowser control from within ASP.NET but I just can't see where to get started. Does anyone have examples?

Keith Adler
  • 20,880
  • 28
  • 119
  • 189
  • 1
    This will be extremely difficult. – SLaks Apr 26 '10 at 17:19
  • 1
    Wow! What a great question. My first reaction is to use the WebBrowser control and use the DrawToBitmap method, but the documentation states, "This method is not supported by this control." Oh, well. – AMissico Apr 26 '10 at 17:20
  • I've found this great link, but the one mystery that it never explains is how to get the darn control to work from ASP.NET. http://www.beansoftware.com/ASP.NET-Tutorials/Get-Web-Site-Thumbnail-Image.aspx – Keith Adler Apr 26 '10 at 17:22
  • 1
    So I guess DrawToBitmap works. I should get points for that! :O) – AMissico Apr 26 '10 at 17:26
  • 1
    What specifically is stopping it from working? – AMissico Apr 26 '10 at 17:31
  • I have tested the code from the article and it works fine. The DrawToBitmap method definitely works. – AMissico Apr 26 '10 at 17:48
  • 2
    @SLaks - Difficult is a mindset. How can you say something is difficult unless you try? – AMissico Apr 26 '10 at 18:05

6 Answers6

49

Ok, this was rather easy when I combined several different solutions:

These solutions gave me a thread-safe way to use the WebBrowser from ASP.NET:

http://www.beansoftware.com/ASP.NET-Tutorials/Get-Web-Site-Thumbnail-Image.aspx

http://www.eggheadcafe.com/tutorials/aspnet/b7cce396-e2b3-42d7-9571-cdc4eb38f3c1/build-a-selfcaching-asp.aspx

This solution gave me a way to convert BMP to JPG:

Bmp to jpg/png in C#

I simply adapted the code and put the following into a .cs:

using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Threading;
using System.Windows.Forms;

public class WebsiteToImage
{
    private Bitmap m_Bitmap;
    private string m_Url;
    private string m_FileName = string.Empty;

    public WebsiteToImage(string url)
    {
        // Without file 
        m_Url = url;
    }

    public WebsiteToImage(string url, string fileName)
    {
        // With file 
        m_Url = url;
        m_FileName = fileName;
    }

    public Bitmap Generate()
    {
        // Thread 
        var m_thread = new Thread(_Generate);
        m_thread.SetApartmentState(ApartmentState.STA);
        m_thread.Start();
        m_thread.Join();
        return m_Bitmap;
    }

    private void _Generate()
    {
        var browser = new WebBrowser { ScrollBarsEnabled = false };
        browser.Navigate(m_Url);
        browser.DocumentCompleted += WebBrowser_DocumentCompleted;

        while (browser.ReadyState != WebBrowserReadyState.Complete)
        {
            Application.DoEvents();
        }

        browser.Dispose();
    }

    private void WebBrowser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
    {
        // Capture 
        var browser = (WebBrowser)sender;
        browser.ClientSize = new Size(browser.Document.Body.ScrollRectangle.Width, browser.Document.Body.ScrollRectangle.Bottom);
        browser.ScrollBarsEnabled = false;
        m_Bitmap = new Bitmap(browser.Document.Body.ScrollRectangle.Width, browser.Document.Body.ScrollRectangle.Bottom);
        browser.BringToFront();
        browser.DrawToBitmap(m_Bitmap, browser.Bounds);

        // Save as file? 
        if (m_FileName.Length > 0)
        {
            // Save 
            m_Bitmap.SaveJPG100(m_FileName);
        }
    }
}

public static class BitmapExtensions
{
    public static void SaveJPG100(this Bitmap bmp, string filename)
    {
        var encoderParameters = new EncoderParameters(1);
        encoderParameters.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, 100L);
        bmp.Save(filename, GetEncoder(ImageFormat.Jpeg), encoderParameters);
    }

    public static void SaveJPG100(this Bitmap bmp, Stream stream)
    {
        var encoderParameters = new EncoderParameters(1);
        encoderParameters.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, 100L);
        bmp.Save(stream, GetEncoder(ImageFormat.Jpeg), encoderParameters);
    }

    public static ImageCodecInfo GetEncoder(ImageFormat format)
    {
        var codecs = ImageCodecInfo.GetImageDecoders();

        foreach (var codec in codecs)
        {
            if (codec.FormatID == format.Guid)
            {
                return codec;
            }
        }

        // Return 
        return null;
    }
}

And can call it as follows:

WebsiteToImage websiteToImage = new WebsiteToImage( "http://www.cnn.com", @"C:\Some Folder\Test.jpg");
websiteToImage.Generate();

It works with both a file and a stream. Make sure you add a reference to System.Windows.Forms to your ASP.NET project. I hope this helps.

UPDATE: I've updated the code to include the ability to capture the full page and not require any special settings to capture only a part of it.

Community
  • 1
  • 1
Keith Adler
  • 20,880
  • 28
  • 119
  • 189
  • 1
    @SLaks - Why? What specifically is your concern? Using the WebBrowser control is not so different from charting controls that generate an image for display. I do not necessarily agree that threading should be used. I probably would use Control.Invoke and let the control deal with it. – AMissico Apr 26 '10 at 18:16
  • 1
    @AMissico: I don't think that WinForms controls work reliably in a non-interactive session. I might be wrong, though. – SLaks Apr 26 '10 at 18:29
  • @Amissico From the first article: First, being a Windows Form control, this must operate on an STA (Single Threaded Apartment) thread. This means you need to either set the AspCompat = "true" attribute on the page that uses it, or you need to make the actual Webbrowser Navigate call to the target page on a secondary thread whose state has been set to STA. I choose the latter. – Keith Adler Apr 26 '10 at 18:32
  • @Nissan Fan - Okay, I understand now. Yet, why did the code work for me unmodified? Even with AspCompat="true|false" it still worked. (I was using VS2k8 with local ASP.NET Development Server.) – AMissico Apr 26 '10 at 18:50
  • Because in the code, the thread is launced as a STA thread thus it solves the problem. – Keith Adler Apr 26 '10 at 19:44
  • This may work better on the server if the thumbnailing processing is done from a windows service, That may provide some more "interactvity"? – Mark Redman Apr 26 '10 at 21:12
  • I'm not using it for thumbnails, though it could be. – Keith Adler Apr 26 '10 at 21:17
  • Wow, great work done in 9 minuttes (time between the question and the answer). – Espo Apr 27 '10 at 14:57
  • Thanks Espo. I didn't have a lot of time to invest in this and I managed to get a real good start from a search I did after posting. – Keith Adler Apr 27 '10 at 16:47
  • is private string m_FileName = string.Empty; where to put file in wich will my image be saved? – r.r Feb 03 '11 at 10:54
  • I'm having difficulty passing in parameters into the webbrowser control. Thinking says it should be possible to do 'code'(var browser = new WebBrowser { ScrollBarsEnabled = false }; browser.Navigate("http://someurl.com?params=x");) I've also tried msdn's [link](http://msdn.microsoft.com/en-us/library/ms161355.aspx) recommendations for passing postData. Don't get why its in byte[] format and how to retrieve the data from within my conttrol's page. Thanks – bizl Mar 24 '11 at 00:03
  • @CatManDo The thing work perfect but i am having issue with speed its currently too slow, maybe takes 40-50 sec to save a page i am running it on group on deals. – confusedMind May 25 '12 at 13:43
  • This mostly works for me - except I get a bunch of blank areas in my image, which seem to correspond to sections that are rendered using javascript/after document.ready. Is there a way to get that segment to render as well? – Elie Dec 28 '12 at 01:47
  • Very nice. Any way to render the image without writing to a file? – Hell.Bent Apr 24 '13 at 12:38
  • @o365spo Yes simply call the method with a Stream parameter instead of a filename. – Keith Adler May 11 '16 at 05:50
  • This does not work on an IIS server. It's very slow and it gives me a blank JPG file. It is also missing some CCS images when running locally. – Steve Coleman Aug 17 '16 at 21:48
  • Why is the `Application.DoEvents();` required? – BornToCode Aug 22 '16 at 09:30
  • 1
    @BornToCode not technically needed, but let's your app free up event cycles for other things. – Keith Adler Aug 27 '16 at 23:30
  • The WebBrowser control has a known memory leak discussed on many posts here on SO, so this approach might be a bit problematic if you use it on a server which suppose to capture hundreds of pages, don't you think so? – BornToCode Aug 29 '16 at 13:51
  • will not compile: The type or namespace name 'WebBrowser' could not be found, WebBrowserDocumentCompletedEventArgs could not be found. – Steve Staple Apr 05 '17 at 15:12
  • @SteveStaple Just add a reference to System.Windows.Forms. – Keith Adler Apr 06 '17 at 06:59
  • @KeithAdler I am trying to convert a page that contains Google Charts and this does not convert to jpg. Is it possible that svg is not supported? Thanks. – Mitesh Budhabhatti Jul 15 '17 at 08:42
4

Good solution by Mr Cat Man Do.

I've needed to add a row to suppress some errors that came up in some webpages (with the help of an awesome colleague of mine)

private void _Generate()
{
    var browser = new WebBrowser { ScrollBarsEnabled = false };

    browser.ScriptErrorsSuppressed = true;        //           <--

    browser.Navigate(m_Url);
    browser.DocumentCompleted += WebBrowser_DocumentCompleted;
}

...

Thanks Mr Do

Fourier
  • 323
  • 2
  • 6
2

Here is my implementation using extension methods and task factory instead thread:

/// <summary>
    /// Convert url to bitmap byte array
    /// </summary>
    /// <param name="url">Url to browse</param>
    /// <param name="width">width of page (if page contains frame, you need to pass this params)</param>
    /// <param name="height">heigth of page (if page contains frame, you need to pass this params)</param>
    /// <param name="htmlToManipulate">function to manipulate dom</param>
    /// <param name="timeout">in milliseconds, how long can you wait for page response?</param>
    /// <returns>bitmap byte[]</returns>
    /// <example>
    /// byte[] img = new Uri("http://www.uol.com.br").ToImage();
    /// </example>
    public static byte[] ToImage(this Uri url, int? width = null, int? height = null, Action<HtmlDocument> htmlToManipulate = null, int timeout = -1)
    {
        byte[] toReturn = null;

        Task tsk = Task.Factory.StartNew(() =>
        {
            WebBrowser browser = new WebBrowser() { ScrollBarsEnabled = false };
            browser.Navigate(url);

            browser.DocumentCompleted += (s, e) =>
            {
                var browserSender = (WebBrowser)s;

                if (browserSender.ReadyState == WebBrowserReadyState.Complete)
                {
                    if (htmlToManipulate != null) htmlToManipulate(browserSender.Document);

                    browserSender.ClientSize = new Size(width ?? browser.Document.Body.ScrollRectangle.Width, height ?? browser.Document.Body.ScrollRectangle.Bottom);
                    browserSender.ScrollBarsEnabled = false;
                    browserSender.BringToFront();

                    using (Bitmap bmp = new Bitmap(browserSender.Document.Body.ScrollRectangle.Width, browserSender.Document.Body.ScrollRectangle.Bottom))
                    {
                        browserSender.DrawToBitmap(bmp, browserSender.Bounds);
                        toReturn = (byte[])new ImageConverter().ConvertTo(bmp, typeof(byte[]));
                    }
                }

            };

            while (browser.ReadyState != WebBrowserReadyState.Complete)
            {
                Application.DoEvents();
            }

            browser.Dispose();

        }, CancellationToken.None, TaskCreationOptions.None, TaskScheduler.FromCurrentSynchronizationContext());

        tsk.Wait(timeout);

        return toReturn;
    }
Alexandre
  • 7,004
  • 5
  • 54
  • 72
  • +1 for using tasks...not wild about the use of ext method though. Also, why not pass Size, TimeSpan, etc? Those structs are there for a reason... – Jeff Mar 25 '14 at 02:06
1

The solution is perfect, just needs a fixation in the line which sets the WIDTH of the image. For pages with a LARGE HEIGHT, it does not set the WIDTH appropriately:

    //browser.ClientSize = new Size(browser.Document.Body.ScrollRectangle.Width, browser.Document.Body.ScrollRectangle.Bottom);
    browser.ClientSize = new Size(1000, browser.Document.Body.ScrollRectangle.Bottom);

And for adding a reference to System.Windows.Forms, you should do it in .NET-tab of ADD REFERENCE instead of COM -tab.

Kardo
  • 1,658
  • 4
  • 32
  • 52
1

There is a good article by Peter Bromberg on this subject here. His solution seems to do what you need...

Dean Kuga
  • 11,878
  • 8
  • 54
  • 108
0

You could use WatiN to open a new browser, then capture the screen and crop it appropriately.

Yannick Blondeau
  • 9,465
  • 8
  • 52
  • 74
Ian P
  • 12,840
  • 6
  • 48
  • 70
  • This will require staring a real browser in it's own process. Not really a solution that scales well. – Marcel Jan 10 '14 at 14:02