This question was moved because it is ASP.NET specific.
To add my two cents, while I was writing this reply I had a look at the code you pointed too. The example that Cat Man Do has provided in theory could work, but there are many pitfalls with it, and it's fairly long winded. For example he creates a new thread, yet syncs it with the original, in theory making DoEvents useless in my book. For a bit of fun i've put a little example together myself, although I should mention that i've tested this only on C# WinForms not ASP.NET so it may need a bit of adaptation;
-
namespace WebBrowserCapture
-
{
-
using System;
-
using System.Drawing;
-
using System.Drawing.Imaging;
-
using System.Linq;
-
using System.Windows.Forms;
-
using System.Collections.Generic;
-
-
public class WebsiteToImage
-
{
-
private WebBrowser Browser;
-
-
public string URL { get; private set; }
-
public List<Bitmap> Output { get; private set; }
-
public event EventHandler Complete;
-
-
public WebsiteToImage(string url)
-
{
-
URL = url;
-
Output = new List<Bitmap>();
-
Browser = new WebBrowser();
-
Browser.ClientSizeChanged += delegate
-
{
-
Output.Clear();
-
};
-
-
Browser.DocumentCompleted += delegate
-
{
-
var r = Browser.Document.Body.ScrollRectangle;
-
var bmp = new Bitmap(r.Width, r.Height);
-
-
this.Browser.ClientSize = new Size(r.Width, r.Height);
-
this.Browser.ScrollBarsEnabled = false;
-
this.Browser.BringToFront();
-
this.Browser.DrawToBitmap(bmp, Browser.Bounds);
-
Output.Add(bmp);
-
-
if (Complete != null) Complete(this, null);
-
};
-
}
-
-
public void Generate()
-
{
-
Output.Clear();
-
if (Browser.ReadyState == WebBrowserReadyState.Loading) return;
-
Browser.Navigate(this.URL);
-
}
-
-
public void SaveAsJpeg(int outputIndex, string filename, long quality = 100)
-
{
-
var parameters = new EncoderParameters(1);
-
var codecs = ImageCodecInfo.GetImageDecoders();
-
var encoder = codecs.First(t => t.FormatID == ImageFormat.Jpeg.Guid);
-
parameters.Param[0] = new EncoderParameter(Encoder.Quality, quality);
-
Output[outputIndex].Save(filename, encoder, parameters);
-
}
-
}
-
}
-
You may notice I have added a list for the Output, I found that the WebBrowser calls DocumentComplete a few times, so having a list allows you to see what was actually captured. To use this class, the following code can be written:
-
WebsiteToImage t = new WebsiteToImage("some-web-address");
-
t.Complete += delegate
-
{
-
t.SaveAsJpeg(0, "some-filename");
-
};
-
t.Generate();
-
Hope this helps in some way.
Aimee