条码打印之单位转换

    public static class UnitConvert
    {
        /// <summary>
        /// 毫米转百分之一英寸
        /// </summary>
        /// <param name="millimeter"></param>
        /// <returns></returns>
        public static float MillimeterToHundredthInch(float millimeter)
        {
            // 1英寸 = 25.4毫米,1百分之一英寸 = 0.01英寸
            return millimeter * (100f / 25.4f);
        }

        /// <summary>
        /// 百分之一英寸转毫米
        /// </summary>
        /// <param name="hundredthInch"></param>
        /// <returns></returns>
        public static float HundredthInchToMillimeter(float hundredthInch)
        {
            return hundredthInch * (25.4f / 100f);
        }

        /// <summary>
        /// 百分之一英寸转像素(需指定DPI)
        /// </summary>
        /// <param name="hundredthInch"></param>
        /// <param name="dpi"></param>
        /// <returns></returns>
        /// <exception cref="ArgumentException"></exception>
        public static float HundredthInchToPixel(float hundredthInch, float dpi)
        {
            if (dpi <= 0)
                throw new ArgumentException("DPI必须大于0", nameof(dpi));

            // 1英寸 = DPI像素,1百分之一英寸 = DPI/100像素
            return hundredthInch * (dpi / 100f);
        }

        /// <summary>
        /// 像素转百分之一英寸(需指定DPI)
        /// </summary>
        /// <param name="pixel"></param>
        /// <param name="dpi"></param>
        /// <returns></returns>
        /// <exception cref="ArgumentException"></exception>
        public static float PixelToHundredthInch(float pixel, float dpi)
        {
            if (dpi <= 0)
                throw new ArgumentException("DPI必须大于0", nameof(dpi));

            return pixel * (100f / dpi);
        }
    }

.Net 环境下调用打印机,打印文档的纸张大小,宽和高的单位是(百分之一英寸)

image

 

posted @ 2025-08-19 08:37  212的s  阅读(6)  评论(0)    收藏  举报