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. 搭建lnmp教程

    LNMP指的是一个基于CentOS/Debian 上安装Nginx.PHP.MySQL.php.可以在独立主机上轻松的安装LNMP生产环境. 1 安装nginx 如果是一台新的服务器可直接安装(若以前 ...

  2. Hibernate缓存和状态

    缓存是介于应用程序和物理数据源之间,其作用是为了降低应用程序对物理数据源访问的频次,从而提高了应用的运行性能.   缓存的介质一般是内存,所以读写速度很快.但如果缓存中存放的数据量非常大时,也会用硬盘 ...

  3. Java 包装类Integer的值比较

    对于包装类型Integer的值比较与int的值比较是不同的:   public class Java_Val_Compare { public static void main(String[] ar ...

  4. 顶点/片元 shader 总结

    Cg顶点程序必须在结构中传递顶点数据.几种常用的顶点结构定义在文件UnityCG.cginc中,有如下三种结构体: 1.appdata_base: 包含顶点位置,法线和一个纹理坐标.2.appdata ...

  5. mysql数据库出现2003-Can't connect to MySQL server on 'localhost' (10061)的解决方法

    1.右键点击我的电脑,找到管理! 2.找到服务和应用程序: 3.打开找到服务,打开: 4.在服务里找到MySQL,改成启动:

  6. Git详解之六:Git工具

    Git 工具 现在,你已经学习了管理或者维护 Git 仓库,实现代码控制所需的大多数日常命令和工作流程.你已经完成了跟踪和提交文件的基本任务,并且发挥了暂存区和轻量级的特性分支及合并的威力.(伯乐在线 ...

  7. 使用plenv安装perl,并使其支持多线程

    plenv与pyenv.rbenv等都是同类型软件中非常好用的,这三个软件不仅命名类似,操作方式也相差无几,节约了很多学习的成本,所以非常推荐: 安装使用plenv: git clone git:// ...

  8. 深入了解CSS字体度量,行高和vertical-align

    line-height和vertical-align在CSS中是两个简单的属性.如此简单,大多数人都相信自己已经完全理解它们是如何工作的以及如何使用它们.但事实上并不如此.他们其实很复杂,也是CSS中 ...

  9. 【漏洞分析】dedecms有前提前台任意用户密码修改

     0x00 前言 早上浏览sec-news,发现锦行信息安全发布了一篇文章<[漏洞分析] 织梦前台任意用户密码修改>,看完之后就想着自己复现一下. 该漏洞的精髓是php的弱类型比较,'0. ...

  10. 微信小程序之获取验证码js

    在微信小程序中怎样实现获取验证码的倒计时功能捏,倒计时的原理是一样一样的,就是某些地方需要注意. 第一步:结构 <view class='get-code' wx:if="{{!isS ...