简体   繁体   English

使用Win32 API(C#pInvoke)截屏在IIS中不起作用

[英]Taking screenshot with Win32 API (C# pInvoke) not working in IIS

I want to take a screenshot of an external website. 我想拍摄一个外部网站的屏幕截图。 Currently my workflow is to start a Firefox instance with the specified URL and take a screenshot using PrintWindow in the Win32 API 目前,我的工作流程是使用指定的URL启动Firefox实例,并使用Win32 API中的PrintWindow截屏。

When I run this application in IISExpress it works fine but when I run the same application in IIS on a windows VPS it is not working (screenshot is blank). 当我在IISExpress中运行此应用程序时,它可以正常工作,但是当我在Windows VPS上的IIS中运行相同的应用程序时,则无法正常工作(屏幕截图为空白)。

I don't know what I'm doing wrong? 我不知道我在做什么错?

My code: 我的代码:

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

EDIT: Running Firefox works fine because the AppPool is under another account that has the rights to execute firefox. 编辑:运行Firefox可以正常工作,因为AppPool在另一个有权执行Firefox的帐户下。

使用IIS,您无法进行屏幕截图,因为它可以在Windows服务器的NetworkService帐户下使用

What screen are you taking a screenshot of? 您要在哪个屏幕上截屏?

If an application is running on a laptop it will paint to a physical display adaptor. 如果应用程序在笔记本电脑上运行,它将绘制到物理显示器适配器上。 If it is running on a machine at the end of an RDP session it will paint to a virtual display adaptor, and the image will be sent over the wire. 如果它在RDP会话结束时在计算机上运行,​​它将绘制到虚拟显示适配器,并且图像将通过网络发送。

But if an application is running in a non-interactive session then there is no display. 但是,如果应用程序在非交互式会话中运行,则不会显示任何内容。 The application won't receive WM_PAINT messages so nothing gets painted. 该应用程序将不会收到WM_PAINT消息,因此不会绘制任何内容。

There is no screen bitmap to capture. 没有要捕获的屏幕位图。

IIS service donot have the access to the server desktop. IIS服务无权访问服务器桌面。 The code of ASP.NET in IIS is running on the server side. IIS中的ASP.NET代码在服务器端运行。 And it will be trying to capture the screen of the server, which is denied. 并且它将尝试捕获被拒绝的服务器屏幕。

You will need to use the silverlight which runs the code on the client machine. 您将需要使用Silverlight在客户端计算机上运行代码。 Following link might help: 以下链接可能会有所帮助:

Capturing a Silverlight Screen 捕获Silverlight屏幕

User using the webpage in the browser might need to provide permissions due to security reasons. 由于安全原因,在浏览器中使用网页的用户可能需要提供权限。

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

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