【问题标题】:"The handle is invalid" error when getting screenshot in ASP.NET在 ASP.NET 中获取屏幕截图时出现“句柄无效”错误
【发布时间】:2015-02-10 13:11:40
【问题描述】:

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

此代码在本地机器上运行。但是当我在服务器上发布时它不起作用。我收到“句柄无效”错误。

我的错误在哪里?

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

【问题讨论】:

    标签: c# asp.net .net handle


    【解决方案1】:

    您不能使用Screens 类,因为 ASP.NET 未在桌面交互模式下运行。您无法通过 ASP.NET 中的代码从用户或服务器访问屏幕。

    您应该最多恢复为client side javascript 以获取当前网页的屏幕截图。这是你能做的最好的。 (请不要参加 ActiveX 之旅)

    【讨论】:

    • 有什么方法可以做到吗?
    • 客户端点击按钮时无法获取客户端机器截图。 ?感谢帕特里克提供的新信息
    • 您是否点击了我回答中的链接?它使用 html5 和 canvas 元素来获取屏幕截图。
    • @Ganesh_Devlekar:给我一个理由。
    • @Ganesh_Devlekar:它只适用于 Windows。它仅适用于允许它的公司(无)。这是老式的浏览器内黑客攻击。永远不要再使用它!
    【解决方案2】:

    此代码有效。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-05-05
      • 2019-10-12
      • 1970-01-01
      • 1970-01-01
      • 2016-10-22
      • 1970-01-01
      • 2018-12-23
      相关资源
      最近更新 更多