Creating web page screenshots from an ASP.net page

S

Stephane

Hi,

I'm creating screenshots of web pages using the following code. It uses the
WebBrowser object.

/***********************************************/

private Bitmap createScreenshot(int pId, string pUrl)
{
// Generate thumbnail of a webpage
Bitmap thumbnail = GenerateScreenshot(pUrl);

// resize to 248 x 225
int tWidth = thumbnail.Width;
int tHeight = thumbnail.Height;
int tRatio = tWidth * 100 / 216;
int newHeight = tHeight * 100 / tRatio;

Bitmap result = (Bitmap)thumbnail.GetThumbnailImage(216,
newHeight, null, IntPtr.Zero);

thumbnail.Dispose();
return result;
}

public Bitmap GenerateScreenshot(string url)
{
// This method gets a screenshot of the webpage
// rendered at its full size (height and width)

return GenerateScreenshot(url, 1024, 1070);

}

public Bitmap GenerateScreenshot(string url, int width, int height)
{
DateTime timeStamp = DateTime.Now;
// Load the webpage into a WebBrowser control
WebBrowser wb = new WebBrowser();
wb.ScrollBarsEnabled = false;
wb.ScriptErrorsSuppressed = true;
wb.Navigate(url);

WebBrowserReadyState flag = wb.ReadyState;

while (flag != WebBrowserReadyState.Complete && DateTime.Now <
timeStamp.AddSeconds(10))
{
//Thread.Sleep(1000);
System.Windows.Forms.Application.DoEvents();
flag = wb.ReadyState;

TimeSpan elapsed = DateTime.Now.Subtract(timeStamp);
if (elapsed.TotalMilliseconds > TIMEOUT)
{
wb.Dispose();
// Response.Write("Damn " + url + "<br>");
return null;
}
}

// Set the size of the WebBrowser control
wb.Width = width;
wb.Height = height;

if (width == -1)
{
// Take Screenshot of the web pages full width
wb.Width = wb.Document.Body.ScrollRectangle.Width;
}

if (height == -1)
{
// Take Screenshot of the web pages full height
wb.Height = wb.Document.Body.ScrollRectangle.Height;
}

// Get a Bitmap representation of the webpage as it's rendered
in the WebBrowser control
Bitmap bitmap = new Bitmap(wb.Width, wb.Height);
wb.DrawToBitmap(bitmap, new Rectangle(0, 0, wb.Width, wb.Height));

Graphics Grfx = Graphics.FromImage(bitmap);
Grfx.InterpolationMode =
System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
Grfx.SmoothingMode =
System.Drawing.Drawing2D.SmoothingMode.HighQuality;
Grfx.PixelOffsetMode =
System.Drawing.Drawing2D.PixelOffsetMode.HighQuality;
Grfx.CompositingQuality =
System.Drawing.Drawing2D.CompositingQuality.HighQuality;
Grfx.DrawImage(bitmap, 0, 0, wb.Width, wb.Height);

wb.Dispose();

return bitmap;
}

/***********************************************/

While it's no so bad, it's far to be perfect. Like flash objects are not
printed so are some images in background displayed with CSS. And sometimes, I
got an infinite loop because the web page called has a javascript « alert »
or something like that.

I'd like to get screenshots as if I took them myself using « ctrl-print
screen ». Does anyone know a really good way or tool to create screenshots on
the fly from an ASP.Net page?

By the way, I'm on IIS 6 using .Net 2.0

Thanks for any help

Stephane
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Staff online

Members online

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,040
Latest member
papereejit

Latest Threads

Top