简体   繁体   中英

“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. You can't access the screen from the user, nor the server, from your code in ASP.NET.

You should revert at best to client side javascript to get a screenshot of the current web page. That's the best you can do. (Please don't go on the ActiveX tour)

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;

    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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