【问题标题】:How to take a snapshot of webpage from the url?如何从网址拍摄网页快照?
【发布时间】:2019-12-19 16:49:38
【问题描述】:

我想从 url 中截取网页的快照。 url 是一个动态的 html 网页。基本上我们需要那个网页的图片。

我想用 c# 将 html 页面转换为图像,但没有用。 我首先使用 streamreader 读取 html 并使用 NReco.ImageGenerator 尝试转换为字节,最后转换为图像。这不起作用。 最后,我尝试使用 html 网页内部的 javascript 将 html 转换为画布。

function report() {
let region = document.querySelector("body");
 html2canvas(
 $('body'),
 {allowTaint: true, logging: true,'onrendered': function (canvas) 
 {}}).then( //getting problem here at then
 function (canvas) {
 let jpgUrl = canvas.toDataURL();
  console.log(jpgUrl);
  var text = "bottom-right Brochure1";
 var imageName = text + '.jpg';
 download(jpgUrl,imageName, "image/png");
 }

代码解释 - 它将使用 js 对 html 页面中的 body 元素进行快照。获取 url 并从 url 创建画布。并自动下载。但我面临一个问题-----“html2canvas(...),then is not a function”..我不知道它为什么会发生。请帮忙。

【问题讨论】:

  • 谢谢 .. 但这对我不起作用。我希望仅从 html url 中获取快照。
  • 抱歉,既然你提到了 C#,我以为你是在尝试做这个服务器端
  • 没问题。它的js
  • 嗯,html2canvas是第三方库,那你是不是在使用前先把它添加到页面中了?
  • 尝试通过document.body 而不是$('body')。他们的文档根本没有提到 jQuery

标签: javascript c# html canvas html2canvas


【解决方案1】:

由于所有现代浏览器中的跨域资源共享 (CORS) 限制,这不能仅在客户端完成。你需要服务器端的东西来完成这个。要在服务器上使用 javascript 执行此操作,请使用 NodeJS,有几个 npm 包可以提供帮助,例如:node-server-screenshot、PhantomJS 等

【讨论】:

    猜你喜欢
    • 2010-11-07
    • 2017-12-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-08
    相关资源
    最近更新 更多