繁体   English   中英

如何在 Windows 窗体应用程序中获取监视器的屏幕大小以捕获屏幕截图?

[英]How to get the screen size of monitor in Windows Form application for capturing screenshots?

我正在研究一种解决方案,该解决方案可以定期抓取屏幕截图并以图像形式保存。 此应用程序内置于 Windows 窗体中。

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

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

这在分辨率为 1366 * 768 的笔记本电脑中运行良好。

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

有没有办法处理代码中的监视器大小。

假设您要捕获包含表单的屏幕,请使用Screen.FromControl 方法,将表单实例传递给它,然后使用该屏幕的 WorkingArea。

如果这个假设是错误的,请在您的问题中添加更多细节。

此代码执行多个屏幕...它是我使用的...

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

    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM