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(, , 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(, , soourceImage);
if (cutedImage != null)
{
cutedImage.Save(AppDomain.CurrentDomain.BaseDirectory + "//" + i.ToString() + ".jpg", ImageFormat.Jpeg);
}
}
}
});
for (int i = ; i < ; 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 = ;
int targetStartX = ;
int sourceStartY = ;
int targetStartY = ;
int sourceEndX = ;
int targetEndX = ;
int sourceEndY = ;
int targetEndY = ;
if (sourceWidth <= targetWidth)
{
sourceStartX = ;
targetStartX = (targetWidth - sourceWidth) / ;
sourceEndX = sourceWidth;
targetEndX = sourceWidth + targetStartX;
}
else
{
sourceStartX = (sourceWidth - targetWidth) / ;
targetStartX = ;
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 实现截图的更多相关文章

  1. python+Selenium PhantomJS网页截图

    PhantomJS是一个基于webkit的JavaScript API.它使用QtWebKit作为它核心浏览器的功能,使用webkit来编译解释执行JavaScript代码.任何你可以在基于webki ...

  2. [Python爬虫] 之一 : Selenium+Phantomjs动态获取网站数据信息

    本人刚才开始学习爬虫,从网上查询资料,写了一个利用Selenium+Phantomjs动态获取网站数据信息的例子,当然首先要安装Selenium+Phantomjs,具体的看 http://www.c ...

  3. [Python爬虫] 之二十五:Selenium +phantomjs 利用 pyquery抓取今日头条网数据

    一.介绍 本例子用Selenium +phantomjs爬取今日头条(http://www.toutiao.com/search/?keyword=电视)的资讯信息,输入给定关键字抓取资讯信息. 给定 ...

  4. [Python爬虫] 之二十二:Selenium +phantomjs 利用 pyquery抓取界面网站数据

    一.介绍 本例子用Selenium +phantomjs爬取界面(https://a.jiemian.com/index.php?m=search&a=index&type=news& ...

  5. [Python爬虫] 之二十一:Selenium +phantomjs 利用 pyquery抓取36氪网站数据

    一.介绍 本例子用Selenium +phantomjs爬取36氪网站(http://36kr.com/search/articles/电视?page=1)的资讯信息,输入给定关键字抓取资讯信息. 给 ...

  6. [Python爬虫] 之二十:Selenium +phantomjs 利用 pyquery通过搜狗搜索引擎数据

    一.介绍 本例子用Selenium +phantomjs 利用 pyquery通过搜狗搜索引擎数据()的资讯信息,输入给定关键字抓取资讯信息. 给定关键字:数字:融合:电视 抓取信息内如下: 1.资讯 ...

  7. [Python爬虫] 之三十一:Selenium +phantomjs 利用 pyquery抓取消费主张信息

    一.介绍 本例子用Selenium +phantomjs爬取央视栏目(http://search.cctv.com/search.php?qtext=消费主张&type=video)的信息(标 ...

  8. [Python爬虫] 之三十:Selenium +phantomjs 利用 pyquery抓取栏目

    一.介绍 本例子用Selenium +phantomjs爬取栏目(http://tv.cctv.com/lm/)的信息 二.网站信息 三.数据抓取 首先抓取所有要抓取网页链接,共39页,保存到数据库里 ...

  9. [Python爬虫] 之二十九:Selenium +phantomjs 利用 pyquery抓取节目信息信息

    一.介绍 本例子用Selenium +phantomjs爬取节目(http://tv.cctv.com/epg/index.shtml?date=2018-03-25)的信息 二.网站信息 三.数据抓 ...

随机推荐

  1. ReactNative实现图集功能

    需求描述: 图片缩放.拖动.长按保存等基础图片查看的功能: 展示每张图片文本描述: 实现效果,如图: 实现步骤 使用第三方插件:react-native-image-zoom-viewer 插件Git ...

  2. 童话故事 --- CPU的贴身侍卫ITCM和ICache

    "叮铃铃- 叮铃铃-" "谁呀?"黛丝博士打开了家门,"哇,高飞,你怎么来了?" 高飞狗:"好久不见,想来看看你,还买了你最喜欢吃 ...

  3. 深入理解用户权限rwx

    其实在UNIX的实现中,文件权限用12个二进制位表示,如果该位置上的值是1,表示有相应的权限,如果是0则没有相应权限第11位为SUID位,第10位为SGID位,第9位为sticky位,第8-0位对应于 ...

  4. 安装supervisord

    一:简介 supervisord是一个进程管理工具,提供web页面管理,能对进程进行自动重启等操作. 优点: - 可以将非后台运行程序后台运行 - 自动监控,重启进程 缺点: - 不能管理后台运行程序 ...

  5. glibc-commons 依赖解析 版本错误,xxx is duplicate yyy

    glibc-commons 安装了两个版本,导致依赖glibc-commons的很多软件包 被安装了两个版本: 解决办法就是 先清除这些重复的已安装的软件,然后执行 yum update 将 glib ...

  6. Spark监控官方文档学习笔记

    任务的监控和使用 有几种方式监控spark应用:Web UI,指标和外部方法 Web接口 每个SparkContext都会启动一个web UI,默认是4040端口,用来展示一些信息: 一系列调度的st ...

  7. IE8下实现兼容rgba

    昨天遇到一个问题,要实现一个背景透明的效果,用CSS3用rgba()就能实现,即 background: rgba(0,0,0,.5); 但是要兼容到IE8,就发现没有透明效果,因为IE8不支持rgb ...

  8. JavaScript的DOM编程--09--节点的替换

    节点的替换: 1). replaceChild(): 把一个给定父元素里的一个子节点替换为另外一个子节点 var reference = element.replaceChild(newChild,o ...

  9. CSS开发规范

    虽然很久之前整理过一份简单的CSS规范,但是当时写的也不是很全面,有些细节也没有照顾到.记录一份较详细的版本,以备不时之需. 命名规范 [强制] class一律使用小写字母+下划线格式命名 例: cl ...

  10. CSS学习之首页简单布局

    作为一个PHPer,在前端方面javascript.jquery这些的日常工作还搞的定.可对于div+css这些东西可就头疼了,所以现在开始学习CSS 跟着燕十八的教程开始从最基础学起,首先练习一个简 ...