我正在尝试确定主屏幕的大小,以便捕捉它的图像。我的设置是一台笔记本电脑,显示屏为 1600x900,外接显示器为 1920x1080。获取大小的代码在 Windows 上运行良好,但在 Ubuntu(使用 MonoDevelop)上给出了错误的结果。
Rectangle capture_rect = System.Windows.Forms.Screen.PrimaryScreen.Bounds;
Console.WriteLine("width={0} height={1}", capture_rect.Height, capture_rect.Width);
Ubuntu 上的结果是“width=3520 height=1080”。如果我断开外接显示器,我会得到正确的结果,即“width=1600 height=900”。知道为什么我在具有多个显示器的 Ubuntu 上得到错误的值吗?
最佳答案
我没有使用 .NET 获取屏幕尺寸,而是使用了 Linux“xrandr”。这是我的代码:
public Rectangle GetDisplaySize()
{
// Use xrandr to get size of screen located at offset (0,0).
System.Diagnostics.Process p = new System.Diagnostics.Process();
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.FileName = "xrandr";
p.Start();
string output = p.StandardOutput.ReadToEnd();
p.WaitForExit();
var match = System.Text.RegularExpressions.Regex.Match(output, @"(\d+)x(\d+)\+0\+0");
var w = match.Groups[1].Value;
var h = match.Groups[2].Value;
Rectangle r = new Rectangle();
r.Width = int.Parse(w);
r.Height = int.Parse(h);
Console.WriteLine ("Display Size is {0} x {1}", w, h);
return r;
}
关于C# 和 Ubuntu - 如何获取主屏幕的大小?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20164262/