[英]“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.