"The handle is invalid" 在 ASP.NET 中获取屏幕截图时出错

"The handle is invalid" error when getting screenshot in ASP.NET

我尝试在客户端用户单击按钮时获取客户端用户的屏幕截图。

此代码在本地运行 machine.But 当我在服务器上发布时它不起作用。我收到 "The handle is invalid" 错误。

我的错误在哪里?

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(); 
   }

您不能使用 Screens class,因为 ASP.NET 在桌面交互模式下不是 运行。您无法通过 ASP.NET.

中的代码从用户或服务器访问屏幕

您最好将 恢复为 client side javascript 以获得当前网页的屏幕截图。那是你能做的最好的。 (请不要继续ActiveX之旅)

此代码有效。

  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;

    }