標籤:jpeg mon btn password dstar code 添加 nts thread
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))); } } }
C# 類比網站登陸並