在WPF中将像素转换为CM

时间:2012-04-19 15:36:24

标签: .net wpf

我有

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

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

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

3 个答案:

答案 0 :(得分:16)

在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;
}

答案 1 :(得分:2)

这是计算机行业使用英寸的问题。每英寸有96个像素。每英寸有2.54厘米。 37.795275590551178(1 cm)* 2.54 = 95.9999999999997像素(我们称之为96)。

你总是可以为37.795275590551178创建一个应用程序级别常量,或者你可以使用英寸并来回转换英寸和厘米......

要将px转换为cm,您可以将px转换为英寸到厘米,或者您可以使用上面的常量并除以它来获得厘米。

96像素/ 37.795275590551178 = 2.54厘米。

这是关于它的另一篇SO帖子: Centimeter to pixel

答案 2 :(得分:1)

我遇到了类似的问题,但是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; }