Recent

Author Topic: Canvas - Drawing in mm  (Read 3002 times)

celso

  • New member
  • *
  • Posts: 6
Canvas - Drawing in mm
« on: November 22, 2012, 08:13:10 pm »
I´m trying to draw a grid on the screen, but using mm as a unit of measure.

I´d read that it depends on the screen resolution, but I didn´t find any example on how to do it. My code to draw the grid is:

Code: [Select]
    Bitmap.Height := Height;
    Bitmap.Width  := Width;

    // Drawing the background
    Bitmap.Canvas.Pen.Color := clWhite;
    Bitmap.Canvas.Rectangle(0, 0, Width, Height);

    // Drawing the grid
    Bitmap.Canvas.Pen.Color := RGBToColor(243,243,243);
    for x:= 1 to (Width div 10) do
        for y:= 1 to (Height div 10) do
            Bitmap.Canvas.Rectangle( round((x-1)*10),round((y-1)*10),
                                     round(x*10),round(y*10));   

the grid draw lines with 10 of distance, I want to draw with 10mm of distance.

Thanks!

Blaazen

  • Hero Member
  • *****
  • Posts: 2852
  • POKE 54296,15
    • Eye-Candy Controls
Re: Canvas - Drawing in mm
« Reply #1 on: November 22, 2012, 08:26:20 pm »
Forms have the property PixelsPerInch (96 in my case). One inch is 25,4 mm.
So you should calculate: Distance := 10 * ppi / 25,4 (which is here ~ 38 px / 10mm)

But my laptop LCD (15.6") is 332 mm wide and has horizontally 1680 pixels. It results to ~50 px / 10mm.

It is > 20% difference.  :)

If you need exact value you will have to tune your program for each LCD / monitor.
Lazarus 2.1.0 r63594 FPC 3.3.1 r40507 x86_64-linux-qt Chakra, Qt 4.8.7/5.13.2, Plasma 5.17.3
Lazarus 1.8.2 r57369 FPC 3.0.4 i386-win32-win32/win64 Wine 3.21

Try Eye-Candy Controls: https://sourceforge.net/projects/eccontrols/files/

celso

  • New member
  • *
  • Posts: 6
Re: Canvas - Drawing in mm
« Reply #2 on: November 22, 2012, 09:03:00 pm »
I used your suggestion, but I got a PPI, of the screen:

Code: [Select]
xmm := 10 * ScreenInfo.PixelsPerInchX / 25.4;
ymm := 10 * ScreenInfo.PixelsPerInchY / 25.4;   

then I used these values as increment in x and y. Looks Ok, I´ll do some other tests.

But, thanks for the answer! 8)

 

TinyPortal © 2005-2018