如何在Windows窗体应用程序中获取显示器的屏幕尺寸以捕获屏幕截图?

3

我正在开发一个解决方案,可以定期抓取屏幕截图并将其保存为图像。这个应用程序是在Windows Forms中构建的。

我使用了以下代码来获取屏幕分辨率:

int h = Screen.PrimaryScreen.WorkingArea.Height;
int w = Screen.PrimaryScreen.WorkingArea.Width;

在分辨率为1366 * 768的笔记本电脑上,这很正常。

但是,当同一个应用程序在非常大的显示器上运行时,图像会从右侧和底部裁剪掉。

是否有一种方式可以通过代码来处理显示器大小。


1
工作区是显示器的桌面区域,不包括任务栏、停靠窗口和停靠工具栏。可以使用Screen.Bounds来获取整个屏幕。 - Slai
2个回答

2
假设您想捕获包含表单的屏幕,请使用Screen.FromControl方法,将表单实例传递给它,然后使用该屏幕的WorkingArea。
如果此假设不正确,请在您的问题中添加更多细节。

0

这段代码可以实现多屏幕显示... 这是我使用的代码...

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Drawing.Imaging;
using System.Windows.Forms;
using System.IO;

namespace JeremyThompsonLabs
{
    public class Screenshot
    {
        public static string TakeScreenshotReturnFilePath()
        {
            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 bitmap = new Bitmap(screenWidth, screenHeight))
            {
                // Draw the screenshot into our bitmap.
                using (Graphics g = Graphics.FromImage(bitmap))
                {
                    g.CopyFromScreen(screenLeft, screenTop, 0, 0, bitmap.Size);
                }

                var uniqueFileName = Path.Combine(System.IO.Path.GetTempPath(), Path.GetRandomFileName().Replace(".", string.Empty) + ".jpeg");
                try
                {
                    bitmap.Save(uniqueFileName, ImageFormat.Jpeg);
                }
                catch (Exception ex)
                {
                    return string.Empty;
                }
                return uniqueFileName;
            }
        }

    }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接