1、在窗体上加一个按钮,为按钮添加点击事件
private void button1_Click(object sender, EventArgs e)
{
Bitmap m_Bitmap = WebSiteThumbnail.GetWebSiteThumbnail("http://192.168.0.106:1452/", 600, 300, 600, 600);
//保存图片 JPG、GIF、PNG等均可
m_Bitmap.Save("F:/Image/" + DateTime.Now.Year.ToString()
+ DateTime.Now.Month.ToString()
+ DateTime.Now.Day.ToString()
+ DateTime.Now.Hour.ToString()
+ DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString() + DateTime.Now.Millisecond.ToString()
+ ".jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
}
2、在点击事件中调用了WebSiteThumbnail中的方法,该类的代码如下:
public class WebSiteThumbnail
{
Bitmap m_Bitmap;
string m_Url;
int m_BrowserWidth, m_BrowserHeight, m_ThumbnailWidth, m_ThumbnailHeight;
public WebSiteThumbnail(string Url, int BrowserWidth, int BrowserHeight, int ThumbnailWidth, int ThumbnailHeight)
{
m_Url = Url;
m_BrowserHeight = BrowserHeight;
m_BrowserWidth = BrowserWidth;
m_ThumbnailWidth = ThumbnailWidth;
m_ThumbnailHeight = ThumbnailHeight;
}
public static Bitmap GetWebSiteThumbnail(string Url, int BrowserWidth, int BrowserHeight, int ThumbnailWidth, int ThumbnailHeight)
{
WebSiteThumbnail thumbnailGenerator = new WebSiteThumbnail(Url, BrowserWidth, BrowserHeight, ThumbnailWidth, ThumbnailHeight);
return thumbnailGenerator.GenerateWebSiteThumbnailImage();
}
public Bitmap GenerateWebSiteThumbnailImage()
{
Thread m_thread = new Thread(new ThreadStart(_GenerateWebSiteThumbnailImage));
m_thread.SetApartmentState(ApartmentState.STA);
m_thread.Start();
m_thread.Join();
return m_Bitmap;
}
private void _GenerateWebSiteThumbnailImage()
{
WebBrowser m_WebBrowser = new WebBrowser();
m_WebBrowser.ScrollBarsEnabled = false;
m_WebBrowser.Navigate(m_Url);
m_WebBrowser.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(WebBrowser_DocumentCompleted);
while (m_WebBrowser.ReadyState != WebBrowserReadyState.Complete)
Application.DoEvents();
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;
//获取网页文档对象,相当于获取网页的全部源码
HtmlDocument htmlDoc = m_WebBrowser.Document;
HtmlElement btn = htmlDoc.GetElementById("Login1_LoginButton");
if (btn != null)
{
//设置帐号
HtmlElement username = htmlDoc.GetElementById("Login1_UserName");
username.SetAttribute("value", "system");
//设置密码
HtmlElement pwd = htmlDoc.GetElementById("Login1_Password");
pwd.SetAttribute("value", "11111");
//登录
if (btn != null)
{
btn.InvokeMember("Click");
}
//进入后的网址
m_WebBrowser.Navigate("http://192.168.0.106:1452/Manager.aspx");
Thread.Sleep(1000);
m_Bitmap = new Bitmap(m_WebBrowser.Bounds.Width, m_WebBrowser.Bounds.Height);
m_WebBrowser.BringToFront();
HtmlDocument document = m_WebBrowser.Document;
document.Window.ScrollTo(200, 100);
m_WebBrowser.DrawToBitmap(m_Bitmap, new Rectangle(new Point(0, 0), new Size(600, 600)));
}
else
{
m_Bitmap = new Bitmap(m_WebBrowser.Bounds.Width, m_WebBrowser.Bounds.Height);
m_WebBrowser.BringToFront();
HtmlDocument document = m_WebBrowser.Document;
document.Window.ScrollTo(200, 100);
m_WebBrowser.DrawToBitmap(m_Bitmap, new Rectangle(new Point(0, 0), new Size(600, 600)));
}
}
}