How do I convert centimeter to pixel in c# ?
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
int CentimeterToPixel(double Centimeter)
{
double pixel = -1;
using (Graphics g = this.CreateGraphics())
{
pixel = Centimeter * g.DpiY / 2.54d;
}
return (int)pixel;
}
In WPF, one centimeter is about 37.8 device independent pixels.
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.
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:
DpiX
and DpiY
members, so you can use those.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