c# – 截取所有可见应用程序和表单的多个桌面的屏幕截图

栏目: ASP.NET · 发布时间: 5年前

内容简介:翻译自:https://stackoverflow.com/questions/15847637/take-screenshot-of-multiple-desktops-of-all-visible-applications-and-forms
我正在使用一个具有4个输出(监视器)的系统,例如每个输出1280×1024像素.我需要整个桌面和所有打开的应用程序的屏幕截图.

我试过 GetDesktopWindow() (MSDN) ,但它无法正常工作.某些表单未显示在捕获的图片上.

i tried GetDesktopWindow() function but it doesn’t work properly.

当然不是.

GetDesktopWindow 函数返回桌面窗口的句柄.它与捕获该窗口的图像没有任何关系.

此外,桌面窗口与“整个屏幕”不同.它特指桌面窗口.有关更多信息,请参阅 this article ,以及滥用此函数返回的句柄时可能出现的问题.

i’m working with a system that have 4 outputs (monitors) with 1280×1024(e.g) for each output. i need a screenshot from whole desktop and all open applications on it.

使用 Graphics.CopyFromScreen 方法在.NET Framework中执行此操作相对简单.你甚至不需要做任何P / Invoke!

在这种情况下唯一的技巧是确保您传递适当的尺寸.由于您有4个显示器,因此仅传递主屏幕的尺寸将不起作用.您需要传递整个虚拟屏幕的尺寸,其中包含所有显示器.通过查询 SystemInformation.VirtualScreen 属性来检索它,该属性返回虚拟屏幕的边界.如文档所示,这是多监视器系统上整个桌面的界限.

示例代码:

// Determine the size of the "virtual screen", which includes all monitors.
int screenLeft   = SystemInformation.VirtualScreen.Left;
int screenTop    = SystemInformation.VirtualScreen.Top;
int screenWidth  = SystemInformation.VirtualScreen.Width;
int screenHeight = SystemInformation.VirtualScreen.Height;

// Create a bitmap of the appropriate size to receive the screenshot.
using (Bitmap bmp = new Bitmap(screenWidth, screenHeight))
{
    // Draw the screenshot into our bitmap.
    using (Graphics g = Graphics.FromImage(bmp))
    {
        g.CopyFromScreen(screenLeft, screenTop, 0, 0, bmp.Size);
    }

    // Do something with the Bitmap here, like save it to a file:
    bmp.Save(savePath, ImageFormat.Jpeg);
}

编辑:

please check your solution with a wpf application in a thread that is not your main thread. i tried it. it doesn’t work!

嗯,我没有在问题上看到WPF标签或在身体的任何地方提到过.

不过,无论如何.我发布的代码在WPF应用程序中运行得很好,只要添加适当的引用并使用声明即可.您将需要System.Windows.Forms和System.Drawing.可能有更多的WPF方式这样做,不需要依赖这些WinForms程序集,但我不知道它是什么.

它甚至适用于另一个线程.这里没有任何东西需要UI线程.

是的,我测试了它.这是我的完整测试代码:

using System.Windows;
using System.Windows.Forms;   // also requires a reference to this assembly
using System.Drawing;         // also requires a reference to this assembly
using System.Drawing.Imaging;
using System.Threading;

public partial class MainWindow : Window
{
   public MainWindow()
   {
      InitializeComponent();
   }

   private void button1_Click(object sender, RoutedEventArgs e)
   {
      // Create a new thread for demonstration purposes.
      Thread thread = new Thread(() =>
      {
         // Determine the size of the "virtual screen", which includes all monitors.
         int screenLeft   = SystemInformation.VirtualScreen.Left;
    int screenTop    = SystemInformation.VirtualScreen.Top;
    int screenWidth  = SystemInformation.VirtualScreen.Width;
    int screenHeight = SystemInformation.VirtualScreen.Height;

         // Create a bitmap of the appropriate size to receive the screenshot.
         using (Bitmap bmp = new Bitmap(screenWidth, screenHeight))
         {
            // Draw the screenshot into our bitmap.
            using (Graphics g = Graphics.FromImage(bmp))
            {
               g.CopyFromScreen(screenLeft, screenTop, 0, 0, bmp.Size);
            }

            // Do something with the Bitmap here, like save it to a file:
            bmp.Save("G:\\TestImage.jpg", ImageFormat.Jpeg);
         }
      });
      thread.SetApartmentState(ApartmentState.STA);
      thread.Start();
   }
}

翻译自:https://stackoverflow.com/questions/15847637/take-screenshot-of-multiple-desktops-of-all-visible-applications-and-forms


以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

区块链与人工智能:数字经济新时代

区块链与人工智能:数字经济新时代

高航、俞学劢、王毛路 / 电子工业出版社 / 2018-7-23 / 80

《区块链与人工智能》是畅销书《区块链与新经济:数字货币2.0时代》全新修订升级版。本书是市场上为数不多的系统阐述区块链、人工智能技术与产业的入门级系统教程。从比特币到各类数字货币(代币),从基础原理到应用探讨,全景式呈现区块链与人工智能的发展脉络,既有历史的厚重感也有科技的未来感。本书的另一个亮点是系统整理了区块链创业地图,是一本关于区块链创业、应用、媒体的学习指南,以太坊创始人Vitalik专门......一起来看看 《区块链与人工智能:数字经济新时代》 这本书的介绍吧!

在线进制转换器
在线进制转换器

各进制数互转换器

URL 编码/解码
URL 编码/解码

URL 编码/解码

SHA 加密
SHA 加密

SHA 加密工具