Esempio n. 1
0
        /// <summary>
        /// Converts a value to the given <see cref="GraphicalUnit"/> (to) based on the given <see cref="GraphicalUnit"/> (from).
        /// </summary>
        /// <param name="value">Number of units.</param>
        /// <param name="from">The unit to convert from.</param>
        /// <param name="to">The unit to convert to.</param>
        /// <param name="ppi">Pixels per inch.</param>
        public static double Convert(this double value, GraphicalUnit from, GraphicalUnit to, double ppi = 72.0)
        {
            var pixels = 0d;

            switch (from)
            {
            case GraphicalUnit.Pixel:
                pixels = System.Math.Round(value, 0);
                break;

            case GraphicalUnit.Inch:
                pixels = System.Math.Round(value * ppi, 0);
                break;

            case GraphicalUnit.Centimeter:
                pixels = System.Math.Round((value * ppi) / 2.54, 0);
                break;

            case GraphicalUnit.Millimeter:
                pixels = System.Math.Round((value * ppi) / 25.4, 0);
                break;

            case GraphicalUnit.Point:
                pixels = System.Math.Round((value * ppi) / 72, 0);
                break;

            case GraphicalUnit.Pica:
                pixels = System.Math.Round((value * ppi) / 6, 0);
                break;

            case GraphicalUnit.Twip:
                pixels = System.Math.Round((value * ppi) / 1140, 0);
                break;

            case GraphicalUnit.Character:
                pixels = System.Math.Round((value * ppi) / 12, 0);
                break;

            case GraphicalUnit.En:
                pixels = System.Math.Round((value * ppi) / 144.54, 0);
                break;
            }

            var inches = pixels / ppi;
            var result = pixels;

            switch (to)
            {
            case GraphicalUnit.Inch:
                result = inches;
                break;

            case GraphicalUnit.Centimeter:
                result = inches * 2.54;
                break;

            case GraphicalUnit.Millimeter:
                result = inches * 25.4;
                break;

            case GraphicalUnit.Point:
                result = inches * 72.0;
                break;

            case GraphicalUnit.Pica:
                result = inches * 6.0;
                break;

            case GraphicalUnit.Twip:
                result = inches * 1140.0;
                break;

            case GraphicalUnit.Character:
                result = inches * 12.0;
                break;

            case GraphicalUnit.En:
                result = inches * 144.54;
                break;
            }

            return(result);
        }
Esempio n. 2
0
        /// <summary>
        /// Converts a graphical unit value to another graphical unit value.
        /// </summary>
        /// <param name="Value">Number of units.</param>
        /// <param name="From">The unit to convert from.</param>
        /// <param name="To">The unit to convert to.</param>
        /// <param name="Ppi">Pixels per inch.</param>
        /// <param name="RoundTo">Decimal places to round to.</param>
        public static double ToUnit(this double Value, GraphicalUnit From, GraphicalUnit To, double Ppi = 72.0)
        {
            //Convert to pixels
            double Pixels = 0.0;

            switch (From)
            {
            case GraphicalUnit.Pixel:
                Pixels = Math.Round(Value, 0);
                break;

            case GraphicalUnit.Inch:
                Pixels = Math.Round(Value * Ppi, 0);
                break;

            case GraphicalUnit.Centimeter:
                Pixels = Math.Round((Value * Ppi) / 2.54, 0);
                break;

            case GraphicalUnit.Millimeter:
                Pixels = Math.Round((Value * Ppi) / 25.4, 0);
                break;

            case GraphicalUnit.Point:
                Pixels = Math.Round((Value * Ppi) / 72, 0);
                break;

            case GraphicalUnit.Pica:
                Pixels = Math.Round((Value * Ppi) / 6, 0);
                break;

            case GraphicalUnit.Twip:
                Pixels = Math.Round((Value * Ppi) / 1140, 0);
                break;

            case GraphicalUnit.Character:
                Pixels = Math.Round((Value * Ppi) / 12, 0);
                break;

            case GraphicalUnit.En:
                Pixels = Math.Round((Value * Ppi) / 144.54, 0);
                break;
            }

            double Inches = Pixels / Ppi;

            double Result = Pixels;

            //Convert to target unit
            switch (To)
            {
            case GraphicalUnit.Inch:
                Result = Inches;
                break;

            case GraphicalUnit.Centimeter:
                Result = Inches * 2.54;
                break;

            case GraphicalUnit.Millimeter:
                Result = Inches * 25.4;
                break;

            case GraphicalUnit.Point:
                Result = Inches * 72.0;
                break;

            case GraphicalUnit.Pica:
                Result = Inches * 6.0;
                break;

            case GraphicalUnit.Twip:
                Result = Inches * 1140.0;
                break;

            case GraphicalUnit.Character:
                Result = Inches * 12.0;
                break;

            case GraphicalUnit.En:
                Result = Inches * 144.54;
                break;
            }

            return(Result);
        }