如何使用asp.net和phantomjs拍摄页面的屏幕截图并将其返回给客户端

zod*_*zod 2 javascript c# asp.net phantomjs

我希望能够阅读网站的屏幕截图,并尝试使用phantomjs和ASP.NET.

我尝试过使用page.render将屏幕截图保存到文件中.它作为控制台应用程序,但不是我从asp.net处理程序调用它.这可能是由于文件权限,因为简单的应用程序(如hello.js)工作正常.

没关系,我的首选不是写入文件,而是处理字节并直接从处理程序返回图像.

我有点迷失如何做到这一点.我注意到一个名为page.renderBase64的方法,但不知道如何使用它.

目前我正在使用IHttpHandler.

这里有一个类似的问题,但那个人最终放弃了phantomjs.我喜欢它的外观,并希望尽可能继续使用它. 使用C#运行Phantomjs以获取网页快照

And*_*sko 10

根据您的上一条评论,您可以在phantom js文件中执行以下操作:

var base64image = page.renderBase64('PNG');
system.stdout.write(base64image);
Run Code Online (Sandbox Code Playgroud)

在C#中:

    var startInfo = new ProcessStartInfo {
    //some other parameters here
    ...
    FileName = pathToExe,
    Arguments = String.Format("{0}",someParameters),
    UseShellExecute = false,
    CreateNoWindow = true,
    RedirectStandardOutput = true,
    RedirectStandardError = true,
    RedirectStandardInput = true,
    WorkingDirectory = pdfToolPath
    };
    var p = new Process();
    p.StartInfo = startInfo;
    p.Start();
    p.WaitForExit(timeToExit);
    //Read the Error:
    string error = p.StandardError.ReadToEnd();
    //Read the Output:
    string output = p.StandardOutput.ReadToEnd();
Run Code Online (Sandbox Code Playgroud)

输出变量中,您可以读取从phantomJS返回的base64,然后执行您计划的内容.