第一步 新建一个aspx页面
<div>
网址<asp:TextBox ID="TextBox1" runat="server" Width="427px">http://</asp:TextBox>
<asp:Button ID="bt" runat="server" OnClick="bt_Click" Text="抓取" /><br />
<br />
<div id="Preview" runat="server"></div>
</div>
aspx.cs 页面引用
using System.Windows.Forms;
using System.Drawing;
using System.Threading;
以下是.cs 页面的代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using System.Drawing;
using System.Threading;
using System.Drawing.Imaging;
using System.Runtime.InteropServices.ComTypes;
using System.IO;
using System.Drawing;
private void GetUrlImg()
{
System.Drawing.Image img = GetImage(TextBox1.Text);
string fold = "imagessctp/";
string physicsFold = Server.MapPath(fold);
img.Save(physicsFold +"照片1.jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
}
public static System.Drawing.Image GetImage(string webSite)
{
WebPageBitmap Shot = new WebPageBitmap(webSite);
Shot.GetIt();
System.Drawing.Image Pic = Shot.DrawImage();
return Pic;
}
protected void bt_Click(object sender, EventArgs e)
{
Thread NewTh = new Thread(GetUrlImg);
NewTh.SetApartmentState(ApartmentState.STA);//必须启动单元线程
NewTh.Start();
}
在项目中新建一个类 WebPageBitmap 以下内容是类的内容
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Windows.Forms;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Threading;
using System.Drawing.Imaging;
using System.IO;
/// <summary>
///WebPageBitmap 的摘要说明
/// </summary>
public class WebPageBitmap
{
public WebPageBitmap()
{
//
//TODO: 在此处添加构造函数逻辑
//
}
WebBrowser MyBrowser;
string URL;
int Height;
int Width;
public WebPageBitmap(string url)
{
this.URL = url;
MyBrowser = new WebBrowser();
MyBrowser.ScrollBarsEnabled = false;
}
/// <summary>
/// 初始化网页
/// </summary>
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);
}
/// <summary>
/// 截图
/// </summary>
/// <returns></returns>
public System.Drawing.Image DrawImage()
{
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 oThumbNail;
}
catch
{
return null;
}
finally
{
imgOutput.Dispose();
imgOutput = null;
MyBrowser.Dispose();
MyBrowser = null;
}
}
}
这是截的图