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

标签 c# .net winforms ubuntu mono

我正在尝试确定主屏幕的大小,以便捕捉它的图像。我的设置是一台笔记本电脑,显示屏为 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/

相关文章:

c# - 在 64 位计算机上使用 C# 和 "BUILD x86"进行注册表访问

c# - 等待 BackgoundWorker.DoWork() 完成

.net - VS.NET : Project Refs vs. 程序集引用

c# - 使用 WinForm 绘制实时 2D 图形

c# - 将从部门检索到的 SQL 值分配给 session ["UserAuthentication"]

c# - 使用 for 循环和条件语句定义方法

c# - 在 WinForms 应用程序中禁用重绘

c# - Task.Factory.StartNew(someMethod(withParam)).continueWith(sameMethod(differentParam)).Wait()

.net - 使 WebBrowser 控件删除其缓存

c# - 具有完全相同的键和值的字典