我正在尝试确定主屏幕的大小,以便我可以捕获其图像。我的设置是一台拥有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”。如果我断开外接显示器,我会得到正确的结果,即“宽度= 1600高度= 900”。知道为什么我在Ubuntu上有多个显示器的错误值吗?
答案 0 :(得分:3)
我没有使用.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;
r.Width = int.Parse(w);
r.Height = int.Parse(h);
Console.WriteLine ("Display Size is {0} x {1}", w, h);
return r;
}