<%@ Page Language="C#"
AspCompat="true"
AutoEventWireup="true"
CodeFile="TestWebPage.aspx.cs"
Inherits="TestWebPage"
%>
DOCTYPE html PUBLIC
"-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Convert Page To Imagetitle>
head>
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="txtUrl"
runat="server"
Width="208px">asp:TextBox><br />
<asp:Button ID="btnConvert"
runat="server"
Text="Convert Page
To Image" OnClick="btnConvert_Click" />
div>
form>
body>
html>
using System;
using
System.Collections;
using
System.Configuration;
using
System.Data;
using
System.Linq;
using
System.Web;
using
System.Web.Security;
using
System.Web.UI;
using
System.Web.UI.HtmlControls;
using
System.Web.UI.WebControls;
using
System.Web.UI.WebControls.WebParts;
using
System.Xml.Linq;
using
System.Drawing;
using
System.Drawing.Imaging;
using
System.Windows.Forms;
public partial class TestWebPage : System.Web.UI.Page
{
protected void
Page_Load(object sender, EventArgs e)
{
}
public System.Drawing.Bitmap CaptureWebPage(string
URL)
{
// create a hidden web browser, which will
navigate to the page
System.Windows.Forms.WebBrowser
web = new System.Windows.Forms.WebBrowser();
// we don't want scrollbars on our image
web.ScrollBarsEnabled = false;
// don't let any errors shine through
web.ScriptErrorsSuppressed = true;
// let's load up that page!
web.Navigate(URL);
// wait until the page is fully loaded
while (web.ReadyState != WebBrowserReadyState.Complete)
System.Windows.Forms.Application.DoEvents();
System.Threading.Thread.Sleep(1500);
// allow time for page scripts to update
// the appearance of the page
// set the size of our web browser to be the same
size as the page
int width =
web.Document.Body.ScrollRectangle.Width;
int height =
web.Document.Body.ScrollRectangle.Height;
web.Width = width;
web.Height = height;
// a bitmap that we will draw to
System.Drawing.Bitmap bmp
= new System.Drawing.Bitmap(width,
height);
// draw the web browser to the bitmap
web.DrawToBitmap(bmp, new
System.Drawing.Rectangle(0, 0, width,
height));
return bmp; //
return the bitmap for processing
}
protected void btnConvert_Click(object
sender, EventArgs e)
{
Bitmap bitmap = new
Bitmap(CaptureWebPage(txtUrl.Text));
Response.ContentType = "image/jpeg";
bitmap.Save(Response.OutputStream, ImageFormat.Jpeg);
Response.AppendHeader("Content-Disposition",
"attachment; filename=page.jpeg");
bitmap.Dispose();
bitmap.Dispose();
Response.End();
}
}
No comments :
Post a Comment