C#和Ubuntu - 如何获得主屏幕的大小?

Rav*_*avi 5 .net c# ubuntu mono winforms

我正在尝试确定主屏幕的大小,以便我可以捕获其图像.我的设置是一台拥有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);
Run Code Online (Sandbox Code Playgroud)

Ubuntu的结果是"width = 3520 height = 1080".如果我断开外接显示器,我会得到正确的结果,即"宽度= 1600高度= 900".知道为什么我在具有多个显示器的Ubuntu上得到了错误的值吗?

Rav*_*avi 4

我没有使用 .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;
}
Run Code Online (Sandbox Code Playgroud)