[英]Taking screenshot with Win32 API (C# pInvoke) not working in IIS
我想拍摄一个外部网站的屏幕截图。 目前,我的工作流程是使用指定的URL启动Firefox实例,并使用Win32 API中的PrintWindow截屏。
当我在IISExpress中运行此应用程序时,它可以正常工作,但是当我在Windows VPS上的IIS中运行相同的应用程序时,则无法正常工作(屏幕截图为空白)。
我不知道我在做什么错?
我的代码:
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Imaging;
using System.Linq;
using System.Runtime.InteropServices;
using System.Security;
using System.Threading;
using System.Web;
namespace WebTools.Web
{
public class RemoteScreenshot
{
public static Bitmap TakeScreenshot(Process process)
{
// may need a process Refresh before
return TakeScreenshot(process.MainWindowHandle);
}
public static Bitmap TakeScreenshot(IntPtr handle)
{
RECT rc = new RECT();
GetWindowRect(handle, ref rc);
Bitmap bitmap = new Bitmap(rc.right - rc.left, rc.bottom - rc.top);
using (Graphics graphics = Graphics.FromImage(bitmap))
{
PrintWindow(handle, graphics.GetHdc(), 0);
}
return bitmap;
}
[DllImport("user32.dll")]
private static extern bool GetWindowRect(IntPtr hWnd, ref RECT rect);
[DllImport("user32.dll")]
private static extern bool PrintWindow(IntPtr hWnd, IntPtr hDC, int flags);
[StructLayout(LayoutKind.Sequential)]
private struct RECT
{
public int left;
public int top;
public int right;
public int bottom;
}
public static void Save(string url, string file, int timeout = 0)
{
var proc = Process.Start(new ProcessStartInfo("C:\\Program Files (x86)\\Mozilla Firefox\\firefox.exe", url)
{
CreateNoWindow = false,
WindowStyle = ProcessWindowStyle.Maximized
});
Thread.Sleep(timeout);
var bitmap = TakeScreenshot(proc);
bitmap.Save(file, ImageFormat.Png);
bitmap.Dispose();
proc.Kill();
}
}
}
编辑:运行Firefox可以正常工作,因为AppPool在另一个有权执行Firefox的帐户下。
使用IIS,您无法进行屏幕截图,因为它可以在Windows服务器的NetworkService帐户下使用
您要在哪个屏幕上截屏?
如果应用程序在笔记本电脑上运行,它将绘制到物理显示器适配器上。 如果它在RDP会话结束时在计算机上运行,它将绘制到虚拟显示适配器,并且图像将通过网络发送。
但是,如果应用程序在非交互式会话中运行,则不会显示任何内容。 该应用程序将不会收到WM_PAINT消息,因此不会绘制任何内容。
没有要捕获的屏幕位图。
IIS服务无权访问服务器桌面。 IIS中的ASP.NET代码在服务器端运行。 并且它将尝试捕获被拒绝的服务器屏幕。
您将需要使用Silverlight在客户端计算机上运行代码。 以下链接可能会有所帮助:
由于安全原因,在浏览器中使用网页的用户可能需要提供权限。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.