Centimeter to pixel

前端 未结 6 1256
深忆病人
深忆病人 2021-01-11 18:55

How do I convert centimeter to pixel in c# ?

相关标签:
6条回答
  • 2021-01-11 19:40

    You can use the DpiX and DpiY properties of the Graphics object on which you're drawing (which you must have, since conversion is meaningless in the absence of a graphics context of some kind.)

    In DpiX and DpiY, the "D" stands for "dots" or pixels, while the "i" stands for "inches". So, it will convert pixels to inches. Then all you have to do is convert inches to centimeters => (x * 2.54)

    Also if you want to be more "precise", have a look at the following: HOWTO: How to Make an Application Display Real Units of Measurement

    0 讨论(0)
  • 2021-01-11 19:45
    int CentimeterToPixel(double Centimeter)
    {
        double pixel = -1;
        using (Graphics g = this.CreateGraphics())
        {
            pixel = Centimeter * g.DpiY / 2.54d;
        }
        return (int)pixel;
    }
    0 讨论(0)
  • 2021-01-11 19:45

    In WPF, one centimeter is about 37.8 device independent pixels.

    0 讨论(0)
  • 2021-01-11 19:47

    Pixel and centimeter are two different Units they are calculated according to the User's DPI setting. To convert correctly you need to know the DPI of the User Screen.

    If you have a 12.8 centimeter display showing a 1280x1024 image, then you have 100 pixels per centimeter.

    However you can try using the Graphics.TransformPoints to convert from pixel to cm or opposite.

    0 讨论(0)
  • 2021-01-11 19:51

    As I said in my comment, you'll have to give more information. (Is this a Windows Forms app? ASP.Net?)

    The fundamental approach is:

    • Find out the DPI of the output device in question. (It's frequently 96 [96 dots per inch], but you cannot assume that.) This thread may help you do that, if it's a Windows Forms app. Also, the Graphics class has the DpiX and DpiY members, so you can use those.
    • Convert the DPI to DPC [dots-per-centimeter] (DPC = DPI / 2.54).
    • Multiply your number of centimeters by your DPC value.
    0 讨论(0)
  • 2021-01-11 19:52

    Length in CM = LenghtPixels * 2.54 / DPI

    I think you can get the DPI of every display using WMI, the value you're searching for is in the class Win32_DisplayConfiguration, under the field LogPixe.

    Check this link about using WMI to retrieve data: LINK

    0 讨论(0)
提交回复
热议问题