簡體   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