Search code examples
c#wpfprintingresolutiondpi

Transform dpi/dots to pixel gives wrong result


Currently i to develop a small printer preview, but i have some problem with converting dots per mm / dpi to the pixels on the screen.

For example i have a Label which has the coordinations: top = 5 dots left = 20 dots

The printer has the resolution of 8 dots per mm / 203 dpi.

In my simple wpf application i have a canvas, where i want to place a label on that place, where it will be printed. So i need to convert the values 20 and 5 relative to my resolution on the screen. But the result in pixels are wrong. That's my calculation:

// start values
double top = 5;
double left = 20;

// let's try to calculate the mm. We use 8, because we have 8mm per dot
double mmtop = top / 8.0;
double mmleft = left / 8.0;

// let's turn mm to cm
double cmtop = mmtop * 10.0;
double cmleft = mmleft * 10.0;

// Lets calulate the pixels on the base of a 72dpi monitor
double dpitop = cmtop / 25.4 * 72.0;   // = 318.89
double dpileft = cmleft / 25.4 * 72.0; // = 17.71

That's the way i tried to calculate the pixel based position. What am i doing wrong?

Maybe i made some very easy mistake, thank you!


Solution

  • To convert from mm to cm you need to divide by 10 instead of multiplying by 10.