在网页中根据url截图并输出到网页中

 

 

网页截图是很多站点的一个小需求,这段代码实现的是如何根据url获得网页截图并输出到网页中。

代码
 
   
1 using System;
2   using System.Collections.Generic;
3   using System.Linq;
4   using System.Web;
5   using System.Web.UI;
6   using System.Web.UI.WebControls;
7   using System.Threading;
8   using System.Windows.Forms;
9 using System.Drawing;
10 using System.IO;
11
12 /// <summary>
13 /// This page show the way of generate a image in website
14 /// </summary>
15 public partial class Default2 : System.Web.UI.Page
16 {
17 protected void Page_Load( object sender, EventArgs e)
18 {
19 Bitmap m_Bitmap = WebSiteThumbnail.GetWebSiteThumbnail( " http://www.google.cn " , 600 , 600 , 600 , 600 );
20 MemoryStream ms = new MemoryStream();
21 m_Bitmap.Save(ms, System.Drawing.Imaging.ImageFormat.Png); // JPG、GIF、PNG等均可
22 byte [] buff = ms.ToArray();
23 Response.BinaryWrite(buff);
24 }
25 }
26
27 public class WebSiteThumbnail
28 {
29 Bitmap m_Bitmap;
30 string m_Url;
31 int m_BrowserWidth, m_BrowserHeight, m_ThumbnailWidth, m_ThumbnailHeight;
32 public WebSiteThumbnail( string Url, int BrowserWidth, int BrowserHeight, int ThumbnailWidth, int ThumbnailHeight)
33 {
34 m_Url = Url;
35 m_BrowserHeight = BrowserHeight;
36 m_BrowserWidth = BrowserWidth;
37 m_ThumbnailWidth = ThumbnailWidth;
38 m_ThumbnailHeight = ThumbnailHeight;
39 }
40 public static Bitmap GetWebSiteThumbnail( string Url, int BrowserWidth, int BrowserHeight, int ThumbnailWidth, int ThumbnailHeight)
41 {
42 WebSiteThumbnail thumbnailGenerator = new WebSiteThumbnail(Url, BrowserWidth, BrowserHeight, ThumbnailWidth, ThumbnailHeight);
43 return thumbnailGenerator.GenerateWebSiteThumbnailImage();
44 }
45 public Bitmap GenerateWebSiteThumbnailImage()
46 {
47 Thread m_thread = new Thread( new ThreadStart(_GenerateWebSiteThumbnailImage));
48 m_thread.SetApartmentState(ApartmentState.STA);
49 m_thread.Start();
50 m_thread.Join();
51 return m_Bitmap;
52 }
53 private void _GenerateWebSiteThumbnailImage()
54 {
55 WebBrowser m_WebBrowser = new WebBrowser();
56 m_WebBrowser.ScrollBarsEnabled = false ;
57 m_WebBrowser.Navigate(m_Url);
58 m_WebBrowser.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(WebBrowser_DocumentCompleted);
59 while (m_WebBrowser.ReadyState != WebBrowserReadyState.Complete)
60 Application.DoEvents();
61 m_WebBrowser.Dispose();
62 }
63 private void WebBrowser_DocumentCompleted( object sender, WebBrowserDocumentCompletedEventArgs e)
64 {
65 WebBrowser m_WebBrowser = (WebBrowser)sender;
66 m_WebBrowser.ClientSize = new Size( this .m_BrowserWidth, this .m_BrowserHeight);
67 m_WebBrowser.ScrollBarsEnabled = false ;
68 m_Bitmap = new Bitmap(m_WebBrowser.Bounds.Width, m_WebBrowser.Bounds.Height);
69 m_WebBrowser.BringToFront();
70 m_WebBrowser.DrawToBitmap(m_Bitmap, m_WebBrowser.Bounds);
71 m_Bitmap = (Bitmap)m_Bitmap.GetThumbnailImage(m_ThumbnailWidth, m_ThumbnailHeight, null , IntPtr.Zero);
72 }
73 }
74
75

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值