using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Windows.Forms;
using System.IO;
namespace WindowsFormsApplication1
{
public class WebCapture
{
#region 字段、属性
private int width;
private int height;
private string url;
/// <summary>
/// 属性:图片宽
/// </summary>
public int Width { get { return width; } }
/// <summary>
/// 属性:图片高
/// </summary>
public int Height { get { return height; } }
/// <summary>
/// 属性:Url
/// </summary>
public string Url { get { return url; } }
#endregion
#region 构造函数
/// <summary>
/// 构造函数
/// </summary>
public WebCapture()
{
this.width = 0;
this.height = 0;
this.url = "http://www.mzwu.com/";
}
/// <summary>
/// 构造函数
/// </summary>
/// <param name="url">Url</param>
public WebCapture(string url)
{
this.width = 0;
this.height = 0;
this.url = url;
}
/// <summary>
/// 构造函数
/// </summary>
/// <param name="width">图片宽</param>
/// <param name="height">图片高</param>
/// <param name="url">Url</param>
public WebCapture(int width,int height,string url)
{
this.width = width;
this.height = height;
this.url = url;
}
#endregion
/// <summary>
/// 保存图片
/// </summary>
/// <param name="destpath">保存路径</param>
/// <returns></returns>
public void Save(string destpath)
{
Save(this.url, destpath, this.width, this.height);
}
/// <summary>
/// 保存图片
/// </summary>
/// <param name="url">Url</param>
/// <param name="destpath">保存路径</param>
/// <returns></returns>
public void Save(string url, string destpath)
{
Save(url, destpath, this.width, this.height);
}
/// <summary>
/// 保存图片
/// </summary>
/// <param name="url">Url</param>
/// <param name="destpath">保存路径</param>
/// <param name="width">图片宽</param>
/// <param name="height">图片高</param>
/// <returns></returns>
/// <remarks>width,height为0时获取完整大小</remarks>
public void Save(string url, string destpath, int width, int height)
{
//变量
int scrollWidth, scrollHeight;
Bitmap bitmap;
ImageFormat picType;
//图片格式
switch (Path.GetExtension(destpath).ToLower())
{
case ".gif":
picType = System.Drawing.Imaging.ImageFormat.Gif;
break;
case ".jpg":
case ".jpeg":
picType = System.Drawing.Imaging.ImageFormat.Jpeg;
break;
case ".png":
picType = System.Drawing.Imaging.ImageFormat.Png;
break;
case ".bmp":
picType = System.Drawing.Imaging.ImageFormat.Bmp;
break;
default:
picType = System.Drawing.Imaging.ImageFormat.Jpeg;
break;
}
//抓取图片
try
{
WebBrowser browser = new WebBrowser();
browser.ScrollBarsEnabled = false;
browser.DocumentCompleted += delegate(object sender, WebBrowserDocumentCompletedEventArgs e)
{
WebBrowser browser2 = (WebBrowser)sender;
if (browser2.ReadyState == WebBrowserReadyState.Interactive)
{
scrollWidth = browser2.Document.Body.ScrollRectangle.Width;
scrollHeight = browser2.Document.Body.ScrollRectangle.Height;
if (width == 0) width = scrollWidth;
if (height == 0) height = scrollHeight;
//核心语句
bitmap = new Bitmap(width, height);
browser2.Size = new Size(width, height);
//部分站点截到的图为空白,MSDN中注明:
//此 API 支持 .NET Framework 基础结构,不适合在代码中直接使用。此控件不支持此方法。
//http://msdn.microsoft.com/zh-cn/library/system.windows.forms.webbrowserbase.drawtobitmap.aspx
browser2.DrawToBitmap(bitmap, new Rectangle(0, 0, width, height));
bitmap.Save(destpath, picType);
MessageBox.Show(browser2.Document.Body.InnerHtml);
bitmap.Dispose();
browser2.Dispose();
}
};
browser.Navigate(url);
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
}
}
评论(0)
暂无评论。