文档参考:
https://www.cnblogs.com/zuqing/p/5698060.html
在此基础上对其进行了部分优化,增加了超时处理、偏移量、不显示脚本弹窗处理
项目引用程序集:
Microsoft.mshtml
System.Windows.Forms.dll
创建WebSnapshotsHelper类
using System;
using System.Drawing;
using System.Threading;
using System.Windows.Forms;
namespace WebSnapshots
{
public class WebSnapshotsHelper
{
Bitmap m_Bitmap;
string m_Url;
int m_BrowserWidth, m_BrowserHeight, m_SnapWidth, m_SnapHeight;
int m_OffsetX, m_OffsetY;
int m_Timeout;
private WebSnapshotsHelper(string Url, int BrowserWidth, int BrowserHeight, int SnapWidth, int SnapHeight, int OffsetX, int OffsetY, int Timeout)
{
m_Url = Url;
m_BrowserHeight = BrowserHeight;
m_BrowserWidth = BrowserWidth;
m_SnapWidth = SnapWidth;
m_SnapHeight = SnapHeight;
m_OffsetX = OffsetX;
m_OffsetY = OffsetY;
m_Timeout = Timeout;
}
/// <summary>
/// GetWebSiteThumbnail
/// </summary>
/// <param name="Url">目标网络地址</param>
/// <param name="BrowserWidth">浏览器宽度</param>
/// <param name="BrowserHeight">浏览器高度</param>
/// <param name="SnapWidth">截图的宽度</param>
/// <param name="SnapHeight">截图的高度</param>
/// <param name="OffsetX">截图偏移量,从左边X处开始截图</param>
/// <param name="OffsetY">截图偏移量,从上边Y处开始截图</param>
/// <param name="Timeout">超过N秒后即超时,Bitmap返回null</param>
/// SnapWidth+OffsetX不能大于SnapWidth,SnapHeight+OffsetY不能大于SnapHeight
/// <returns></returns>
public static Bitmap GetWebSiteThumbnail(string Url, int BrowserWidth, int BrowserHeight, int SnapWidth, int SnapHeight, int OffsetX=0,int OffsetY=0,int Timeout=60)
{
WebSnapshotsHelper thumbnailGenerator = new WebSnapshotsHelper(Url, BrowserWidth, BrowserHeight, SnapWidth, SnapHeight, OffsetX, OffsetY, Timeout);
return thumbnailGenerator.GenerateWebSiteThumbnailImage();
}
private Bitmap GenerateWebSiteThumbnailImage()
{
Thread m_thread = new Thread(new ThreadStart(_GenerateWebSiteSnapImage));
m_thread.SetApartmentState(ApartmentState.STA);
m_thread.Start();
m_thread.Join();
return m_Bitmap;
}
private void _GenerateWebSiteSnapImage()
{
WebBrowser m_WebBrowser = new WebBrowser();
m_WebBrowser.ScrollBarsEnabled = false;
//获取或设置一个值,该值指示 WebBrowser 是否显示对话框,如脚本错误消息。
//如果控件不显示其对话框,则为 true;否则为 false。 默认值为 false。
m_WebBrowser.ScriptErrorsSuppressed = true;
m_WebBrowser.Navigate(m_Url);
m_WebBrowser.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(WebBrowser_DocumentCompleted);
DateTime dateFrom = DateTime.Now;
DateTime dateTo;
while (m_WebBrowser.ReadyState != WebBrowserReadyState.Complete || m_WebBrowser.IsBusy)
{
dateTo = DateTime.Now;
if ((dateTo - dateFrom).TotalSeconds < m_Timeout)
{
Application.DoEvents();
}
else
{
break;
}
}
m_WebBrowser.Dispose();
}
private void WebBrowser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
WebBrowser m_WebBrowser = (WebBrowser)sender;
m_WebBrowser.ClientSize = new Size(this.m_BrowserWidth, this.m_BrowserHeight);
m_WebBrowser.ScrollBarsEnabled = false;
m_Bitmap = new Bitmap(m_BrowserWidth, m_BrowserHeight);
m_WebBrowser.BringToFront();
m_WebBrowser.DrawToBitmap(m_Bitmap, m_WebBrowser.Bounds);
Rectangle rClipRect = new Rectangle(m_OffsetX, m_OffsetY, m_SnapWidth, m_SnapHeight);
m_Bitmap = m_Bitmap.Clone(rClipRect, m_Bitmap.PixelFormat);
m_Bitmap = (Bitmap)m_Bitmap.GetThumbnailImage(m_SnapWidth, m_SnapHeight, null, IntPtr.Zero);
}
}
}
测试用例:
url = "http://www.cnblogs.com/";
Bitmap m_Bitmap = WebSnapshotsHelper.GetWebSiteThumbnail(url, 1920, 1080, 1000, 580, 0, 0, 60); //宽高根据要获取快照的网页决定
m_Bitmap.Save("snap_image.jpg", System.Drawing.Imaging.ImageFormat.Jpeg); //图片格式可以自由控制
m_Bitmap.Dispose();
System.Diagnostics.Process.Start("snap_image.jpg");