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

首頁 > 編程 > C# > 正文

C#實(shí)現(xiàn)在網(wǎng)頁中根據(jù)url截圖并輸出到網(wǎng)頁的方法

2020-01-24 01:19:43
字體:
供稿:網(wǎng)友

本文實(shí)例講述了C#實(shí)現(xiàn)在網(wǎng)頁中根據(jù)url截圖并輸出到網(wǎng)頁的方法。分享給大家供大家參考,具體如下:

網(wǎng)頁截圖是很多站點(diǎn)的一個(gè)小需求,這段代碼實(shí)現(xiàn)的是如何根據(jù)url獲得網(wǎng)頁截圖并輸出到網(wǎng)頁中。

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);  }}

希望本文所述對(duì)大家C#程序設(shè)計(jì)有所幫助。

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 遵义县| 宕昌县| 海安县| 平远县| 临沧市| 林周县| 永州市| 孙吴县| 华亭县| 长丰县| 苗栗县| 吕梁市| 西畴县| 长顺县| 日喀则市| 彰化市| 夏邑县| 大新县| 金沙县| 黑河市| 白朗县| 临武县| 嘉善县| 西和县| 江阴市| 沐川县| 喜德县| 武川县| 龙游县| 廉江市| 哈巴河县| 上杭县| 临安市| 洛宁县| 泰安市| 鹤岗市| 鹤庆县| 大同市| 安乡县| 周至县| 哈巴河县|