论坛的提问贴,有实用价值:http://topic.csdn.net/u/20071009/10/ee7b6234-2c8b-4eeb-9b13-199f09d22303.html
using System;
using System.Data;
using System.Windows.Forms;
using System.Drawing;
/// <summary>
/// WebSnap :网页抓图对象
/// </summary>
public class WebSnap
{
public WebSnap()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
/// <summary>
/// 开始一个抓图并返回图象
/// </summary>
/// <param name="Url">要抓取的网页地址</param>
/// <returns></returns>
public Bitmap StartSnap(string Url)
{
WebBrowser myWB = this.GetPage(Url);
Bitmap returnValue = this.SnapWeb(myWB);
myWB.Dispose();
return returnValue;
}
private WebBrowser GetPage(string Url)
{
WebBrowser myWB = new WebBrowser();
myWB.ScrollBarsEnabled = false;
myWB.Navigate(Url);
while (myWB.ReadyState != WebBrowserReadyState.Complete)
{
System.Windows.Forms.Application.DoEvents();
}
return myWB;
}
private Bitmap SnapWeb(WebBrowser wb)
{
HtmlDocument hd = wb.Document;
int height = Convert.ToInt32(hd.Body.GetAttribute("scrollHeight")) + 10;
int width = Convert.ToInt32(hd.Body.GetAttribute("scrollWidth")) + 10;
wb.Height = height;
wb.Width = width;
Bitmap bmp = new Bitmap(width, height);
Rectangle rec = new Rectangle();
rec.Width = width;
rec.Height = height;
wb.DrawToBitmap(bmp, rec);
return bmp;
}
}
using System.Data;
using System.Windows.Forms;
using System.Drawing;
/// <summary>
/// WebSnap :网页抓图对象
/// </summary>
public class WebSnap
{
public WebSnap()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
/// <summary>
/// 开始一个抓图并返回图象
/// </summary>
/// <param name="Url">要抓取的网页地址</param>
/// <returns></returns>
public Bitmap StartSnap(string Url)
{
WebBrowser myWB = this.GetPage(Url);
Bitmap returnValue = this.SnapWeb(myWB);
myWB.Dispose();
return returnValue;
}
private WebBrowser GetPage(string Url)
{
WebBrowser myWB = new WebBrowser();
myWB.ScrollBarsEnabled = false;
myWB.Navigate(Url);
while (myWB.ReadyState != WebBrowserReadyState.Complete)
{
System.Windows.Forms.Application.DoEvents();
}
return myWB;
}
private Bitmap SnapWeb(WebBrowser wb)
{
HtmlDocument hd = wb.Document;
int height = Convert.ToInt32(hd.Body.GetAttribute("scrollHeight")) + 10;
int width = Convert.ToInt32(hd.Body.GetAttribute("scrollWidth")) + 10;
wb.Height = height;
wb.Width = width;
Bitmap bmp = new Bitmap(width, height);
Rectangle rec = new Rectangle();
rec.Width = width;
rec.Height = height;
wb.DrawToBitmap(bmp, rec);
return bmp;
}
}
protected void Button1_Click(object sender, EventArgs e)
{
WebSnap ws = new WebSnap();
Bitmap bmp = ws.StartSnap(TextBox1.Text);
System.IO.MemoryStream ms = new System.IO.MemoryStream();
bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
Response.BinaryWrite(ms.GetBuffer());
}
{
WebSnap ws = new WebSnap();
Bitmap bmp = ws.StartSnap(TextBox1.Text);
System.IO.MemoryStream ms = new System.IO.MemoryStream();
bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
Response.BinaryWrite(ms.GetBuffer());
}