-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
160 lines (146 loc) · 6.31 KB
/
Program.cs
File metadata and controls
160 lines (146 loc) · 6.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.Firefox;
using OpenQA.Selenium.IE;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace WebShot {
class Program {
static int Main(string[] args) {
if (args.Length == 0 || args.Contains("-?") || args.Contains("/?")) {
Help();
}
else {
string url = args[0];
string outPath = string.Format(@".\WebShot_{0:yyyyMMdd_hhmmss_nnn}.png", DateTime.Now);
string browser = "IE";
int windowWidth = 1024;
int imageWidth = 64;
int imageHeight = imageWidth;
int wait = 0;
int timeout = 20;
try {
//process arguments
char[] removeQuotes = new char[] { '"' };
foreach (var arg in args) {
if (arg.ToLower().StartsWith("-url=")) url = arg.Split('=')[1].Trim(removeQuotes);
if (arg.ToLower().StartsWith("-out=")) outPath = arg.Split('=')[1].Trim(removeQuotes);
if (arg.ToLower().StartsWith("-wait=")) wait = int.Parse(arg.Split('=')[1]);
if (arg.ToLower().StartsWith("-timeout=")) timeout = int.Parse(arg.Split('=')[1]);
if (arg.ToLower().StartsWith("-windowwidth=")) windowWidth = int.Parse(arg.Split('=')[1]);
if (arg.ToLower().StartsWith("-imagewidth=")) imageWidth = int.Parse(arg.Split('=')[1]);
imageHeight = imageWidth;
if (arg.ToLower().StartsWith("-imageheight=")) imageHeight = int.Parse(arg.Split('=')[1]);
if (arg.ToLower().StartsWith("-browser=")) browser = arg.Split('=')[1];
}
//create image
CreateImage(url, outPath, windowWidth, imageWidth, imageHeight, wait, timeout, browser);
}
catch (Exception ex) {
Console.Error.WriteLine(ex.Message);
return 1;
}
}
#if DEBUG
Console.ReadKey();
#endif
return 0;
}
static void Help() {
Console.WriteLine(@"WebShot (Alpha)
Usage: WebShot URL [Options]
Options:
-ImageWidth=64 ... image width in pixels
-ImageHeight=64 ... image height in pixels
-WindowWidth=1024 ... width of the browser window in pixels
-Out=.\WebShot_20160831_153000_000.png ... path of output file
-Timeout=20 ... wait max. 20 seconds for page to load
-Wait=0 ... after loading page wait 0 seconds before taking screenshot
-Browser=IE ... browser (needs to be installed)
");
}
static void CreateImage(string Url, string OutPath, int WindowWidth, int ImageWidth, int ImageHeight, int Wait, int TimeOut, string Browser) {
float ratio = (float)ImageWidth / (float)ImageHeight;
IWebDriver browser = null;
try {
browser = GetDriver(Browser, new Size(WindowWidth, (int)(WindowWidth / ratio)), TimeOut);
//load url
browser.Navigate().GoToUrl(Url);
if (Wait > 0) {
Thread.Sleep(Wait * 1000);
}
//take screenshot
Screenshot ss = ((ITakesScreenshot)browser).GetScreenshot();
//proces image
using (MemoryStream ms = new MemoryStream(ss.AsByteArray)) {
using (Image image = Image.FromStream(ms)) {
SaveImage(image, OutPath, new Size(ImageWidth, ImageHeight));
}
}
}
catch (Exception) {
throw;
}
finally {
if (browser != null) {
browser.Quit();
browser.Dispose();
}
}
}
static IWebDriver GetDriver(string Name, Size WindowsSize, int TimeOut) {
IWebDriver driver = null;
switch (Name.ToLower()) {
case "ff":
case "firefox":
string ffPath = ConfigurationManager.AppSettings["FireFoxPath"];
if (!string.IsNullOrEmpty(ffPath)) {
}
driver = new FirefoxDriver(new FirefoxBinary(@"C:\Program Files\Mozilla Firefox\firefox.exe"), new FirefoxProfile());
break;
case "crome":
driver = new ChromeDriver();
break;
case "ie":
case "internetexplorer":
default:
driver = new InternetExplorerDriver();
break;
}
driver.Manage().Window.Size = WindowsSize;
driver.Manage().Timeouts().SetPageLoadTimeout(TimeSpan.FromSeconds(TimeOut));
return driver;
}
static void SaveImage(Image image, String FilePath) {
image.Save(FilePath, GetImageFormat(FilePath));
}
static void SaveImage(Image image, String FilePath, Size size) {
Image resized = new Bitmap(size.Width, size.Height);
Graphics g = Graphics.FromImage(resized);
g.CompositingQuality = CompositingQuality.HighQuality;
g.SmoothingMode = SmoothingMode.HighQuality;
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
g.DrawImage(image, 0, 0, size.Width, size.Height);
resized.Save(FilePath, GetImageFormat(FilePath));
}
static ImageFormat GetImageFormat(string FilePath) {
switch (Path.GetExtension(FilePath).ToLower()) {
case "png":
return ImageFormat.Png;
case "gif":
return ImageFormat.Gif;
default:
return ImageFormat.Jpeg;
}
}
}
}