Convert Pixels to Inches and vice versa in C#

后端 未结 3 465
深忆病人
深忆病人 2021-01-02 11:01

I am looking to convert pixes to inches and vice versa. I understand that I need DPI, but I am not sure how to get this information (e.g. I don\'t have the Graphics

3条回答
  •  渐次进展
    2021-01-02 11:46

    There's, physically, no real way without knowing the DPI. Pixels are discrete, inches are not, if you're talking inches on your monitor, you need to know (at the very least) the resolution (and pixel aspect ratio) and the size of the visible monitor area in order to calculate your DPI. The resolution is usually possible to fetch somewhere (I'm not a C# or .NET programmer, so I can't help you there), but the size of the monitor is usually not available. If an estimate is good enough then have the user enter the size of the monitor (i.e. 21" or whatever) and solve for DPI:

    (resX/DPI)^2 + (resY/DPI)^2 = screenDiagonal^2
    

    giving (assuming you know the diagonal and the resolution)

    DPI = sqrt(resX^2+resY^2)/screenDiagonal
    

    This is just an estimate, as monitors are never exactly 21" (.. or whatever), and the pixel aspect ratio is hardly ever exactly 1:1.

    If you're talking inches on paper, then, quite naturally you need to know the DPI of your printer (or, more accurately, the current printer settings).

提交回复
热议问题