Drawing lines in millimeters in vb.net

Hi there,

How do I draw a line of X millimeters in a picturebox.
When putting a ruler onto the screen(monitor physically), the drawn line and ruler should be equal in size.

I need it for VB.Net

- Thanks ...
Who is Participating?
jrschererConnect With a Mentor CEOCommented:
Monitors can be old CRT or new LCD. With CRT i would say forget it. It will not be accurate nor stable. You may have a few % error.
for LCD you have to know the pixel size. Usually it is around .265mm, resulting in 96 dots per inch.
Take the width in mm of your screen and devide it by the total pixels in X (your Display Settings Property)
On my 17" LCD I have 340 mm and 1280 pixels equals to a pixel size of .2656mm equals to 95.63DPI

This is your base for drawing accurate size on screen. Now you can use any graphics unit

Here an extract drom help:
Member name Description
 Display Specifies the unit of measure of the display device. Typically pixels for video displays, and 1/100 inch for printers.  
 Document Specifies the document unit (1/300 inch) as the unit of measure.  
 Inch Specifies the inch as the unit of measure.  
 Millimeter Specifies the millimeter as the unit of measure.  
 Pixel Specifies a device pixel as the unit of measure.  
 Point Specifies a printer's point (1/72 inch) as the unit of measure.  
 World Specifies the world coordinate system unit as the unit of measure.  

How are you drawing the line? If you are using a graphics object, you can set the PageUnit to be GraphicsUnit.Millimeter.
buchholtAuthor Commented:
I'm drawing like this:

        Dim g As Graphics = e.Graphics
        Dim p As New Pen(Color.Black, 1)
        g.PageUnit = GraphicsUnit.Millimeter

        Dim a As New PointF(0, 50)
        Dim b As New PointF(100, 50)

        g.DrawLine(p, a, b)

... this does not give my a line on 100 millimeters as I would expect - but a 116 millimeter long line!!!
Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

What are you using to measure? Are you measuring on the monitor or on a printout? Try a screen capture, then print and measure.
buchholtAuthor Commented:
I am measuring on the monitor with a ruler
I imagine there is a fair amount of distortion. Your monitor might not be adjusted exactly right. If you go into the monitor settings, you can probably fiddle with the settings so whatever guide it offers will match your ruler. Then try measuring your line in VB.

Again, I suggest that you simply print out the line and measure it.

Oh, I forgot to say. The DPI setting of your computer is in ControlPanel/ Display/ Settings/ Advanced. Thsi setting is taken as the reference to convert between dimensional (mm or inches) and Pixels
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.