PrinterUnitConvert.cs source code in C# .NET

Source code for the .NET framework in C#

                        

Code:

/ 4.0 / 4.0 / untmp / DEVDIV_TFS / Dev10 / Releases / RTMRel / ndp / fx / src / CommonUI / System / Drawing / Printing / PrinterUnitConvert.cs / 1407647 / PrinterUnitConvert.cs

                            //------------------------------------------------------------------------------ 
// 
//     Copyright (c) Microsoft Corporation.  All rights reserved.
// 
//----------------------------------------------------------------------------- 

namespace System.Drawing.Printing { 
    using System.Runtime.InteropServices; 
    using System.Diagnostics;
    using System; 
    using System.Drawing;
    using System.ComponentModel;
    using Microsoft.Win32;
 
    /// 
    ///  
    ///     
    ///       Specifies a series of conversion methods that are
    ///       useful when interoperating with the raw Win32 printing API. 
    ///       This class cannot be inherited.
    ///    
    /// 
    public sealed class PrinterUnitConvert { 
        private PrinterUnitConvert() {
        } 
 
        /// 
        ///  
        ///    
        ///       Converts the value, in fromUnit units, to toUnit units.
        ///    
        ///  
        public static double Convert(double value, PrinterUnit fromUnit, PrinterUnit toUnit) {
            double fromUnitsPerDisplay = UnitsPerDisplay(fromUnit); 
            double toUnitsPerDisplay = UnitsPerDisplay(toUnit); 
            return value * toUnitsPerDisplay / fromUnitsPerDisplay;
        } 

        /// 
        /// 
        ///     
        ///       Converts the value, in fromUnit units, to toUnit units.
        ///     
        ///  
        public static int Convert(int value, PrinterUnit fromUnit, PrinterUnit toUnit) {
            return(int) Math.Round(Convert((double)value, fromUnit, toUnit)); 
        }

        /// 
        ///  
        ///    
        ///       Converts the value, in fromUnit units, to toUnit units. 
        ///     
        /// 
        public static Point Convert(Point value, PrinterUnit fromUnit, PrinterUnit toUnit) { 
            return new Point(
                            Convert(value.X, fromUnit, toUnit),
                            Convert(value.Y, fromUnit, toUnit)
                            ); 
        }
 
        ///  
        /// 
        ///     
        ///       Converts the value, in fromUnit units, to toUnit units.
        ///    
        /// 
        public static Size Convert(Size value, PrinterUnit fromUnit, PrinterUnit toUnit) { 
            return new Size(
                           Convert(value.Width, fromUnit, toUnit), 
                           Convert(value.Height, fromUnit, toUnit) 
                           );
        } 

        /// 
        /// 
        ///     
        ///       Converts the value, in fromUnit units, to toUnit units.
        ///     
        ///  
        public static Rectangle Convert(Rectangle value, PrinterUnit fromUnit, PrinterUnit toUnit) {
            return new Rectangle( 
                                Convert(value.X, fromUnit, toUnit),
                                Convert(value.Y, fromUnit, toUnit),
                                Convert(value.Width, fromUnit, toUnit),
                                Convert(value.Height, fromUnit, toUnit) 
                                );
        } 
 
        /// 
        ///  
        ///    
        ///       Converts the value, in fromUnit units, to toUnit units.
        ///    
        ///  
        public static Margins Convert(Margins value, PrinterUnit fromUnit, PrinterUnit toUnit) {
            Margins result = new Margins(); 
 
            result.DoubleLeft = Convert(value.DoubleLeft, fromUnit, toUnit);
            result.DoubleRight = Convert(value.DoubleRight, fromUnit, toUnit); 
            result.DoubleTop = Convert(value.DoubleTop, fromUnit, toUnit);
            result.DoubleBottom = Convert(value.DoubleBottom, fromUnit, toUnit);

            return result; 
        }
 
        private static double UnitsPerDisplay(PrinterUnit unit) { 
            double result;
            switch (unit) { 
                case PrinterUnit.Display:
                    result = 1.0;
                    break;
                case PrinterUnit.ThousandthsOfAnInch: 
                    result = 10.0;
                    break; 
                case PrinterUnit.HundredthsOfAMillimeter: 
                    result = 25.4;
                    break; 
                case PrinterUnit.TenthsOfAMillimeter:
                    result = 2.54;
                    break;
                default: 
                    Debug.Fail("Unknown PrinterUnit " + unit);
                    result = 1.0; 
                    break; 
            }
 
            return result;
        }
    }
} 


// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
                        

Link Menu

Network programming in C#, Network Programming in VB.NET, Network Programming in .NET
This book is available now!
Buy at Amazon US or
Buy at Amazon UK