WPF像素到桌面像素 [英] WPF Pixels to desktop pixels

查看:99
本文介绍了WPF像素到桌面像素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我过去已链接到响应关于将WPF像素坐标转换为桌面坐标的类似问题,但是我不确定我是否了解所涉及的数学.

I've been linked in the past to this response to a similar question on converting WPF pixel coordinates to desktop ones, however I'm not sure I understand the maths involved.

Astonish的答案指出每个WPF单位的像素= ConstantWPFUnit大小*监视器DPI;"并且恒定的WPF单位大小是1/96."

Astonish' answer states that "Pixels per WPF Unit = ConstantWPFUnit size * monitor DPI;" and that "The constant WPF unit size is 1/96."

在我的情况下,我从图形对象中获取了DPI,该图形对象是由我在拍摄桌面屏幕快照后创建的位图对象(因为我找不到Astonish所说的属性)创建的,因此我有:

In my case, I've taken the DPI from a graphics object which was created from the bitmap object (as I couldn't find the property the Astonish spoke of) that I created after taking a screenshot of the desktop, so I have:

Graphics g = Graphics.FromImage(bitmap);
float WpfUnit = (1 / 96) * g.DpiX;

考虑到从图形对象返回的DPI是96,所以我留下了

Given that the DPI being returned from the graphics object is 96, I am left with

WpfUnit = (1 / 96) * 96 = 1

但是,出于某些未知的原因(我认为),WpfUnit的输出为0.我唯一能解决这个问题的方法就是说

However, WpfUnit is coming out as 0 for some unknown (to me) reason. The only way I can see to fix this is to say

if(WpfUnit == 0) WpfUnit = 1;

即使这样并不能真正解决问题,因为按照链接的答案中的建议,将高度值和顶部值乘以WpfUnit时,除乘以1外,对它们没有任何处理.

And even that doesn't really fix the issue, as the height value and top values, when multiplied by the WpfUnit as suggested in the linked answer, have nothing done to them aside from being multiplied by 1.

因此,总而言之,我仍然坚持将WPF像素转换为桌面像素.任何帮助,将不胜感激.

So, in conclusion, I'm still stuck on converting WPF pixels to desktop pixels. Any help on this would be greatly appreciated.

推荐答案

内置

How about built-in PointToScreen and PointFromScreen methods? Or am I missing something?

这篇关于WPF像素到桌面像素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆