有没有办法在c#中获得屏幕的原生分辨率?
我问的原因是我有一些曲线,无论分辨率如何,它们看起来都非常重要。当屏幕不是原始分辨率时,它们看起来与以前有所不同,我想显示一个警告,就是这种情况。
答案 0 :(得分:3)
在WinForms中,您可以使用
之一var someScreen = Screen.AllScreens[i];
var mainScreen = Screen.PrimaryScreen;
并且屏幕具有边界(粗略)和工作区域(净)矩形。
第二个想法,这只会告诉你当前的分辨率。这应该足够了,正如已经评论过的那样,你真的想知道宽高比。
答案 1 :(得分: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);
}
答案 2 :(得分:3)
根据我的经验,最佳解决方案是从监视器“EDID
中提取该信息如何获得原生分辨率的答案如下:How to fetch the NATIVE resolution of attached monitor from 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);
}
答案 3 :(得分:2)
尝试这样的事情: -
GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Width
GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Height
要获得屏幕的当前分辨率,您可以使用: -
Rectangle resolution = Screen.PrimaryScreen.Bounds;
现在更改分辨率。
查看此link。
Screen screen = Screen.PrimaryScreen;
int S_width=screen.Bounds.Width;
int S_height=screen.Bounds.Height;