9

有没有办法在 c# 中获得屏幕的本机分辨率?

我问的原因是我有一些曲线,无论分辨率如何,它们看起来都一样非常重要。当屏幕不是原始分辨率时,它们看起来与以前有些不同,我想显示一个警告,就是这种情况。

4

4 回答 4

4

根据我的经验,最好的解决方案是从监视器的EDID中提取该信息

How to get native resolution 回答:How to fetch the NATIVE resolution of attach monitor of EDID file through VB6.0?

我制作了一个小 javascript,从 54 开始的 8 个字节中获取分辨率。

var dtd = 0;
var edid =  new Uint8Array(8);
var i = 0;

edid[i++] = 0x0E;
edid[i++] = 0x1F;
edid[i++] = 0x00;
edid[i++] = 0x80;
edid[i++] = 0x51;
edid[i++] = 0x00;
edid[i++] = 0x1B;
edid[i++] = 0x30;

var horizontalRes = ((edid[dtd+4] >> 4) << 8) | edid[dtd+2] ;
var verticalRes = ((edid[dtd+7] >> 4) << 8) | edid[dtd+5];
console.log(horizontalRes+", "+verticalRes);

这是一个 C# 版本:

    static Point ExtractResolution(byte [] edid)
    {
        const int dtd = 54;
        var horizontalRes = ((edid[dtd + 4] >> 4) << 8) | edid[dtd + 2];
        var verticalRes = ((edid[dtd + 7] >> 4) << 8) | edid[dtd + 5];
        return new Point(horizontalRes, verticalRes);
    }
于 2014-11-20T11:45:53.950 回答
3

通常,最大分辨率是 LCD 显示器的原始分辨率。然而,情况并非总是如此。如果我们可以利用那里,获得最大分辨率就足够了。

可以使用以下方法获得最大分辨率:

    [DllImport("user32.dll")]
    private static extern int EnumDisplaySettings(string deviceName, int modeNum, ref DeviceMode devMode);

    /// <summary>
    /// Gets the max resolution + refresh rate supported by specific display
    /// </summary>
    /// <param name="deviceName">Device name(System.Windows.Forms.Screen.DeviceName)</param>
    /// <param name="dispWidth">Width of the display</param>
    /// <param name="dispHeight">Height of the display</param>
    /// <param name="refreshRate">Refresh rate of the display</param>
    /// <returns></returns>
    public static void GetMaxResolutionWithRefreshRate(string deviceName, out int dispWidth, out int dispHeight, out int refreshRate)
    {
        dispWidth = dispHeight = refreshRate = 0;
        DeviceMode deviceMode = new DeviceMode();
        for (int i = 0; Win32.EnumDisplaySettings(deviceName, i, ref deviceMode) != 0; i++)
        {
            if (deviceMode.dmPelsWidth > dispWidth || (deviceMode.dmPelsWidth == dispWidth && deviceMode.dmPelsHeight >= dispHeight && deviceMode.dmDisplayFrequency >= refreshRate))
            {
                dispWidth = deviceMode.dmPelsWidth;
                dispHeight = deviceMode.dmPelsHeight;
                refreshRate = deviceMode.dmDisplayFrequency;
            }
        }
    }

public static void GetMaxResolutionWithRefreshRate(out int dispWidth, out int dispHeight, out int refreshRate)
    {
        GetMaxResolutionWithRefreshRate(null, out dispWidth, out dispHeight, out refreshRate);
    }
于 2014-09-26T16:41:40.807 回答
2

尝试这样的事情: -

GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Width
GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Height

要获得屏幕的当前分辨率,您可以使用:-

Rectangle resolution = Screen.PrimaryScreen.Bounds;

现在用于更改分辨率。

看看这个链接

Screen screen = Screen.PrimaryScreen;
int S_width=screen.Bounds.Width;
int S_height=screen.Bounds.Height;
于 2013-08-12T13:56:33.433 回答
2

在 WinForms 中,您可以使用其中一种

var someScreen = Screen.AllScreens[i];
var mainScreen = Screen.PrimaryScreen;

屏幕有一个边界(总)和一个工作区(净)矩形。

再三考虑,这只会告诉您当前的分辨率。不过,这应该足够了,正如已经评论的那样,您真的想知道纵横比。

于 2013-08-12T14:00:24.340 回答