C#网页 截图

Posted fred1987

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C#网页 截图相关的知识,希望对你有一定的参考价值。

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#网页 截图的主要内容,如果未能解决你的问题,请参考以下文章

C#实现网页加载后将页面截取成长图片

CefSharp截取完整网页图片,网页截图

Python中使用 Selenium 实现网页截图实例

C# winform 如何直接读取HTML语言后打开该网页

如何使用 C# 和 ASP.NET 从网页中截取 div?

如何以Java实现网页截图技术,根据URL得到网页快照