簡體   English   中英

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

[英]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在客戶端計算機上運行代碼。 以下鏈接可能會有所幫助:

捕獲Silverlight屏幕

由於安全原因,在瀏覽器中使用網頁的用戶可能需要提供權限。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM