wpf 获取屏幕的原始分辨率
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18188825/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get native resolution of screen
提问by Mathias
Is there a way to get the native resolution of a screen in c#?
有没有办法在 c# 中获得屏幕的原始分辨率?
The reason that I ask is that I have some curves and it is very important that they look the same no matter what resolution. When the screen isn't in native resolution they look somewhat different than before and I want to show a warning that that is the case.
我问的原因是我有一些曲线,无论分辨率如何,它们看起来都一样是非常重要的。当屏幕不是原始分辨率时,它们看起来与以前有些不同,我想显示一个警告,就是这种情况。
采纳答案by clamp
From my experience the best solution is to extract that information from the monitors' EDID
根据我的经验,最好的解决方案是从显示器的EDID 中提取该信息
How to get the native resolution is answered in: How to fetch the NATIVE resolution of attached monitor from EDID file through VB6.0?
如何获得原始分辨率在:如何通过 VB6.0 从 EDID 文件中获取附加显示器的 NATIVE 分辨率?
i have made a little javascript that gets the resolution out of the 8 bytes starting at 54.
我做了一个小 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);
and here is a C# version:
这是一个 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);
}
回答by Ronak Agrawal
Generally, Max Resolution is the Native Resolution for LCD Displays. However, that's not always the case. If we can leverage there, getting Max Resolutions should suffice.
通常,最大分辨率是 LCD 显示器的原始分辨率。然而,情况并非总是如此。如果我们可以在那里利用,获得最大分辨率就足够了。
Max Resolution can be obtained using:
可以使用以下方法获得最大分辨率:
[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);
}
回答by Rahul Tripathi
Try something like this:-
尝试这样的事情:-
GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Width
GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Height
To get the current resolution of the screen you can use:-
要获得屏幕的当前分辨率,您可以使用:-
Rectangle resolution = Screen.PrimaryScreen.Bounds;
Now for changing the resolution.
现在改变分辨率。
Check out this link.
查看此链接。
Screen screen = Screen.PrimaryScreen;
int S_width=screen.Bounds.Width;
int S_height=screen.Bounds.Height;
回答by Henk Holterman
In WinForms you can use one of
在 WinForms 中,您可以使用其中之一
var someScreen = Screen.AllScreens[i];
var mainScreen = Screen.PrimaryScreen;
and a Screen has a Bounds (gross) and a WorkingArea (net) Rectangle.
一个 Screen 有一个 Bounds (gross) 和一个 WorkingArea (net) Rectangle。
On second thought, this will only tell you the current resolution. That should be enough though, as has been commented you really want to know the aspect ratio.
再想一想,这只会告诉您当前的分辨率。不过,这应该足够了,正如评论中所说,您真的很想知道纵横比。

