【问题标题】:How to get the screen size of monitor in Windows Form application for capturing screenshots?如何在 Windows 窗体应用程序中获取监视器的屏幕大小以捕获屏幕截图?
【发布时间】:2017-06-04 21:22:38
【问题描述】:

我正在研究一种解决方案,它会定期抓取屏幕截图并以图像的形式保存。此应用程序是在 Windows 窗体中构建的。

我已经使用下面的代码来获取屏幕分辨率-:

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

这适用于分辨率为 1366 * 768 的笔记本电脑。

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

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

【问题讨论】:

标签: c# .net winforms screenshot multiple-monitors


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-12-03
      • 1970-01-01
      • 2020-09-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-04
      相关资源
      最近更新 更多