厘米到像素

Sha*_*hin 8 c# math winforms

如何在c#中将厘米转换为像素?

Wow*_*owa 20

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


小智 6

你可以使用你正在绘制的Graphics对象的DpiXDpiY属性(你必须拥有它,因为在没有某种图形上下文的情况下转换是没有意义的.)

DpiXDpiY中,"D"代表"点"或像素,而"i"代表"英寸".因此,它会将像素转换为英寸.然后你所要做的就是将英寸转换为厘米=>(x*2.54)

此外,如果您想要更"精确",请查看以下内容: HOWTO:如何使应用程序显示真实的测量单位