(C#) 如何获取屏幕的点与像素关系?

发布于 2024-07-24 23:21:52 字数 63 浏览 5 评论 0原文

据我了解,点与像素的关系取决于屏幕分辨率。 那么如何在 C# 中在运行时计算它呢?

谢谢

From what I've understood the relationship point to pixel will depend on the screen resolution. So how can I calculate it at run-time in c#?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

陪你搞怪i 2024-07-31 23:21:53

如果你想获取屏幕的 DPI,那就有点棘手了。 您必须创建一个真正的 Graphics 对象并对其进行查询。

例如,在主窗体的 Load 事件中:

using( Graphics g = CreateGraphics() )
{
    _dpiX = g.DpiX;
    _dpiY = g.DpiY; // In practice usually == dpiX
    _points = _dpiX / 72.0f; // There are 72 points per inch
}

当然,大多数显示器都会谎报实际 DPI,并且始终返回 72 或 96,或者在启用大字体时返回 120。如果您实际上想要将物理英寸映射到屏幕,您可以必须在用户的帮助下实际校准它 - 让他们选择一条测量为 1 英寸的线。

If you're trying to get the DPI of the screen it's a bit trickier. You'll have to create a real Graphics object and query that.

For example, in the Load event of your main form:

using( Graphics g = CreateGraphics() )
{
    _dpiX = g.DpiX;
    _dpiY = g.DpiY; // In practice usually == dpiX
    _points = _dpiX / 72.0f; // There are 72 points per inch
}

Of course most monitors lie about the actual DPI and always return 72 or 96, or when large fonts are enabled 120. If you actually want to map a physical inch to a the screen you'll have to actually calibrate it with the user's help - having them pick a line that they measure to be 1 inch.

静若繁花 2024-07-31 23:21:53

一切都在 Screen 对象中:

int bpp = System.Windows.Forms.Screen.PrimaryScreen.BitsPerPixel;
int wid = Screen.PrimaryScreen.WorkingArea.Width;
int ht = Screen.PrimaryScreen.WorkingArea.Height;

在我的机器上它给出:

bpp=32
width=1280
height=740

It is all in the Screen object:

int bpp = System.Windows.Forms.Screen.PrimaryScreen.BitsPerPixel;
int wid = Screen.PrimaryScreen.WorkingArea.Width;
int ht = Screen.PrimaryScreen.WorkingArea.Height;

On my machine it gives:

bpp=32
width=1280
height=740
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文