c# 网页截图的实现方法

发布时间:2019-11-20编辑:脚本学堂
c#实现网页截图的方法,有需要的朋友可以参考下。主要实现以下功能:
对指定的页面获取,按指定的大小生成缩略图,也可以1:1的产生图。值得一提的是,页面上的javascript 运行对截图貌似没任何影响。

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;
}
}
}
}

调用示例:
 

复制代码 代码示例:

string UrlPath;
bool CaptureState = false;
Guid guid;
protected bool SaveOriginalPageToImage(Guid myGuid)
{
//使用guid 来命名
guid = myGuid;
if (this.CurrentPageAct == PageAct.Edit)
{
string PagePath = Request.Url.LocalPath;
PagePath = PagePath.Replace("Operation", "Capture");

UrlPath = PagePath + "?act=view&ProjectNo=" + _projectNo;

Thread NewTh = new Thread(CaptureImage);
NewTh.SetApartmentState(ApartmentState.STA);
NewTh.Start();
while (NewTh.ThreadState == ThreadState.Running)
{
}
//返回截取状态
return CaptureState;
}
return false;
}

/**//// <summary>
/// 捕获屏幕
/// </summary>
/// <param name="UrlPath"></param>
/// <returns></returns>
public void CaptureImage()
{
try
{
string url = "http://" + Request.Url.Host + ":" + Request.Url.Port.ToString();
url = url + UrlPath;

GetImage thumb = new GetImage(url, 1024, 1200, 1024, 1200);
System.Drawing.Bitmap x = thumb.GetBitmap();
string FileName = DateTime.Now.ToString("yyyyMMddhhmmss");

x.Save(Server.MapPath("~/Capture/SavePage") + "" + guid + ".jpg");
CaptureState = true;
}
catch (Exception ex)
{
CaptureState = false;
}
}

整个实现过程就是这样了,用c#轻松实现网页截图。
脚本学堂,祝大家学习愉快。