WPF - canvas height in pixels

爱⌒轻易说出口 提交于 2019-11-28 22:08:34

In WPF units are device independent. The formula to figure out the actual pixel size of a unit is:

Pixels per WPF Unit = ConstantWPFUnit size * monitor DPI;

Size of your element in pixels = Pixels per WPF Unit * MyElement.ActualWidth

The constant WPF unit size is 1/96. If I remember correctly the monitor DPI can be found as properties in the class returned from SystemInformation.GetPrimaryMonitor or something similar.

ActualWidth of a FrameworkElement is in device-independent units and is the width the element actually takes up after layout takes place. Simply multiply this property by the pixels per WPF unit you calculate above and you'll have your answer.

I have a suspicion that you doing to much manual coding however. Stretching images and other visual elements in any desired way can usually be accomplished simply by setting properties on the control/brush in question. Have you tried making HorizontalAlignment="Stretch" and VerticalAlignment="Center" for the element that contains the bitmap?

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!