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)問題
- 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)通過ffmpeg從flv視頻文件中截圖的方法
- C#實(shí)現(xiàn)網(wǎng)頁(yè)截圖功能
- c#實(shí)現(xiàn)winform屏幕截圖并保存的示例
- C# 抓圖服務(wù)的實(shí)現(xiàn)
相關(guān)文章
c# 免費(fèi)組件html轉(zhuǎn)pdf的實(shí)現(xiàn)過程
這篇文章主要介紹了c# 免費(fèi)組件html轉(zhuǎn)pdf的實(shí)現(xiàn)過程,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-06-06
c#打印預(yù)覽控件中實(shí)現(xiàn)用鼠標(biāo)移動(dòng)頁(yè)面功能代碼分享
項(xiàng)目中需要實(shí)現(xiàn)以下功能:打印預(yù)覽控件中,可以用鼠標(biāo)拖動(dòng)頁(yè)面,以查看超出顯示范圍之外的部分內(nèi)容,下面就是實(shí)現(xiàn)代碼2013-12-12
Unity實(shí)現(xiàn)物體運(yùn)動(dòng)時(shí)畫出軌跡
這篇文章主要為大家詳細(xì)介紹了Unity實(shí)現(xiàn)物體運(yùn)動(dòng)時(shí)畫出軌跡,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-09-09
C#使用List類實(shí)現(xiàn)動(dòng)態(tài)變長(zhǎng)數(shù)組的方法
這篇文章主要介紹了C#使用List類實(shí)現(xiàn)動(dòng)態(tài)變長(zhǎng)數(shù)組的方法,涉及C#中List類的使用技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-04-04

