110,566
社区成员
发帖
与我相关
我的任务
分享
if (wb.ReadyState == WebBrowserReadyState.Complete)
这个条件的了。
using System;
using System.Windows.Forms;
using System.Threading;
using System.Diagnostics;
using System.IO;
namespace testss
{
class WebHelper
{
private string link;
private string path;
private string serverPath;
public WebHelper(string link, string path, string serverpath)
{
this.link = link;
this.path = path;
this.serverPath = serverpath;
}
/// <summary>
/// 启用新线程
/// </summary>
public void GetImg()
{
var m_thread = new Thread(GetHtmlImage);
m_thread.SetApartmentState(ApartmentState.STA);
m_thread.Start();
m_thread.Join();
}
/// <summary>
/// 调用截图方法,然后进行等待,一直到图片生成为止
/// </summary>
private void GetHtmlImage()
{
GetImg1(link, path, serverPath);
while (true)
{
if (File.Exists(serverPath + "\\" + path))
{
break;
}
DateTime current = DateTime.Now;
while (current.AddMilliseconds(2000) > DateTime.Now)
{
Application.DoEvents();//转让控制权
}
}
}
/// <summary>
/// 进行截图
/// </summary>
/// <param name="links">截图网页链接</param>
/// <param name="path">截图生成的文件存放的路径及其文件名</param>
/// <param name="serverPath">phantomjs.exe与screenshot.js所在目录的路径</param>
/// <returns></returns>
public bool GetImg1(string links, string path, string serverPath)
{
try
{
Process p = new Process();
p.StartInfo.FileName = serverPath + @"\phantomjs.exe";
// p.StartInfo.FileName = @"E:\SoftWare\phantomjs-2.1.1-windows\bin\phantomjs.exe";
p.StartInfo.WorkingDirectory = serverPath + @"\";
p.StartInfo.Arguments = string.Format(serverPath + @"\test.js " + links + " " + path);
p.StartInfo.CreateNoWindow = true;
p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
p.Start();
return true;
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
// Console.WriteLine(ex.Message);
return false;
}
}
}
}
//WebHelper web = new WebHelper("", "", @"C:\Users\xl\Desktop\testss\testss\Pic");
web.GetImg();
var webPage = require('webpage');
var page = webPage.create();
page.viewportSize = { width: 1920, height: 1080 };
page.open("https://bbs.csdn.net/topics/392422441?page=1#post-403373637", function start(status) {
window.setTimeout(function(){
//var base64 = page.renderBase64('PNG');
//console.log(base64);
page.render('c:/Utils/bin/test.jpeg', {format: 'jpeg', quality: '100'});
phantom.exit();
},5000);
});
static void Main(string[] args)
{
//ClientInit();
System.Diagnostics.Process process = new System.Diagnostics.Process();
process.StartInfo = new System.Diagnostics.ProcessStartInfo(@"cmd.exe", @"/d " + "C: /c /Utils/bin/phantomjs.exe " + "C:/Utils/bin/test.js");
process.StartInfo.FileName = "cmd.exe";
process.StartInfo.UseShellExecute = false; //是否使用操作系统shell启动
process.StartInfo.RedirectStandardInput = true;//接受来自调用程序的输入信息
process.StartInfo.RedirectStandardOutput = true;//由调用程序获取输出信息
process.StartInfo.RedirectStandardError = true;//重定向标准错误输出
process.StartInfo.CreateNoWindow = false;//不显示程序窗口
try
{
/*
* 异步调用
*/
process.Start();
}
catch (System.Exception ex)
{
Debug.WriteLine(ex.Message);
}
Console.WriteLine(process.StandardOutput.ReadToEnd());
Console.ReadLine();
}
var webPage = require('webpage');
var page = webPage.create();
page.viewportSize = { width: 1920, height: 1080 };
page.open("https://bbs.csdn.net/topics/392422441?page=1#post-403373637", function start(status) {
window.setTimeout(function(){
var base64 = page.renderBase64('PNG');
console.log(base64);
phantom.exit();
},5000);
});