首页 > 代码库 > 利用Selenium+PhantomJS 实现截图
利用Selenium+PhantomJS 实现截图
using OpenQA.Selenium;using OpenQA.Selenium.PhantomJS;using System;using System.Drawing;using System.Drawing.Imaging;using System.IO;using System.Threading.Tasks;namespace WebKitTest{ internal class Program { private static void Main(string[] args) { Parallel.For(0, 20, i => { var url = "http://www.baidu.com"; IWebDriver driver = new PhantomJSDriver(GetPhantomJSDriverService()); driver.Navigate().GoToUrl(url); ITakesScreenshot screenshotDriver = driver as ITakesScreenshot; Screenshot screenshot = screenshotDriver.GetScreenshot(); using (MemoryStream stream = new MemoryStream(screenshot.AsByteArray)) { using (var soourceImage = Image.FromStream(stream)) { var cutedImage = GetReducedImage(800, 800, soourceImage); if (cutedImage != null) { cutedImage.Save(AppDomain.CurrentDomain.BaseDirectory + "//" + i.ToString() + ".jpg", ImageFormat.Jpeg); } } } }); for (int i = 0; i < 100; i++) { } Console.Read(); } private static PhantomJSDriverService GetPhantomJSDriverService() { PhantomJSDriverService pds = PhantomJSDriverService.CreateDefaultService(); //设置代理服务器地址 //pds.Proxy = $"{ip}:{port}"; //设置代理服务器认证信息 //pds.ProxyAuthentication = GetProxyAuthorization(); return pds; } /// <summary> /// 生成缩略图重载方法1,返回缩略图的Image对象 /// </summary> /// <param name="targetWidth">缩略图的宽度</param> /// <param name="targetHeight">缩略图的高度</param> /// <returns>缩略图的Image对象</returns> public static Image GetReducedImage(int targetWidth, int targetHeight, Image sourceImage) { try { //用指定的大小和格式初始化Bitmap类的新实例 Bitmap bitmap = new Bitmap(targetWidth, targetHeight, PixelFormat.Format32bppArgb); //从指定的Image对象创建新Graphics对象 using (Graphics graphics = Graphics.FromImage(bitmap)) { //清除整个绘图面并以透明背景色填充 graphics.Clear(Color.Transparent); //在指定位置并且按指定大小绘制原图片对象 int sourceWidth = sourceImage.Width; int sourceHeight = sourceImage.Height; int sourceStartX = 0; int targetStartX = 0; int sourceStartY = 0; int targetStartY = 0; int sourceEndX = 0; int targetEndX = 0; int sourceEndY = 0; int targetEndY = 0; if (sourceWidth <= targetWidth) { sourceStartX = 0; targetStartX = (targetWidth - sourceWidth) / 2; sourceEndX = sourceWidth; targetEndX = sourceWidth + targetStartX; } else { sourceStartX = (sourceWidth - targetWidth) / 2; targetStartX = 0; sourceEndX = targetWidth + sourceStartX; targetEndX = targetWidth; } if (targetEndX - sourceStartX > targetWidth) { targetEndX = targetWidth - sourceStartX; } if (sourceHeight <= targetHeight) { sourceEndY = sourceHeight; targetEndY = sourceHeight; } else { sourceEndY = targetHeight; targetEndY = targetHeight; } Rectangle destRect = new Rectangle(targetStartX, targetStartY, targetEndX - targetStartX, targetEndY - targetStartY);//矩形容器 Rectangle srcRect = new Rectangle(sourceStartX, sourceStartY, sourceEndX - sourceStartX, sourceEndY - sourceStartY); graphics.DrawImage(sourceImage, destRect, srcRect, GraphicsUnit.Pixel); return bitmap; } } catch (Exception e) { return null; } } }}
private static void Main(string[] args) { Parallel.For(0, 20, i => { var url = "http://www.baidu.com"; IWebDriver driver = new PhantomJSDriver(GetPhantomJSDriverService()); driver.Navigate().GoToUrl(url); ITakesScreenshot screenshotDriver = driver as ITakesScreenshot; Screenshot screenshot = screenshotDriver.GetScreenshot(); using (MemoryStream stream = new MemoryStream(screenshot.AsByteArray)) { using (var soourceImage = Image.FromStream(stream)) { var cutedImage = GetReducedImage(800, 800, soourceImage); if (cutedImage != null) { cutedImage.Save(AppDomain.CurrentDomain.BaseDirectory + "//" + i.ToString() + ".jpg", ImageFormat.Jpeg); } } } }); for (int i = 0; i < 100; i++) { } Console.Read(); }
private static PhantomJSDriverService GetPhantomJSDriverService() { PhantomJSDriverService pds = PhantomJSDriverService.CreateDefaultService(); //设置代理服务器地址 //pds.Proxy = $"{ip}:{port}"; //设置代理服务器认证信息 //pds.ProxyAuthentication = GetProxyAuthorization(); return pds; }
/// <summary> /// 生成缩略图重载方法1,返回缩略图的Image对象 /// </summary> /// <param name="targetWidth">缩略图的宽度</param> /// <param name="targetHeight">缩略图的高度</param> /// <returns>缩略图的Image对象</returns> public static Image GetReducedImage(int targetWidth, int targetHeight, Image sourceImage) { try { //用指定的大小和格式初始化Bitmap类的新实例 Bitmap bitmap = new Bitmap(targetWidth, targetHeight, PixelFormat.Format32bppArgb); //从指定的Image对象创建新Graphics对象 using (Graphics graphics = Graphics.FromImage(bitmap)) { //清除整个绘图面并以透明背景色填充 graphics.Clear(Color.Transparent); //在指定位置并且按指定大小绘制原图片对象
int sourceWidth = sourceImage.Width; int sourceHeight = sourceImage.Height; int sourceStartX = 0; int targetStartX = 0; int sourceStartY = 0; int targetStartY = 0; int sourceEndX = 0; int targetEndX = 0; int sourceEndY = 0; int targetEndY = 0; if (sourceWidth <= targetWidth) { sourceStartX = 0; targetStartX = (targetWidth - sourceWidth) / 2; sourceEndX = sourceWidth; targetEndX = sourceWidth + targetStartX; } else { sourceStartX = (sourceWidth - targetWidth) / 2; targetStartX = 0; sourceEndX = targetWidth + sourceStartX; targetEndX = targetWidth; } if (targetEndX - sourceStartX > targetWidth) { targetEndX = targetWidth - sourceStartX; }
if (sourceHeight <= targetHeight) { sourceEndY = sourceHeight; targetEndY = sourceHeight; } else { sourceEndY = targetHeight; targetEndY = targetHeight; } Rectangle destRect = new Rectangle(targetStartX, targetStartY, targetEndX - targetStartX, targetEndY - targetStartY);//矩形容器 Rectangle srcRect = new Rectangle(sourceStartX, sourceStartY, sourceEndX - sourceStartX, sourceEndY - sourceStartY);
graphics.DrawImage(sourceImage, destRect, srcRect, GraphicsUnit.Pixel); return bitmap; } } catch (Exception e) { return null; } }
利用Selenium+PhantomJS 实现截图