C#實(shí)現(xiàn)在網(wǎng)頁(yè)中根據(jù)url截圖并輸出到網(wǎng)頁(yè)的方法
本文實(shí)例講述了C#實(shí)現(xiàn)在網(wǎng)頁(yè)中根據(jù)url截圖并輸出到網(wǎng)頁(yè)的方法。分享給大家供大家參考,具體如下:
網(wǎng)頁(yè)截圖是很多站點(diǎn)的一個(gè)小需求,這段代碼實(shí)現(xiàn)的是如何根據(jù)url獲得網(wǎng)頁(yè)截圖并輸出到網(wǎng)頁(yè)中。
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ì)有所幫助。
- C# 使用BitBlt進(jìn)行窗口抓圖的示例
- C# 實(shí)現(xiàn)SDL2進(jìn)行視頻播放窗口截圖和字幕添加
- C#實(shí)現(xiàn)QQ截圖功能及相關(guān)問(wèn)題
- C#實(shí)現(xiàn)的滾動(dòng)網(wǎng)頁(yè)截圖功能示例
- C# 實(shí)現(xiàn)截圖軟件功能實(shí)例代碼
- C# 實(shí)現(xiàn)QQ式截圖功能實(shí)例代碼
- C#實(shí)現(xiàn)屬于自己的QQ截圖工具
- C#實(shí)現(xiàn)通過(guò)ffmpeg從flv視頻文件中截圖的方法
- C#實(shí)現(xiàn)網(wǎng)頁(yè)截圖功能
- c#實(shí)現(xiàn)winform屏幕截圖并保存的示例
- C# 抓圖服務(wù)的實(shí)現(xiàn)
相關(guān)文章
C#中使用WinRAR實(shí)現(xiàn)加密壓縮及解壓縮文件
這篇文章主要介紹了C#中使用WinRAR實(shí)現(xiàn)加密壓縮及解壓縮文件,本文直接給出實(shí)例代碼,代碼中包含詳細(xì)注釋,需要的朋友可以參考下2015-07-07C#使用yield關(guān)鍵字構(gòu)建迭代器詳解
這篇文章主要為大家詳細(xì)介紹了C#使用yield關(guān)鍵字構(gòu)建迭代器的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-10-10深入探討C#中的const、readonly關(guān)鍵字
這篇文章主要介紹了深入探討C#中的const、readonly關(guān)鍵字,本文可以幫助你深刻理解這兩個(gè)關(guān)鍵字,而且是面試中最可能面試到的問(wèn)題哦,需要的朋友可以參考下2014-08-08