国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C# > 正文

C#實現在網頁中根據url截圖并輸出到網頁的方法

2019-10-29 21:34:03
字體:
來源:轉載
供稿:網友
這篇文章主要介紹了C#實現在網頁中根據url截圖并輸出到網頁的方法,涉及C#網頁瀏覽器及圖片操作的相關技巧,需要的朋友可以參考下
 

本文實例講述了C#實現在網頁中根據url截圖并輸出到網頁的方法。分享給大家供大家參考,具體如下:

網頁截圖是很多站點的一個小需求,這段代碼實現的是如何根據url獲得網頁截圖并輸出到網頁中。

using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.UI;using System.Web.UI.WebControls;using System.Threading;using System.Windows.Forms;using System.Drawing;using System.IO;/// <summary>/// This page show the way of generate a image in website/// </summary>public partial class Default2 : System.Web.UI.Page{  protected void Page_Load(object sender, EventArgs e)  {    Bitmap m_Bitmap = WebSiteThumbnail.GetWebSiteThumbnail("http://www.google.cn", 600, 600, 600, 600);    MemoryStream ms = new MemoryStream();    m_Bitmap.Save(ms, System.Drawing.Imaging.ImageFormat.Png);//JPG、GIF、PNG等均可      byte[] buff = ms.ToArray();    Response.BinaryWrite(buff);  }}public class WebSiteThumbnail{  Bitmap m_Bitmap;  string m_Url;  int m_BrowserWidth, m_BrowserHeight, m_ThumbnailWidth, m_ThumbnailHeight;  public WebSiteThumbnail(string Url, int BrowserWidth, int BrowserHeight, int ThumbnailWidth, int ThumbnailHeight)  {    m_Url = Url;    m_BrowserHeight = BrowserHeight;    m_BrowserWidth = BrowserWidth;    m_ThumbnailWidth = ThumbnailWidth;    m_ThumbnailHeight = ThumbnailHeight;  }  public static Bitmap GetWebSiteThumbnail(string Url, int BrowserWidth, int BrowserHeight, int ThumbnailWidth, int ThumbnailHeight)  {    WebSiteThumbnail thumbnailGenerator = new WebSiteThumbnail(Url, BrowserWidth, BrowserHeight, ThumbnailWidth, ThumbnailHeight);    return thumbnailGenerator.GenerateWebSiteThumbnailImage();  }  public Bitmap GenerateWebSiteThumbnailImage()  {    Thread m_thread = new Thread(new ThreadStart(_GenerateWebSiteThumbnailImage));    m_thread.SetApartmentState(ApartmentState.STA);    m_thread.Start();    m_thread.Join();    return m_Bitmap;  }  private void _GenerateWebSiteThumbnailImage()  {    WebBrowser m_WebBrowser = new WebBrowser();    m_WebBrowser.ScrollBarsEnabled = false;    m_WebBrowser.Navigate(m_Url);    m_WebBrowser.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(WebBrowser_DocumentCompleted);    while(m_WebBrowser.ReadyState != WebBrowserReadyState.Complete)      Application.DoEvents();    m_WebBrowser.Dispose();  }  private void WebBrowser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)  {    WebBrowser m_WebBrowser = (WebBrowser)sender;    m_WebBrowser.ClientSize = new Size(this.m_BrowserWidth, this.m_BrowserHeight);    m_WebBrowser.ScrollBarsEnabled = false;    m_Bitmap = new Bitmap(m_WebBrowser.Bounds.Width, m_WebBrowser.Bounds.Height);    m_WebBrowser.BringToFront();    m_WebBrowser.DrawToBitmap(m_Bitmap, m_WebBrowser.Bounds);    m_Bitmap = (Bitmap)m_Bitmap.GetThumbnailImage(m_ThumbnailWidth, m_ThumbnailHeight, null, IntPtr.Zero);  }}

希望本文所述對大家C#程序設計有所幫助。



注:相關教程知識閱讀請移步到c#教程頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 镇赉县| 洪湖市| 襄城县| 英超| 林口县| 财经| 东乡族自治县| 崇阳县| 梅州市| 蕲春县| 娱乐| 大邑县| 房山区| 达日县| 哈巴河县| 华亭县| 元朗区| 班戈县| 渑池县| 汪清县| 平乡县| 河池市| 当雄县| 孟州市| 绥中县| 昆明市| 卢龙县| 浦城县| 浮山县| 永兴县| 那坡县| 马山县| 兴义市| 定南县| 永城市| 扶绥县| 股票| 浙江省| 仪征市| 什邡市| 永川市|