简体   繁体   English

在ASP.NET中获取屏幕截图时出现“句柄无效”错误

[英]“The handle is invalid” error when getting screenshot in ASP.NET

I tried get screenshot of client user when client user click a button. 当客户端用户单击按钮时,我尝试获取客户端用户的屏幕截图。

This code is working on local machine.But it is not working when I published on a server. 该代码在本地计算机上有效。但是当我在服务器上发布时,该代码无效。 I get "The handle is invalid" error. 我收到“句柄无效”错误。

Where is my mistake ? 我的错误在哪里?

using System;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Windows.Forms;
using System.Drawing.Imaging;
using System.Drawing;

public partial class Page: System.Web.UI.Page

{
    protected void Page_Load(object sender, EventArgs e) {...}

}

static string ImagePath;
public static void ScreenShot()
{


 foreach (Screen screen in Screen.AllScreens)
        {
            Bitmap bitmap = new Bitmap(screen.Bounds.Width, screen.Bounds.Height, PixelFormat.Format32bppArgb);

            using (Graphics graphics = Graphics.FromImage(bitmap))
            {
                graphics.CopyFromScreen(screen.Bounds.X, screen.Bounds.Y, 0, 0, screen.Bounds.Size, CopyPixelOperation.SourceCopy);
            }

            ImagePath= "/Screenshots/" + HttpContext.Current.Session["UserName"].ToString()+ " "+ DateTime.Now.ToString("dd.MM.yyyy HH.mm.ss");
            bitmap.Save(HttpContext.Current.Server.MapPath(ImagePath+ ".jpg"));
        }

}

   protected void btn_Click(object sender, EventArgs e)
   {
     ScreenShot(); 
   }

You can't use the Screens class, since ASP.NET is not running in a desktop interactive mode. 您不能使用Screens类,因为ASP.NET不在桌面交互模式下运行。 You can't access the screen from the user, nor the server, from your code in ASP.NET. 您无法从用户或服务器从ASP.NET中的代码访问屏幕。

You should revert at best to client side javascript to get a screenshot of the current web page. 您应该最多恢复到客户端javascript,以获取当前网页的屏幕截图。 That's the best you can do. 那是你所能做的最好的。 (Please don't go on the ActiveX tour) (请不要参加ActiveX导览)

this code works. 此代码有效。

  public static Bitmap Capture()
    {
        Bitmap bitmap = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height);

        using (Graphics graphics = Graphics.FromImage(bitmap))
        {
            graphics.CopyFromScreen( 100, 100, 100, 100, bitmap.Size);
        }

        return bitmap;

    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM