许多人都在想如何进行网页截图,其实简单的方法是利用WebBrowser进行截图,下面提供一个截图类GetSnap,可以在web和winform上进行截图。
类GetSnap:
======================================
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.Windows.Forms;
using System.Diagnostics;
/// <summary>
/// GetSnap 的摘要说明
/// </summary>
public class GetSnap
{
private string MyURL;
public string WebSite
{
get { return MyURL; }
set { MyURL = value; }
}
public GetSnap(string WebSite/*, int ScreenWidth, int ScreenHeight, int ImageWidth, int ImageHeight*/)
{
this.WebSite = WebSite;
}
public Bitmap GetBitmap()
{
WebPageBitmap Shot = new WebPageBitmap(this.WebSite/*, this.ScreenWidth, this.ScreenHeight*/);
Shot.GetIt();
//Bitmap Pic = Shot.DrawBitmap(this.ImageHeight, this.ImageWidth);
Bitmap Pic = Shot.DrawBitmap();
return Pic;
}
}
class WebPageBitmap
{
WebBrowser MyBrowser;
string URL;
int Height;
int Width;
public WebPageBitmap(string url/*, int width, int height*/)
{
this.URL = url;
MyBrowser = new WebBrowser();
MyBrowser.ScrollBarsEnabled = false;
}
public void GetIt()
{
MyBrowser.Navigate(this.URL);
while (MyBrowser.ReadyState != WebBrowserReadyState.Complete)
{
Application.DoEvents();
}
this.Height = int.Parse(MyBrowser.Document.Body.GetAttribute("scrollHeight"));
this.Width = int.Parse(MyBrowser.Document.Body.GetAttribute("scrollwidth"));
MyBrowser.Size = new Size(this.Width, this.Height);
}
public Bitmap DrawBitmap(/*int theight, int twidth*/)
{
int theight=this.Height;
int twidth = this.Width;
Bitmap myBitmap = new Bitmap(Width, Height);
Rectangle DrawRect = new Rectangle(0, 0, Width, Height);
MyBrowser.DrawToBitmap(myBitmap, DrawRect);
System.Drawing.Image imgOutput = myBitmap;
System.Drawing.Image oThumbNail = new Bitmap(twidth, theight, imgOutput.PixelFormat);
Graphics g = Graphics.FromImage(oThumbNail);
g.CompositingQuality = CompositingQuality.HighSpeed;
g.SmoothingMode = SmoothingMode.HighSpeed;
g.InterpolationMode = InterpolationMode.HighQualityBilinear;
Rectangle oRectangle = new Rectangle(0, 0, twidth, theight);
g.DrawImage(imgOutput, oRectangle);
try
{
return (Bitmap)oThumbNail;
}
catch (Exception ex)
{
return null;
}
finally
{
imgOutput.Dispose();
imgOutput = null;
MyBrowser.Dispose();
MyBrowser = null;
}
}
}
===========================================
在web中截图:
新建test.aspx页面,引入命名空间using System.Drawing.Imaging;using System.Threading;
添加button1事件:
protected void Button1_Click(object sender, EventArgs e)
{
Thread NewTh = new Thread(CaptureImage);
NewTh.SetApartmentState(ApartmentState.STA);//必须启动单元线程
NewTh.Start();
}
添加回调函数:
///// 捕获屏幕
public void CaptureImage()
{
//你的任务代码
try
{
string url=http://www.baidu.com;
GetSnap thumb = new GetSnap(url);
System.Drawing.Bitmap x = thumb.GetBitmap();//获取截图
string FileName = DateTime.Now.ToString("yyyyMMddhhmmss");
x.Save(Server.MapPath("SnapPic/" + FileName + ".jpg"));//保存截图到SnapPic目录下
}
catch (Exception ex)
{
}
}