天天看点

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

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

在网页中根据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

作者:

Tyler Ning

出处:

http://www.cnblogs.com/tylerdonet/

本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,如有问题,可以通过以下邮箱地址

[email protected]

 联系我,非常感谢。

继续阅读