c#获取网页截图的多种方式

发布时间:2019-11-16编辑:脚本学堂
c#网页截图功能的实现代码,利用WebBrowser进行截图,一个截图类GetSnap,可以在web和winform上进行截图,C#对指定的网页进行截图,获取指定网页并保存为图片。

一、c#获取浏览器网页截图
 

复制代码 代码示例:
private void Form_Load(object sender, EventArgs e)
{
  //接收web url
  string colle = string.Empty;
  string url = string.Empty;
  //获取进程调用传入的命令
  string[] args = Environment.GetCommandLineArgs();
  string[] args = new string[] { @"E:MicroSpaceMicroSpacelocalpageDebugGetCutImage.exe", "-u:http://dgjs123.com/template/amo.html","-n:E:TESTtest.jpg" };
  for (int i = 1; i < args.Length; i++)
  {
 switch (args[i].Substring(0, 3))
 {
case "-u:":
 url = args[i].Substring(3);
 break;
case "-n:":
 imagename = args[i].Substring(3);
 break;
default:
 colle = args[i];
 break;
 }
  }
  WebBrowser webBrowser = new WebBrowser(); // 创建一个WebBrowser
  webBrowser.ScrollBarsEnabled = false; // 隐藏滚动条
  webBrowser.Navigate(url); // 打开网页
  webBrowser.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(webBrowser_DocumentCompleted); // 增加网页加载完成事件处理函数
}
private void webBrowser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
  WebBrowser webBrowser = (WebBrowser)sender;
  // 网页加载完毕才保存
  if (webBrowser.ReadyState == WebBrowserReadyState.Complete)
  {
 bool save_suc = true;
 try
 {
// 获取网页高度和宽度
int height = webBrowser.Document.Body.ScrollRectangle.Height;
int width = webBrowser.Document.Body.ScrollRectangle.Width;
 
//截图宽度、高度
int imgwidth = width - 2 * 60;
int imgheight = height - 48;
 
// 调节webBrowser的高度和宽度
webBrowser.Height = height;
webBrowser.Width = width;
 
Bitmap bitmap = new Bitmap(width, height); // 创建高度和宽度与网页相同的图片
Rectangle rectangle = new Rectangle(0, 0, width, height); // 绘图区域
//Rectangle mm=new Rectangle(
webBrowser.DrawToBitmap(bitmap, rectangle); // 绘制浏览器图片至bitmap
 
int bmpw = bitmap.Width;
int bmph = bitmap.Height;
 
System.Drawing.Image Imag = new System.Drawing.Bitmap(imgwidth, imgheight);//创建Image
System.Drawing.Graphics gp = System.Drawing.Graphics.FromImage(Imag);//获取Image的Graphics
 
//通过Graphics绘图实现截图效果
gp.DrawImage(bitmap,new Rectangle(0,0,imgwidth,imgheight),new Rectangle(60,48,imgwidth,imgheight),GraphicsUnit.Pixel);
//保存
Imag.Save(imagename, System.Drawing.Imaging.ImageFormat.Jpeg);
//bitmap.Save(imagename, System.Drawing.Imaging.ImageFormat.Jpeg);
bitmap.Dispose();
gp.Dispose();
Imag.Dispose();
 }
 catch (Exception ex){
 }
 finally
 {
this.Dispose(true);
System.Diagnostics.Process.GetCurrentProcess().Kill();
this.Close();
Application.Exit();
 }
  }
}
//图片保存方式
//imagename 图片路径
bitmap.Save(imagename, System.Drawing.Imaging.ImageFormat.Jpeg);
Image.Save(imagename, System.Drawing.Imaging.ImageFormat.Jpeg);

二、C#网页截图代码

利用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.dgjs123.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)
{
}
}

三、C#对指定的网页进行截图

可以实现对指定的页面获取,按指定的大小生成缩略图,当然也可以1:1的产生图,页面上的javascript 运行对截图。

首先,添加系统引用
 

System.Drawing;
System.Drawing.Design;
System.Windows.Forms;

获取指定网页并转换成图片的类
 

复制代码 代码示例:
using System;
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;
namespace MyLib 
{
public class GetImage
{
private int S_Height;
private int S_Width;
private int F_Height;
private int F_Width;
private string MyURL;
public int ScreenHeight 
{
get { return S_Height; }
set { S_Height = value; }
}
public int ScreenWidth 
{
get { return S_Width; }
set { S_Width = value; }
}
public int ImageHeight 
{
get { return F_Height; }
set { F_Height = value; }
}
public int ImageWidth 
{
get { return F_Width; }
set { F_Width = value; }
}
public string WebSite
{
get { return MyURL; }
set { MyURL = value; }
}
public GetImage(string WebSite, int ScreenWidth, int ScreenHeight, int ImageWidth, int ImageHeight)
{
this.WebSite = WebSite;
this.ScreenWidth = ScreenWidth;
this.ScreenHeight = ScreenHeight;
this.ImageHeight = ImageHeight;
this.ImageWidth = ImageWidth;
}
public Bitmap GetBitmap()
{
WebPageBitmap Shot = new WebPageBitmap(this.WebSite, this.ScreenWidth, this.ScreenHeight);
Shot.GetIt();
Bitmap Pic = Shot.DrawBitmap(this.ImageHeight, this.ImageWidth);
return Pic;
}
}
class WebPageBitmap 
{
WebBrowser MyBrowser;
string URL;
int Height;
int Width;
public WebPageBitmap(string url, int width, int height)
{
this.Height = height;
this.Width = width;
this.URL = url;
MyBrowser = new WebBrowser();
MyBrowser.ScrollBarsEnabled = false;
MyBrowser.Size = new Size(this.Width, this.Height);
}
public void GetIt()
{
MyBrowser.Navigate(this.URL);
while (MyBrowser.ReadyState != WebBrowserReadyState.Complete)
{
Application.DoEvents();
}
}
public Bitmap DrawBitmap(int theight, int twidth)
{
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;
}
}
}
}