[robot]WebBrowser元件簡易範例

  • 893
  • 0
  • 2015-08-05

摘要:[Winform]簡易範例


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace NewTaipeiCityDistPicTest
{
    public partial class Form1 : Form
    {
        bool pageready = false;
        public Form1()
        {
            InitializeComponent();
           //禁止瀏覽器跳出alert, 以免影響測試進行
           webBrowser1.ScriptErrorsSuppressed = true;
            webBrowser1.Navigate("https://landzone2.planning.ntpc.gov.tw/urdweb/Map/Default.aspx");
            //等待網頁載入完畢
            WaitForPageLoad();
            //選擇行政區
            HtmlElement ddlSecDist = webBrowser1.Document.Window.Frames["QueryFrame"].Document.GetElementById("ddlSecDist");
            ddlSecDist.SetAttribute("value", "三重區");
            ddlSecDist.InvokeMember("onchange");
            WaitForPageLoad();
            //選擇地段
            HtmlElement ddlSec = webBrowser1.Document.Window.Frames["QueryFrame"].Document.GetElementById("ddlSec");
            ddlSec.SetAttribute("value", "FG051726");
            
            //設定地號
            HtmlElement txtLandno = webBrowser1.Document.Window.Frames["QueryFrame"].Document.GetElementById("txtLandno");
            txtLandno.SetAttribute("value", "88");

            //按下按鈕
            HtmlElement btnQuerySec = webBrowser1.Document.Window.Frames["QueryFrame"].Document.GetElementById("btnQuerySec");
            btnQuerySec.InvokeMember("click");
            WaitForPageLoad();
            
            //等待六秒,這期間瀏覽器繼續執行下去,但是下面的程式碼先不跑
            for (var i = 0; i < 6; i++) { Application.DoEvents(); System.Threading.Thread.Sleep(1000); }
            //截圖           
            Bitmap bitmap = new Bitmap(1500, 1200);
            Rectangle bitmapRect = new Rectangle(0, 0, 1500, 1200);
            // This is a method of the WebBrowser control, and the most important part
            webBrowser1.DrawToBitmap(bitmap, bitmapRect);

            // Generate a thumbnail of the screenshot (optional)
            System.Drawing.Image origImage = bitmap;
            System.Drawing.Image origThumbnail = new Bitmap(1500, 1200, origImage.PixelFormat);

            Graphics oGraphic = Graphics.FromImage(origThumbnail);
            oGraphic.CompositingQuality = CompositingQuality.HighQuality;
            oGraphic.SmoothingMode = SmoothingMode.HighQuality;
            oGraphic.InterpolationMode = InterpolationMode.HighQualityBicubic;
            Rectangle oRectangle = new Rectangle(0, 0, 1500, 1200);
            oGraphic.DrawImage(origImage, oRectangle);

            // Save the file in PNG format
            origThumbnail.Save("Screenshot.png", ImageFormat.Png);
            origImage.Dispose();
            //執行下列則可關閉程式
            this.Close();
            Environment.Exit(Environment.ExitCode); 
        }
        private void WaitForPageLoad()
        {
            webBrowser1.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(PageWaiter);
            while (!pageready)
            {
                Application.DoEvents();
            }
            pageready = false;
        }

        private void PageWaiter(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            if (webBrowser1.ReadyState == WebBrowserReadyState.Complete)
            {
                pageready = true;
                webBrowser1.DocumentCompleted -= new WebBrowserDocumentCompletedEventHandler(PageWaiter);
            }
        }
        
    }
}

參考文章:

[C#][WinForm]Application.Exit關閉應用程式後真的不執行了嗎?

http://www.dotblogs.com.tw/ricochen/archive/2010/01/18/13066.aspx