2017-01-20 89 views
2

我正在研究一种抓取屏幕截图的解决方案,并定期将其以图像的形式保存。此应用程序内置于Windows窗体中。如何在Windows Form应用程序中获取监视器的屏幕大小以捕获屏幕截图?

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

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

这工作正常,与1366×768分辨率的笔记本电脑。

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

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

+0

[“的工作区域是显示器的桌面面积,不包括任务栏,停靠窗口,并停靠工具栏。” ](https://msdn.microsoft.com/en-us/library/system.windows.forms.screen.workingarea)。可以使用['Screen.Bounds'](https://msdn.microsoft.com/en-us/library/system.windows.forms.screen.bounds)来获取整个屏幕 – Slai

回答

1

假设您想要捕获包含表单的屏幕,请使用Screen.FromControl method,将表单实例传递给它,然后使用该屏幕的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; 
      } 
     } 

    } 
}