WPF-画布高度(以像素为单位) [英] WPF - canvas height in pixels

查看:466
本文介绍了WPF-画布高度(以像素为单位)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何确定画布(具有一定宽度)的宽度(以像素为单位)?

How can I determine the width in pixels for a canvas (which has a certain width)?

我在AnimatedImage控件中有一个位图,并且我知道该位图的宽度(以像素为单位),并且我想缩放该位图,使其在水平方向上完全适合画布.我如何确定这个比例?

I have a bitmap in an AnimatedImage control, and I know the bitmap's width in pixels, and I want to scale the bitmap so that it horizontally fits exactly the canvas. How can i determine this scale?

注意:我不需要使用RenderTargetBitmap,因为位图已经加载.

Note: I do not need to use RenderTargetBitmap, because the bitmap is already loaded.

推荐答案

在WPF中,单位独立于设备.得出单位实际像素大小的公式为:

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

每个WPF单位的像素=恒定WPF单位大小*监视器DPI;

Pixels per WPF Unit = ConstantWPFUnit size * monitor DPI;

以像素为单位的元素大小=每个WPF单位的像素* MyElement.ActualWidth

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

恒定的WPF单位大小为1/96.如果我没记错的话,可以在从SystemInformation.GetPrimaryMonitor或类似的东西返回的类中的属性中找到监视器DPI.

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.

FrameworkElement的ActualWidth以与设备无关的单位表示,是布局发生后元素实际占用的宽度.只需将该属性乘以您在上面计算出的每个WPF单位的像素,即可得到答案.

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.

我怀疑您对许多手动编码做了什么.通常可以简单地通过设置相关控件/画笔的属性来以任何所需方式拉伸图像和其他视觉元素.您是否尝试过为包含位图的元素制作Horizo​​ntalAlignment ="Stretch"和VerticalAlignment ="Center"?

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?

这篇关于WPF-画布高度(以像素为单位)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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