像素和厘米互相转换

在做report的时候,用到了像素和厘米之间的相互转换

程序是winform 开发


像素转厘米:

        /// 
        /// 像素转换成厘米
        /// 
        /// 像素
        /// 厘米
        private double PixelToCm(double Pixel)
        {
            double cm = -1;
            using (Graphics g = this.CreateGraphics())
            {
                cm = (Pixel / g.DpiY) * 2.54d;
            }
            return (double)cm;
        }
厘米转像素

        private int CentimeterToPixel(double Centimeter)
        {
            double pixel = -1;
            using (Graphics g = this.CreateGraphics())
            {
                pixel = Centimeter * g.DpiY / 2.54d;
            }
            return (int)pixel;
        }





你可能感兴趣的:(.Net)