我有
?(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;
}
这是使用英寸的计算机行业的问题。每英寸有 96 个像素。每英寸有 2.54 厘米。37.795275590551178 (1 cm) * 2.54 = 95.9999999999997 像素(我们称之为 96)。
您始终可以为 37.795275590551178 创建一个应用程序级别常量,或者您可以使用英寸并来回转换英寸和厘米...
对于将 px 转换为 cm,您可以将 px 转换为英寸到 cm,或者您可以使用上述常数并除以它以获得厘米。
96 像素 / 37.795275590551178 = 2.54 厘米。
这是关于它的另一篇 SO 帖子: 厘米到像素
我遇到了类似的麻烦,但 de WPF 进行计算以将逻辑像素调整为物理像素。也许这段代码可以帮助你。
public struct PixelUnitFactor
{
public const double Px = 1.0;
public const double Inch = 96.0;
public const double Cm = 37.795275590551178;
public const double Pt = 1.33333333333333;
}
public struct CmUnitFactor
{
public const double Px = 0.0264583333333333;
public const double Inch = 2.54;
public const double Cm = 1.0;
public const double Pt = 0.035277778;
}
public struct InUnitFactor
{
public const double Px = 0.0104166666666667;
public const double Inch = 1.0;
public const double Cm = 0.3937007874015748;
public const double Pt = 0.013888888888889;
}
public struct PtUnitFactor
{
public const double Px = 0.75;
public const double Inch = 72.0;
public const double Cm = 28.346456693;
public const double Pt = 1.0;
}
public class FormsEvent
{
//FUNCIONES PARA CONVERTIR PIXELS EN CENTÍMETROS, PULGADAS Y PUNTOS
public static double Convertir_PxToCm(double px) { return px / PixelUnitFactor.Cm; }
public static double Convertir_PxToIn(double px) { return px / PixelUnitFactor.Inch; }
public static double Convertir_PxToPt(double px) { return px / PixelUnitFactor.Pt; }
//FUNCIONES PARA CONVERTIR CENTÍMETROS EN PIXELS, PULGADAS Y PUNTOS
public static double Convertir_CmToPx(double cm) { return cm / CmUnitFactor.Px; }
public static double Convertir_CmToIn(double cm) { return cm / CmUnitFactor.Inch; }
public static double Convertir_CmToPt(double cm) { return cm / CmUnitFactor.Pt; }
//FUNCIONES PARA CONVERTIR PULGADAS EN PIXELS, CENTÍMETROS Y PUNTOS
public static double Convertir_InToPx(double inches) { return inches / InUnitFactor.Px; }
public static double Convertir_InToCm(double inches) { return inches / InUnitFactor.Cm; }
public static double Convertir_InToPt(double inches) { return inches / InUnitFactor.Pt; }
//FUNCIONES PARA CONVERTIR PUNTOS EN PIXELS, PULGADAS Y CENTÍMETROS
public static double Convertir_PtToPx(double pt) { return pt / PtUnitFactor.Px; }
public static double Convertir_PtToCm(double pt) { return pt / PtUnitFactor.Cm; }
public static double Convertir_PtToIn(double pt) { return pt / PtUnitFactor.Inch; }