我在c#hands中使用屏幕捕获方法。在本地项目中一切正常。但是当我将我的应用程序发布到live domain时,我正在拍摄黑屏。
我正在使用以下两种方式进行活动窗口捕获 1)http://www.tutorialized.com/tutorial/How-to-capture-web-page-screenshot-in-asp.net-using-c/67952 2)http://www.c-sharpcorner.com/article/screen-capture-and-save-as-an-image/
以上方法在本地项目中运行良好。但是在发布截图后保存到驱动器但是使用黑色(空)。
您的回答将不胜感激。
答案 0 :(得分:8)
The projects are intended to be used on a desktop environment, not on a web page (project on the first link is completely wrong!).
You get a black screen because you capture the screen where the application is running, so you get the screen of the server, and if there is no user logged in, the screen is black!
You can't use this code to capture remote web client screen.
try to use the WebBrowser control instead and render the result on a bitmap:
<asp:TextBox ID="txtUrl" runat="server" Text = "" />
<asp:Button Text="Capture" runat="server" OnClick="Capture" />
<br />
<asp:Image ID="imgScreenShot" runat="server" Height="300" Width="400" Visible = "false" />
code-behind:
using System.IO;
using System.Drawing;
using System.Threading;
using System.Windows.Forms;
protected void Capture(object sender, EventArgs e)
{
string url = txtUrl.Text.Trim();
Thread thread = new Thread(delegate()
{
using (WebBrowser browser = new WebBrowser())
{
browser.ScrollBarsEnabled = false;
browser.AllowNavigation = true;
browser.Navigate(url);
browser.Width = 1024;
browser.Height = 768;
browser.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(DocumentCompleted);
while (browser.ReadyState != WebBrowserReadyState.Complete)
{
System.Windows.Forms.Application.DoEvents();
}
}
});
thread.SetApartmentState(ApartmentState.STA);
thread.Start();
thread.Join();
}
private void DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
WebBrowser browser = sender as WebBrowser;
using (Bitmap bitmap = new Bitmap(browser.Width, browser.Height))
{
browser.DrawToBitmap(bitmap, new Rectangle(0, 0, browser.Width, browser.Height));
using (MemoryStream stream = new MemoryStream())
{
bitmap.Save(stream, System.Drawing.Imaging.ImageFormat.Png);
byte[] bytes = stream.ToArray();
imgScreenShot.Visible = true;
imgScreenShot.ImageUrl = "data:image/png;base64," + Convert.ToBase64String(bytes);
}
}
}
答案 1 :(得分:1)
实际问题似乎是“如何使用Javascript捕获屏幕截图”?在之前的SO问题Using HTML5/Canvas/JavaScript to take screenshots中已经回答了这个问题。答案使用html2canvas库来读取页面的DOM并使用Canvas渲染图像。
以下代码将捕获当前页面并将结果发布在页面底部:
html2canvas(document.body, {
onrendered: function(canvas) {
document.body.appendChild(canvas);
}
});
要渲染其他元素,请将其传递给html2canvas而不是document.body
:
html2canvas(someElement,...);
渲染IFRAME虽然可能是一个问题,因为它的内容不是页面的一部分,因此是DOM。 Javascript根本无法访问其内容。这是answered in this SO question,并被视为html2canvas
的限制。