最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

C#利用WebBrowser將網(wǎng)頁(yè)保存為圖片

 更新時(shí)間:2025年10月29日 09:52:23   作者:清山博客  
這篇文章主要為大家詳細(xì)介紹了C#如何利用WebBrowser將網(wǎng)頁(yè)保存為圖片,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下

本文主要來(lái)和大家介紹一種使用 C# 實(shí)現(xiàn)從 URL 獲取網(wǎng)頁(yè)并將其轉(zhuǎn)換為圖片的方法。通過(guò) WebBrowser 控件加載指定 URL 的網(wǎng)頁(yè)內(nèi)容,調(diào)整其大小以適應(yīng)整個(gè)網(wǎng)頁(yè),并利用 DrawToBitmap 方法將顯示的內(nèi)容導(dǎo)出為位圖文件。支持 JPEG 和 PNG 兩種格式。

以下是完整代碼:

using System;
using System.Drawing;
using System.Windows.Forms;

namespace UrlToImage
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            var browser = new WebBrowser {ScrollBarsEnabled = false, ScriptErrorsSuppressed = true};
            browser.Navigate("http://www.rc114.com/");
            browser.DocumentCompleted += webBrowser_DocumentCompleted;
        }

        private void webBrowser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            var browser = (WebBrowser) sender;
            if (browser.ReadyState == WebBrowserReadyState.Complete)
            {
                if (browser.Document != null)
                {
                    if (browser.Document.Body != null)
                    {
                        var height = browser.Document.Body.ScrollRectangle.Height;
                        var width = browser.Document.Body.ScrollRectangle.Width;
                        browser.Height = height;
                        browser.Width = width;
                        using (var bitmap = new Bitmap(width, height))
                        {
                            var rectangle = new Rectangle(0, 0, width, height);
                            browser.DrawToBitmap(bitmap, rectangle);
                            var dialog = new SaveFileDialog();
                            dialog.Filter = " JPEG (*.jpg)|*.jpg|PNG (*.png)|*.png ";
                            dialog.ShowDialog();
                            bitmap.Save(dialog.FileName);
                        }
                    }
                }
            }
        }
    }
}

方法補(bǔ)充

C#將網(wǎng)頁(yè)內(nèi)容轉(zhuǎn)換成圖片保存到本地( webbrowser 可應(yīng)用于B/S結(jié)構(gòu)中)

代碼如下:

GetImage.cs 類(lèi)

using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
namespace SaveHtMLtoImageComm
{
    public class GetImage
    {
        private int S_Height;
        private int S_Width;
        private int F_Height;
        private int F_Width;
        private string MyURL;
        public int ScreenHeight
        {
            get { return S_Height; }
            set { S_Height = value; }
        }
        public int ScreenWidth
        {
            get { return S_Width; }
            set { S_Width = value; }
        }
        public int ImageHeight
        {
            get { return F_Height; }
            set { F_Height = value; }
        }
        public int ImageWidth
        {
            get { return F_Width; }
            set { F_Width = value; }
        }
        public string WebSite
        {
            get { return MyURL; }
            set { MyURL = value; }
        }
        public GetImage(string WebSite, int ScreenWidth, int ScreenHeight, int ImageWidth, int ImageHeight)
        {
            this.WebSite = WebSite;
            this.ScreenWidth = ScreenWidth;
            this.ScreenHeight = ScreenHeight;
            this.ImageHeight = ImageHeight;
            this.ImageWidth = ImageWidth;
        }
        public Bitmap GetBitmap()
        {
            WebPageBitmap Shot = new WebPageBitmap(this.WebSite, this.ScreenWidth, this.ScreenHeight);
            Shot.GetIt();
            Bitmap Pic = Shot.DrawBitmap(this.ImageHeight, this.ImageWidth);
            return Pic;
        }
    }
}

WebPageBitmap.cs類(lèi)

using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;
using System.Net;
using System.Runtime.InteropServices;
namespace SaveHtMLtoImageComm
{
 
    public struct Struct_INTERNET_PROXY_INFO 
{ 
public int dwAccessType; 
public IntPtr proxy; 
public IntPtr proxyBypass; 
}; 
    public class WebPageBitmap
    {
        [DllImport("wininet.dll", SetLastError = true)]
        private static extern bool InternetSetOption(IntPtr hInternet, int dwOption, IntPtr lpBuffer, int lpdwBufferLength);
        private void RefreshIESettings(string strProxy)
        {
            const int INTERNET_OPTION_PROXY = 38;
            const int INTERNET_OPEN_TYPE_PROXY = 3;
            Struct_INTERNET_PROXY_INFO struct_IPI;
            // Filling in structure 
            struct_IPI.dwAccessType = INTERNET_OPEN_TYPE_PROXY;
            struct_IPI.proxy = Marshal.StringToHGlobalAnsi(strProxy);
            struct_IPI.proxyBypass = Marshal.StringToHGlobalAnsi("local");
            // Allocating memory 
            IntPtr intptrStruct = Marshal.AllocCoTaskMem(Marshal.SizeOf(struct_IPI));
            // Converting structure to IntPtr 
            Marshal.StructureToPtr(struct_IPI, intptrStruct, true);
            bool iReturn = InternetSetOption(IntPtr.Zero, INTERNET_OPTION_PROXY, intptrStruct, Marshal.SizeOf(struct_IPI));
        } 
        WebBrowser MyBrowser;
        string URL;
        int Height;
        int Width;
        
        public WebPageBitmap(string url, int width, int height)
        {
            RefreshIESettings("www.my400800.cn:80"); 
            this.Height = height;
            this.Width = width;
            this.URL = url;
            MyBrowser = new WebBrowser();
            WebProxy proxy = new WebProxy("blog.my400800.cn", 8080);                                    //定義一個(gè)網(wǎng)關(guān)對(duì)象
         
        
           // MyBrowser. .Proxy = proxy;
            MyBrowser.ScrollBarsEnabled = false;
            MyBrowser.Size = new Size(this.Width, this.Height);
        }
        public void GetIt()
        {
            MyBrowser.Navigate(this.URL);
          
            while (MyBrowser.ReadyState != WebBrowserReadyState.Complete)
            {
                Application.DoEvents();
            }
        }
        public Bitmap DrawBitmap(int theight, int twidth)
        {
            Bitmap myBitmap = new Bitmap(Width, Height);
            Rectangle DrawRect = new Rectangle(0, 0, Width, Height);
            MyBrowser.DrawToBitmap(myBitmap, DrawRect);
            System.Drawing.Image imgOutput = myBitmap;
            System.Drawing.Image oThumbNail = new Bitmap(twidth, theight, imgOutput.PixelFormat);
            Graphics g = Graphics.FromImage(oThumbNail);
            g.CompositingQuality = CompositingQuality.HighSpeed;
            g.SmoothingMode = SmoothingMode.HighSpeed;
            g.InterpolationMode = InterpolationMode.HighQualityBilinear;
            Rectangle oRectangle = new Rectangle(0, 0, twidth, theight);
            g.DrawImage(imgOutput, oRectangle);
            try
            {
                return (Bitmap)oThumbNail;
            }
            catch (Exception ex)
            {
                return null;
            }
            finally
            {
                imgOutput.Dispose();
                imgOutput = null;
                MyBrowser.Dispose();
                MyBrowser = null;
            }
        }
    }
}

main.cs類(lèi)

using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Threading;
using System.Web.UI;
namespace SaveHtMLtoImageComm
{
  public  class main
    {
      public string strURl = "";
      public string strImgFileName = "";
      public Page Context = null;
      public void startImg() {
          string url = strURl;
          //GetImage thumb = new GetImage(url, 1024, 4000, 1024, 4000);
          GetImage thumb = new GetImage(url, 1024, 1024, 1024, 1024);
          System.Drawing.Bitmap x = thumb.GetBitmap();
          //string FileName = DateTime.Now.ToString("yyyyMMddhhmmss");
          x.Save(Context.Server.MapPath("~") + @"/image/" + strImgFileName);// FileName + ".jpg");
      }
      
    }
}

到此這篇關(guān)于C#利用WebBrowser將網(wǎng)頁(yè)保存為圖片的文章就介紹到這了,更多相關(guān)C#網(wǎng)頁(yè)保存為圖片內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Unity工具類(lèi)ScrollView實(shí)現(xiàn)拖拽滑動(dòng)翻頁(yè)

    Unity工具類(lèi)ScrollView實(shí)現(xiàn)拖拽滑動(dòng)翻頁(yè)

    這篇文章主要為大家詳細(xì)介紹了Unity工具類(lèi)ScrollView實(shí)現(xiàn)拖拽滑動(dòng)翻頁(yè),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2020-04-04
  • C# 對(duì)文件與文件夾的操作包括刪除、移動(dòng)與復(fù)制

    C# 對(duì)文件與文件夾的操作包括刪除、移動(dòng)與復(fù)制

    在.Net中,對(duì)文件(File)和文件夾(Folder)的操作可以使用File類(lèi)和Directory類(lèi),也可以使用FileInfo類(lèi)和DirectoryInfo類(lèi),本文將詳細(xì)介紹,需要的朋友可以參考
    2012-11-11
  • C#淺拷貝和深拷貝實(shí)例解析

    C#淺拷貝和深拷貝實(shí)例解析

    這篇文章主要介紹了C#淺拷貝和深拷貝,是比較重要的概念,需要的朋友可以參考下
    2014-08-08
  • C#:(變量)字段和局部變量的作用域沖突

    C#:(變量)字段和局部變量的作用域沖突

    C#:字段和局部變量的作用域沖突,需要的朋友可以參考一下
    2013-02-02
  • c# 快速排序算法

    c# 快速排序算法

    快速排序使用分治法(Divide and conquer)策略來(lái)把一個(gè)串行(list)分為兩個(gè)子串行(sub-lists)
    2013-10-10
  • c# 實(shí)現(xiàn)的支付寶支付

    c# 實(shí)現(xiàn)的支付寶支付

    這篇文章主要介紹了c# 實(shí)現(xiàn)的支付寶支付的方法,幫助大家更好的理解和使用c#,感興趣的朋友可以了解下
    2021-01-01
  • 一文 詳解C#正確使用異常的六大原則

    一文 詳解C#正確使用異常的六大原則

    編程的世界充滿(mǎn)了挑戰(zhàn)和樂(lè)趣,異常就是我們繞不過(guò)去的大石頭,有時(shí)候,我們需要主動(dòng)引發(fā)一些異常,有時(shí)候,又需要主動(dòng)捕捉一些異常,今天,我們就來(lái)聊聊六個(gè)關(guān)于異常使用的黃金法則,幫助你在這個(gè)充滿(mǎn)挑戰(zhàn)的領(lǐng)域中游刃有余,需要的朋友可以參考下
    2025-12-12
  • C#抽象類(lèi)?(abstract?class)?vs?接口?(interface)?選型與應(yīng)用場(chǎng)景分析

    C#抽象類(lèi)?(abstract?class)?vs?接口?(interface)?選型與應(yīng)用場(chǎng)景分析

    在C#中,抽象類(lèi)(abstract class)和接口(interface)是實(shí)現(xiàn)面向?qū)ο笤O(shè)計(jì)中的多態(tài)性和代碼復(fù)用的兩種主要機(jī)制,雖然它們都可以用于定義方法的簽名和實(shí)現(xiàn)共享的屬性,但它們?cè)谠O(shè)計(jì)理念、使用場(chǎng)景和靈活性方面存在顯著差異,下面詳細(xì)介紹它們各自的特性和應(yīng)用場(chǎng)景
    2026-06-06
  • C# ListBox中的Item拖拽代碼分享

    C# ListBox中的Item拖拽代碼分享

    在本文中我們給大家分享了關(guān)于C#的ListBox中的Item拖拽的功能代碼分享,對(duì)此有需要的朋友參考學(xué)習(xí)下。
    2018-03-03
  • C#使用SqlServer作為日志數(shù)據(jù)庫(kù)的設(shè)計(jì)與實(shí)現(xiàn)

    C#使用SqlServer作為日志數(shù)據(jù)庫(kù)的設(shè)計(jì)與實(shí)現(xiàn)

    這篇文章主要給大家介紹了關(guān)于C#使用SqlServer作為日志數(shù)據(jù)庫(kù)的設(shè)計(jì)與實(shí)現(xiàn)的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2021-01-01

最新評(píng)論

中西区| 武乡县| 武夷山市| 井冈山市| 珠海市| 溧水县| 福鼎市| 宜昌市| 平谷区| 五指山市| 白沙| 茂名市| 新民市| 雷州市| 灵台县| 仪陇县| 梅河口市| 普定县| 三门峡市| 五寨县| 原平市| 都昌县| 清水河县| 奇台县| 本溪市| 马关县| 桃园市| 佳木斯市| 夏津县| 阿克陶县| 海兴县| 博野县| 介休市| 凤冈县| 马鞍山市| 衡阳县| 望谟县| 汪清县| 萨嘎县| 岳普湖县| 兴海县|