如何在C#中捕获Windowsapp store应用的窗口内容

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

编辑:就像一个笔记我试图捕捉不同的程序的窗口,而不是我自己的程序。 我的程序可以假定为.NET 4.6.1 / C#中的Windows控制台应用程序

此外,我知道在Windows API中必须以某种方式实现这一点,因为Aero Peekfunction,如果您将鼠标hover在正在运行的程序图标上的任务栏上,则会显示窗口的完整高度,包括屏幕外组件。 (见右边的高窗,设置为比我的显示器高出6000px)

看到右边的高大的窗户,设置为比我的显示器高出6000px

从Windows 8.1开始,您可以使用Windows.UI.Xaml.Media.Imaging.RenderTargetBitmap将元素呈现到位图。 这有几点需要注意:

  1. 您可以捕获屏幕外的元素,只要它们位于XAML可视化树中并且“ Visibility设置为“ Visible而不是“ Collapsed
  2. 某些元素(如video)将无法捕获。

有关更多详细信息,请参阅API:

https://msdn.microsoft.com/library/windows/apps/xaml/windows.ui.xaml.media.imaging.rendertargetbitmap.aspx

这可能会成功。 基本上获取应用程序的窗口句柄,调用其上的本机函数来找出应用程序窗口位置,提供那些做图形类并从屏幕复制。

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