利用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; } }
作者:yjq
欢迎任何形式的转载,但请务必注明出处。
.netcore相关功能需要定制的可以找我。有丰富的并发处理,性能优化经验。单点登录,限流,熔断,读写分离等功能均可定制。也可以帮忙系统优化处理,系统诊断,请联系博主(备注添加原因)。微信:yjq425527169 QQ:425527169
本篇文章如有些许帮助请点击推荐让更多需要帮助的人可以看到,请支持原创,请大方打赏(右边点击打赏)
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· 展开说说关于C#中ORM框架的用法!
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?