如何在 C# 中捕获 Windows Store 应用程序的窗口内容 [英] How to capture window contents of a Windows Store App in C#

查看:37
本文介绍了如何在 C# 中捕获 Windows Store 应用程序的窗口内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些代码来捕获 Windows 桌面应用程序内容并保存到 .NET 中的位图对象.它使用 User32.dll 和 Gdi32.dll (BitBlt) 并且工作得很好.但是,当我为代码提供一个包含 Windows 应用商店应用程序的窗口的句柄时,代码会生成全黑位图.我不确定这是安全功能还是什么.我不能使用 ScreenCapture api,因为窗口的内容在调整大小后几乎总是比屏幕高/大.对于 Windows 应用商店应用,有没有人有幸捕获窗口内容,即使它们比屏幕大?

I have a bit of code to capture windows desktop app contents and save to a Bitmap object in .NET. It uses User32.dll and Gdi32.dll (BitBlt) and works just fine. However, the code produces all-black bitmaps when I give the code a handle to a window that holds a Windows Store app. I'm not sure if this is a security feature or what. I cannot use the ScreenCapture api as the contents of the window, after being resized, are almost always taller/larger than the screen. Has anyone had any luck capturing window contents even when they're larger than the screen, for a Windows Store app?

正如注释一样,我试图捕获不同程序的窗口,而不是我自己的程序.可以假设我的程序是 .NET 4.6.1/C# 中的 Windows 控制台应用程序

Just as a note I am trying to capture a different program's window, not my own program. My program can be assumed to be a Windows Console application in .NET 4.6.1 / C#

此外,我知道这在 Windows API 中一定是可能的,因为 Aero Peek 功能,如果您将鼠标悬停在正在运行的程序图标上的任务栏上,则会显示窗口的完整高度,包括屏幕外组件.(见右侧高窗,设置为 6000px 远高于我的显示)

Also, I know that this must be possible somehow in Windows APIs, because the Aero Peek feature, where if you hover over the taskbar on the running program's icon shows the full height of the window, including offscreen components. (see tall window on right, set to 6000px much higher than my display)

推荐答案

这可能会奏效.基本上是获取应用程序的窗口句柄,调用其上的本机函数来确定应用程序窗口位置,提供图形类并从屏幕复制.

This might do the trick. Basically get the window handle to the app, call the native functions on it to figure out the app window position, provide those do the graphics class and copy from the screen.

class Program
{
    [DllImport("user32.dll", CharSet = CharSet.Auto)]
    public static extern IntPtr FindWindow(string strClassName, string strWindowName);

    [DllImport("user32.dll")]
    public static extern bool GetWindowRect(IntPtr hwnd, ref Rect rectangle);

    public struct Rect
    {
        public int Left { get; set; }
        public int Top { get; set; }
        public int Right { get; set; }
        public int Bottom { get; set; }
    }


    static void Main(string[] args)
    {
        /// Give this your app's process name.
        Process[] processes = Process.GetProcessesByName("yourapp");
        Process lol = processes[0];
        IntPtr ptr = lol.MainWindowHandle;
        Rect AppRect = new Rect();
        GetWindowRect(ptr, ref AppRect);
        Rectangle rect = new Rectangle(AppRect.Left, AppRect.Top, (AppRect.Right - AppRect.Left), (AppRect.Bottom - AppRect.Top));
        Bitmap bmp = new Bitmap(rect.Width, rect.Height, PixelFormat.Format32bppArgb);
        Graphics g = Graphics.FromImage(bmp);
        g.CopyFromScreen(rect.Left, rect.Top, 0, 0, bmp.Size, CopyPixelOperation.SourceCopy);

        // make sure temp directory is there or it will throw.
        bmp.Save(@"c:\temp\test.jpg", ImageFormat.Jpeg);
    }
}

这篇关于如何在 C# 中捕获 Windows Store 应用程序的窗口内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆