gpt4 book ai didi

.net - 在 WPF 中将像素转换为 CM

转载 作者:行者123 更新时间:2023-12-03 18:12:06 25 4
gpt4 key购买 nike

我有

?(New System.Windows.LengthConverter()).ConvertFrom("1cm")
37.795275590551178 {Double}
Double: 37.795275590551178

所以,在 1cm我有 37.795275590551178px WPF 像素。

我的问题是如何从 px 转换回来至 cm ?

最佳答案

在 WPF 中,我们处理 DeviceIndependentUnits(DIU,命名,通常为“px”),该单位不依赖于设备或屏幕分辨率。

实际上在 .NET Framework (4) 中使用的因子,分别为 'px', 'in', 'cm' 和 'pt'

// System.Windows.LengthConverter
private static double[] PixelUnitFactors = new double[]
{
1.0,
96.0,
37.795275590551178,
1.3333333333333333
};

所以,我们有
private struct PixelUnitFactor
{
public const double Px = 1.0;
public const double Inch = 96.0;
public const double Cm = 37.7952755905512;
public const double Pt = 1.33333333333333;
}

public double CmToPx(double cm)
{
return cm * PixelUnitFactor.Cm;
}

public double PxToCm(double px)
{
return px / PixelUnitFactor.Cm;
}

关于.net - 在 WPF 中将像素转换为 CM,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10231627/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com