码迷,mamicode.com
首页 > Windows程序 > 详细

C#网页 截图

时间:2019-07-01 19:57:31      阅读:143      评论:0      收藏:0      [点我收藏+]

标签:save   send   llb   while   navigate   app   ble   class   nta   

using System.IO;
using System.Drawing;
using System.Drawing.Imaging;
using System.Threading;
using System.Windows.Forms;
using System;

namespace ConsoleApp1
{
    public  class CaptureWebPage
    {

        public byte[] picbytes; 

        public CaptureWebPage(string url)
        {
            string bmpPath =DateTime.Now.ToString("yyyyMMddHHmmssffff")+".bmp";
            Bitmap bmp = new Bitmap(1000,10000);
            
            makepicture(url);
            bmp = ByteToImage(picbytes);
            bmp.Save(bmpPath);
        }
        private void makepicture(string url)
        {
            Thread thread = new Thread(delegate ()
            {
                using (WebBrowser browser = new WebBrowser())
                {
                    browser.ScrollBarsEnabled = true;
                    browser.AllowNavigation = true;
                    browser.Navigate(url);
                    browser.Width = 1024;
                    browser.Height =10000;
                    browser.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(DocumentCompleted);
                    while (browser.ReadyState != WebBrowserReadyState.Complete)
                    {
                        System.Windows.Forms.Application.DoEvents();
                    }

                }
            });
            thread.SetApartmentState(ApartmentState.STA);
            thread.Start();
            thread.Join();
        }

        private void DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            WebBrowser browser = sender as WebBrowser;
            using (Bitmap bitmap = new Bitmap(browser.Width, browser.Height))
            {
                browser.DrawToBitmap(bitmap, new System.Drawing.Rectangle(0, 0, browser.Width, browser.Height));
                using (MemoryStream stream = new MemoryStream())
                {
                    bitmap.Save(stream, System.Drawing.Imaging.ImageFormat.Png);
                    byte[] bytes = stream.ToArray();
                    picbytes = bytes;

                }
            }
        }

        public static Bitmap ByteToImage(byte[] blob)
        {
            MemoryStream mStream = new MemoryStream();
            byte[] pData = blob;
            mStream.Write(pData, 0, Convert.ToInt32(pData.Length));
            Bitmap bm = new Bitmap(mStream, false);
            mStream.Dispose();
            return bm;
        }
    }
}

 

C#网页 截图

标签:save   send   llb   while   navigate   app   ble   class   nta   

原文地址:https://www.cnblogs.com/Fred1987/p/11116234.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!