文章出處

  public class HomeController : Controller
    {
        //
        // GET: /Home/
        static System.Windows.Forms.WebBrowser wb;
      
        public void ScreenCapture()
        {
            System.Threading.Thread t = new System.Threading.Thread(new ThreadStart(() =>
            {
                wb = new System.Windows.Forms.WebBrowser();
                wb.DocumentCompleted += wb_DocumentCompleted;
                wb.Navigate("https://www.baidu.com/");
                while (wb.ReadyState != System.Windows.Forms.WebBrowserReadyState.Complete)
                {
                    System.Windows.Forms.Application.DoEvents(); //避免假死,若去掉則可能無法觸發 DocumentCompleted 事件。
                }
            })
            );
            t.SetApartmentState(ApartmentState.STA);
            t.Start();
        }
        void wb_DocumentCompleted(object sender, System.Windows.Forms.WebBrowserDocumentCompletedEventArgs e)
        {
            //設置瀏覽器寬度、高度為文檔寬度、高度,以便截取整個網頁。
            //wb.Width = wb.Document.Body.ScrollRectangle.Width;
            //wb.Height = wb.Document.Body.ScrollRectangle.Height;
            wb.Width = 1366;
            wb.Height = wb.Document.Body.ScrollRectangle.Height;
            using (Bitmap bmp = new Bitmap(wb.Width, wb.Height))
            {
                wb.DrawToBitmap(bmp, new Rectangle(0, 0, bmp.Width, bmp.Height));
                bmp.Save("C:\\Capture1.png", ImageFormat.Png);
            }
        }
    }

 


文章列表


不含病毒。www.avast.com
arrow
arrow
    全站熱搜
    創作者介紹
    創作者 大師兄 的頭像
    大師兄

    IT工程師數位筆記本

    大師兄 發表在 痞客邦 留言(0) 人氣()