C# 類比網站登陸並截圖

來源:互聯網
上載者:User

標籤: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# 類比網站登陸並

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.