繁体   English   中英

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

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

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

该代码在本地计算机上有效。但是当我在服务器上发布时,该代码无效。 我收到“句柄无效”错误。

我的错误在哪里?

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类,因为ASP.NET不在桌面交互模式下运行。 您无法从用户或服务器从ASP.NET中的代码访问屏幕。

您应该最多恢复到客户端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;

    }

暂无
暂无

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

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