1: /*
2: Copyright (c) 2010 <a href="http://www.gutgames.com">James Craig</a>
3:
4: Permission is hereby granted, free of charge, to any person obtaining a copy
5: of this software and associated documentation files (the "Software"), to deal
6: in the Software without restriction, including without limitation the rights
7: to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8: copies of the Software, and to permit persons to whom the Software is
9: furnished to do so, subject to the following conditions:
10:
11: The above copyright notice and this permission notice shall be included in
12: all copies or substantial portions of the Software.
13:
14: THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15: IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16: FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17: AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18: LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19: OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20: THE SOFTWARE.*/
21:
22: #region Usings
23: using System;
24: using System.Drawing;
25: using System.Drawing.Imaging;
26: using System.Threading;
27: using System.Windows.Forms;
28: #endregion
29:
30: namespace Utilities.Web.WebPageThumbnail
31: {
32: /// <summary>
33: /// Class for taking a screen shot of a web page
34: /// </summary>
35: public class WebPageThumbnail
36: {
37: #region Constructor
38: /// <summary>
39: /// Constructor
40: /// </summary>
41: public WebPageThumbnail()
42: {
43: }
44: #endregion
45:
46: #region Private Variables
47: private string FileName;
48: private string Url;
49: private int Width;
50: private int Height;
51: #endregion
52:
53: #region Public Functions
54:
55: /// <summary>
56: /// Generates a screen shot of a web site
57: /// </summary>
58: /// <param name="FileName">File name to save as</param>
59: /// <param name="Url">Url to take the screen shot of</param>
60: /// <param name="Width">Width of the image (-1 for full size)</param>
61: /// <param name="Height">Height of the image (-1 for full size)</param>
62: public void GenerateBitmap(string FileName, string Url, int Width, int Height)
63: {
64: this.Url = Url;
65: this.FileName = FileName;
66: this.Width = Width;
67: this.Height = Height;
68: Thread TempThread = new Thread(new ThreadStart(CreateBrowser));
69: TempThread.SetApartmentState(ApartmentState.STA);
70: TempThread.Start();
71: TempThread.Join();
72: }
73:
74: #endregion
75:
76: #region Private Functions
77:
78: /// <summary>
79: /// Creates the browser
80: /// </summary>
81: private void CreateBrowser()
82: {
83: using (WebBrowser Browser = new WebBrowser())
84: {
85: Browser.ScrollBarsEnabled = false;
86: DateTime TimeoutStart = DateTime.Now;
87: TimeSpan Timeout = new TimeSpan(0, 0, 10);
88: Browser.Navigate(Url);
89: Browser.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(Browser_DocumentCompleted);
90: while (Browser.ReadyState != WebBrowserReadyState.Complete)
91: {
92: if (DateTime.Now - TimeoutStart > Timeout)
93: break;
94: Application.DoEvents();
95: }
96: }
97: }
98:
99: /// <summary>
100: /// Called when the browser is completed
101: /// </summary>
102: /// <param name="sender"></param>
103: /// <param name="e"></param>
104: void Browser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
105: {
106: WebBrowser Browser = (WebBrowser)sender;
107: Browser.ScriptErrorsSuppressed = true;
108: Browser.ScrollBarsEnabled = false;
109: if (Width == -1)
110: {
111: Browser.Width = Browser.Document.Body.ScrollRectangle.Width;
112: }
113: else
114: {
115: Browser.Width = Width;
116: }
117: if (Height == -1)
118: {
119: Browser.Height = Browser.Document.Body.ScrollRectangle.Height;
120: }
121: else
122: {
123: Browser.Height = Height;
124: }
125: using (Bitmap Image = new Bitmap(Browser.Width, Browser.Height))
126: {
127: Browser.BringToFront();
128: Browser.DrawToBitmap(Image, new Rectangle(0, 0, Browser.Width, Browser.Height));
129: Image.Save(FileName, ImageFormat.Bmp);
130: }
131: }
132:
133: #endregion
134: }
135: }