Last active
September 14, 2022 19:15
-
-
Save micklaw/6eb58988c2b587bbdc7d to your computer and use it in GitHub Desktop.
Screengrab a website c#
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Drawing; | |
using System.Drawing.Imaging; | |
using System.IO; | |
using System.Linq; | |
using System.Threading; | |
using System.Windows.Forms; | |
namespace ScreenGrab.Core.Helpers | |
{ | |
/// <summary> | |
/// Create a screenshot .png of a remote webpage | |
/// Implemented with this link in mind: http://stackoverflow.com/questions/2715385/convert-webpage-to-image-from-asp-net | |
/// </summary> | |
public class WebsiteToImage : IDisposable | |
{ | |
private Bitmap _bitmap; | |
private readonly string _url; | |
private readonly bool _useBodyHeight; | |
private readonly int _width = 1800; | |
private int _height = 1400; | |
public WebsiteToImage(string url) | |
{ | |
_url = url; | |
} | |
public WebsiteToImage(string url, int width, int height, bool useBodyHeight) : this(url) | |
{ | |
_width = width; | |
_height = height; | |
_useBodyHeight = useBodyHeight; | |
} | |
public Bitmap Generate() | |
{ | |
var thread = new Thread(_Generate); | |
thread.SetApartmentState(ApartmentState.STA); | |
thread.Start(); | |
thread.Join(); | |
return _bitmap; | |
} | |
private void _Generate() | |
{ | |
/* [ML] - It is possible to tell windows which rendering compatibility to use while running the WebBrowser. | |
http://weblog.west-wind.com/posts/2011/May/21/Web-Browser-Control-Specifying-the-IE-Version */ | |
using (var browser = new WebBrowser {ScrollBarsEnabled = false, ScriptErrorsSuppressed = true}) | |
{ | |
browser.Navigate(_url); | |
browser.DocumentCompleted += WebBrowser_DocumentCompleted; | |
while (browser.ReadyState != WebBrowserReadyState.Complete) | |
{ | |
Application.DoEvents(); | |
} | |
} | |
} | |
private void WebBrowser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e) | |
{ | |
var browser = (WebBrowser)sender; | |
if (browser != null && e.Url == browser.Url) | |
{ | |
if (browser?.Document?.Body != null) | |
{ | |
if (_useBodyHeight) | |
{ | |
_height = browser.Document.Body.ScrollRectangle.Height; | |
} | |
browser.ClientSize = new Size(_width, _height); | |
_bitmap = new Bitmap(_width, _height); | |
browser.BringToFront(); | |
browser.DrawToBitmap(_bitmap, browser.Bounds); | |
} | |
} | |
} | |
public void Dispose() | |
{ | |
Dispose(true); | |
GC.SuppressFinalize(this); | |
} | |
~WebsiteToImage() | |
{ | |
Dispose(false); | |
} | |
// The bulk of the clean-up code is implemented in Dispose(bool) | |
protected virtual void Dispose(bool disposing) | |
{ | |
if (disposing) | |
{ | |
_bitmap.Dispose(); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment