问题
Is there a way to get the native resolution of a screen in 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.
回答1:
From my experience the best solution is to extract that information from the monitors' 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?
i have made a little javascript that gets the resolution out of the 8 bytes starting at 54.
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:
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);
}
回答2:
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.
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);
}
回答3:
In WinForms you can use one of
var someScreen = Screen.AllScreens[i];
var mainScreen = Screen.PrimaryScreen;
and a Screen has a Bounds (gross) and a 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.
回答4:
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;
来源:https://stackoverflow.com/questions/18188825/get-native-resolution-of-screen