Created
January 12, 2014 23:09
-
-
Save cheeplusplus/8391955 to your computer and use it in GitHub Desktop.
URL to PNG
This file contains 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.Threading; | |
using Awesomium.Core; | |
namespace UrlToPng | |
{ | |
class Program | |
{ | |
private const string ScrollbarCss = "::-webkit-scrollbar { visibility: hidden; }"; | |
static void Main(string[] args) | |
{ | |
if (args.Length < 4) | |
{ | |
Console.WriteLine("Missing arguments: UrlToPng.exe <w> <h> <url> <output>"); | |
return; | |
} | |
var width = int.Parse(args[0]); | |
var height = int.Parse(args[1]); | |
var target = args[2]; | |
var output = args[3]; | |
// Display some informative message. Loading the page | |
// may take a while depending on your internet | |
// connection speed. | |
Console.WriteLine("Getting a {0}x{1} snapshot of {2} ...", width, height, target); | |
// We demonstrate an easy way to hide the scrollbars by providing | |
// custom CSS. Read more about how to style the scrollbars here: | |
// http://www.webkit.org/blog/363/styling-scrollbars/. | |
var prefs = WebPreferences.Default; | |
prefs.CustomCSS = ScrollbarCss; | |
var session = WebCore.CreateWebSession(prefs); | |
// Create a WebView. | |
// WebView implements IDisposable. You can dispose and | |
// destroy the view by calling WebView.Close(). | |
// Here we demonstrate wrapping it in a using statement. | |
using (var webView = WebCore.CreateWebView(width, height, session)) | |
{ | |
// Variable used to announce | |
// that the page has loaded. | |
var finishedLoading = false; | |
// Load a page in the view. | |
webView.Source = target.ToUri(); | |
// Handle the LoadCompleted event to monitor page loading. | |
webView.LoadingFrameComplete += (sender, e) => | |
{ | |
finishedLoading = true; | |
}; | |
// Wait for the page to load. | |
while (!finishedLoading) | |
{ | |
Thread.Sleep(100); | |
// WebCore provides an Auto-Update feature | |
// for UI applications. A console application | |
// has no UI and no synchronization context | |
// so we need to manually call Update here. | |
WebCore.Update(); | |
} | |
// Render to a pixel buffer and save the buffer to a .png image. | |
var surface = (BitmapSurface)webView.Surface; | |
surface.SaveToPNG(output); | |
} | |
// Shut down Awesomium before exiting. | |
WebCore.Shutdown(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment