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. UVALive 4490 Help Bubu

    题目大意:有n本书,高度值域为8,现可以把k本书拿出来再放进去,相邻的.高度相同的书算作一块,最小化块的个数.n=100. 强烈建议大家不要在做完区间DP后做别的DP题:区间DP是整体考虑,而一般DP ...

  2. bzoj 2588 Count on a tree

    Description 给定一棵N个节点的树,每个点有一个权值,对于M个询问(u,v,k),你需要回答u xor lastans和v这两个节点间第K小的点权.其中lastans是上一个询问的答案,初始 ...

  3. bzoj 4012: [HNOI2015]开店 主席树

    Description 风见幽香有一个好朋友叫八云紫,她们经常一起看星星看月亮从诗词歌赋谈到 人生哲学.最近她们灵机一动,打算在幻想乡开一家小店来做生意赚点钱.这样的 想法当然非常好啦,但是她们也发现 ...

  4. Oracle 用户操作表权限

    grant select any table to xxx 将使得xxx用户能够查看到所有用户的表:正确的授权不能是这样: 用户是隔离表的schema,授权时..

  5. struts2 使用filter解决中文乱码问题

    1.编写fliter的代码 import java.io.IOException;import javax.servlet.Filter;import javax.servlet.FilterChai ...

  6. SQL奇技淫巧

    1.SQL行列转换 问题:假设有张学生成绩表(tb)如下:姓名 课程 分数张三 语文 74张三 数学 83张三 物理 93李四 语文 74李四 数学 84李四 物理 94想变成(得到如下结果): 姓名 ...

  7. 学习整理与细化(2)——HTML VS XHTML

    <html> <head>//文档头 <title>webpage title</title> </head> <body>// ...

  8. grpc介绍

    grpc入门(一) 一.什么是grpc grpc是谷歌开源的一款高性能的rpc框架 (https://grpc.io),可以使用protocol buffers作为IDL(Interface Defi ...

  9. rpc之thrift

    rpc之thrift 一.介绍 thrift是一个rpc(remove procedure call)框架,可以实现不同的语言(java.c++.js.python.ruby.c#等)之间的相互调用. ...

  10. 一秒搞定mysql的远程登录

    执行下面命令! mysql> GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'mypw' WITH GRANT OPTION ; ...