1 /// <summary> 2 /// WebBrowser Url生成图片 3 /// HTML转图片 4 /// </summary> 5 public class WebSnapshotsHelper 6 { 7 Bitmap m_Bitmap; 8 string m_Url; 9 int m_BrowserWidth, m_BrowserHeight, m_ThumbnailWidth, m_ThumbnailHeight; 10 public WebSnapshotsHelper(string Url, int BrowserWidth, int BrowserHeight, int ThumbnailWidth, int ThumbnailHeight) 11 { 12 m_Url = Url; 13 m_BrowserHeight = BrowserHeight; 14 m_BrowserWidth = BrowserWidth; 15 m_ThumbnailWidth = ThumbnailWidth; 16 m_ThumbnailHeight = ThumbnailHeight; 17 } 18 public static Bitmap GetWebSiteThumbnail(string Url, int BrowserWidth, int BrowserHeight, int ThumbnailWidth, int ThumbnailHeight) 19 { 20 WebSnapshotsHelper thumbnailGenerator = new WebSnapshotsHelper(Url, BrowserWidth, BrowserHeight, ThumbnailWidth, ThumbnailHeight); 21 return thumbnailGenerator.GenerateWebSiteThumbnailImage(); 22 } 23 public Bitmap GenerateWebSiteThumbnailImage() 24 { 25 Thread m_thread = new Thread(new ThreadStart(_GenerateWebSiteThumbnailImage)); 26 m_thread.SetApartmentState(ApartmentState.STA); 27 m_thread.Start(); 28 m_thread.Join(); 29 return m_Bitmap; 30 } 31 private void _GenerateWebSiteThumbnailImage() 32 { 33 WebBrowser m_WebBrowser = new WebBrowser(); 34 m_WebBrowser.ScrollBarsEnabled = false; 35 m_WebBrowser.Navigate(m_Url); 36 m_WebBrowser.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(WebBrowser_DocumentCompleted); 37 while (m_WebBrowser.ReadyState != WebBrowserReadyState.Complete) 38 { 39 Application.DoEvents(); 40 } 41 m_WebBrowser.Dispose(); 42 } 43 private void WebBrowser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e) 44 { 45 WebBrowser m_WebBrowser = (WebBrowser)sender; 46 m_WebBrowser.ClientSize = new Size(this.m_BrowserWidth, this.m_BrowserHeight); 47 m_WebBrowser.ScrollBarsEnabled = false; 48 m_Bitmap = new Bitmap(m_WebBrowser.Bounds.Width, m_WebBrowser.Bounds.Height); 49 m_WebBrowser.BringToFront(); 50 m_WebBrowser.DrawToBitmap(m_Bitmap, m_WebBrowser.Bounds); 51 m_Bitmap = (Bitmap)m_Bitmap.GetThumbnailImage(m_ThumbnailWidth, m_ThumbnailHeight, null, IntPtr.Zero); 52 } 53 54 }