javascript 截取 iFrame 的屏幕截图
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9362620/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Take a screenshot of an iFrame
提问by Cute Bear
I am looking for a simple way to take a screenshot of an iFrame in my ASP page. I just couldn't achieve it with C# and I lack of knowledge of Javascript! Does anyone out there know the simple and best way to achieve this?
我正在寻找一种简单的方法来在我的 ASP 页面中截取 iFrame 的屏幕截图。我只是无法用 C# 实现它,而且我缺乏 Javascript 的知识!有没有人知道实现这一目标的简单和最佳方法?
What I am trying to do is, I am building a website that students can log in to e-government website in my country and prove if they are continuing student with a single click so that they can get discount from our service.
我想要做的是,我正在建立一个网站,学生可以登录我所在国家/地区的电子政务网站,并通过单击来证明他们是否是继续学生,以便他们可以从我们的服务中获得折扣。
Edit: The puzzle should be solved in local.
编辑:拼图应该在本地解决。
采纳答案by Cute Bear
this piece of code worked for me. I hope it does the same to the others.
这段代码对我有用。我希望它对其他人也一样。
private void saveURLToImage(string url)
{
if (!string.IsNullOrEmpty(url))
{
string content = "";
System.Net.WebRequest webRequest = WebRequest.Create(url);
System.Net.WebResponse webResponse = webRequest.GetResponse();
System.IO.StreamReader sr = new StreamReader(webResponse.GetResponseStream(), System.Text.Encoding.GetEncoding("UTF-8"));
content = sr.ReadToEnd();
//save to file
byte[] b = Convert.FromBase64String(content);
System.IO.MemoryStream ms = new System.IO.MemoryStream(b);
System.Drawing.Image img = System.Drawing.Image.FromStream(ms);
img.Save(@"c:\pic.jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
img.Dispose();
ms.Close();
}
}
回答by Quentin
Unless I'm misunderstanding you, this is impossible.
除非我误解了你,否则这是不可能的。
- You cannot instruct the user's browser to take a screenshot (this would be a security risk … and has few uses cases anyway).
- You cannot load the page you want a screenshot of yourself (with server side code) because you don't have the credentials needed to access it.
- 您不能指示用户的浏览器截取屏幕截图(这会带来安全风险……而且用例很少)。
- 您无法加载您想要自己截图的页面(使用服务器端代码),因为您没有访问它所需的凭据。
回答by Daniel B
server side Take a screenshot of a webpage with JavaScript?
javascript http://html2canvas.hertzen.com/
javascript http://html2canvas.hertzen.com/