Converting Millimeters to Pixels using VB.NET

esteuart

Member
Joined
May 6, 2003
Messages
11
I need to draw a rectangle exactly in the middle of a panel. I know the exact size of the rectangle, in Millimeters, that I need, and I can figure out the size of the panel in pixels. I need to convert the millimeters to pixels so that I can figure the Location for the rectangle.

Does anyone know how to convert from Millimeters to pixels based on the current resolution??:confused:
 
Hi

In my opinion, it is possible to convert milimeter to pixel ... However, before I suggest the solution, I would like to understand few terms/phrases in your message. What do you mean:

[1] panel
[2] the size of panel in pixels
[3] current resolution

TQvM
 
Hi esteuart,

Wait ... sorry ... I think I know which panel that you talked about ... its a control in VB .NET, right?

Well ... I try to answer ... I guess youre using Windows XP. If thats the case, go to Start Menu -> Programs -> Accessories -> Paint.

Once Paint is opened, click Image menu -> Attributes... Make sure that the option button of Pixels is checked. Then type 100 for Width and 100 for Height, and after that press OK. You will see that page (by default, the color of the page should be white) shrinks to the size defined by the values of Width and Height.

Again, you open the Attributes windows. This time, you check the option button of Cm. Please note that the values in Width and Height will change automatically. Lets say the new value for Width is 3.13, so it means that 100 pixels is equivalent to 3.13 cm or 31.3mm.

For more detail, please read the attachment.

TQvM
 
There is no fixed value for how many millimetres are in a pixel or vice versa, it depends on how many DPI (Dots Per Inch) you are working with.
 
Hi,

I agree with Divil that there is no FIXED value for for how many millimetres are in a pixel or vice versa. By the way, Divil, how do you learn that the value of DPI (Dots Per Inch) that we are working with?

Can I say that Dots Per Inch is EQUIVALENT to Pixels Per Inch?

TQvM
 
I would like to add some useful links to my previous messages.

I guess I found the answer that DPI is NOT EQUAIVALENT to PPI (Pixels Per Inch). [Please refer to the following link: http://www.computerhope.com/jargon/p/ppi.htm]

The definition of some useful video/image related terms can be found as follows:
----------
DPI: http://www.computerhope.com/jargon/d/dpi.htm or http://whatis.techtarget.com/definition/0,,sid9_gci213912,00.html

Pixel: http://www.computerhope.com/jargon/p/pixel.htm or http://whatis.techtarget.com/definition/0,,sid9_gci212793,00.html

Resolution: http://www.computerhope.com/jargon/r/resoluti.htm or http://whatis.techtarget.com/definition/0,,sid9_gci212895,00.html

DOT Pitch: http://www.computerhope.com/jargon/d/dotpitch.htm or http://whatis.techtarget.com/definition/0,,sid9_gci211995,00.html
----------

If you refer to the definition of DOT Pitch, i.e. Distance between pixels on a computer display screen measured in millimeters from http://www.computerhope.com/jargon/d/dotpitch.htm, DOT Pitch is monitor dependence. If you notice the specifications of different brands of monitor, you will find that DOT Pitch of them might be different from others. So, again, as mentioned by Divil, there is no FIXED value for how many pixels in a milimeter.

Lets refer back the Microsoft Paint example given before. The value, i.e. 100 pixel = 3.31cm (1.23inches), is obtained when the screen resolution of my monitor is set to be 1024 by 768 pixels. In this case, as shown in the Attributes window of MS Paint, the DPI is 81 x 81 dots per inch. Lets say now I set my screen resolution to be 800 by 600 pixels, the relationship between pixel & cm or pixel & inch becomes 100 pixel = 4.00cm or 100 pixel = 1.58inches respectively. For this case, the DPI is 64 x 64 dots per inch.

The following sentences are quoted from http://whatis.techtarget.com/definition/0,,sid9_gci212895,00.html -

Display resolution is not measured in dots per inch as it usually is with printers. However, the resolution and the physical monitor size together do let you determine the pixels per inch (and therefore, pixels per milimeter by doing little unit conversion). Typically, PC monitors have somewhere between 50 and 100 pixels per inch. For example, a 15-inch display modes monitor has a resolution of 640 pixels along a 12-inch horizontal line or about 53 pixels per inch. A smaller VGA display would have more pixels per inch.

So, esteuart, I hope this could help ...

TQvM
 
I understand that there is no FIXED value and that it is monitor and resolution dependent. That, in fact, is my problem. I want the rectangle that I draw (using GDI+ in VB.NET) to be actual size no matter what the resolution or monitor size is. I need a way to calculate what the size SHOULD be in pixels based on the actual size in Millimeters.

The graphics class will let me change the unit to Millimeters and then when I draw the rectangle it is the right size. The problem is then I need to center it in a Panel (a VB component). The panel is defined in Pixels, so I cant figure out where the top left corner of the rectangle should be.

The top-left corner is based on the following equation:
PanelWidth / 2 - RectangleWidth / 2 and
PanelHeight / 2 - RectangleHeight / 2.

This will give me the left and top points, respectively. The problem is that the rectangle is in Millimeters and the Panel is in Pixels, so the math wont work. Therefore I need to convert one or the other to the same unit of measure so that I can make the above equation work.

I should note that this needs to be dynamic because this application could be run on various monitors and resolutions.

Any suggestions?

Eric
 
Ill throw this out for you - if you really want to get it to appear as the exact millimeter size on the screen, youll need to know the monitor size as well. And not just 17" or 19" or 21" (for lucky people like me :)), but how much of that is visible, do they have a black border around the edge, etc.

The point is, you wont be able to get exact. Now, if you final destination is a printer or other output device, there are methods to scale your pixel dimensions to millimeters for a printer, which WILL be exact (assuming the math is right and the printer is accurate enough). Trying to get an EXACT size on a monitor is close to a lost cause in my opinion.

-Nerseus
 
Right now Im working on the screen version, but it will be going out to a printer in the end. Im glad to hear that there is a way to make it accurate for printing as this is a must. Someone on another forum gave me some code that does a pretty good job of figuring the size for the screen. It isnt accurate, but it will work for my purposes. I will post the code below. The problem with it is when I change my resolution, it isnt very accurate at all, but like I said, it will work.

Heres the code:

rect.Width = (Graphics.DpiX / MM_PER_INCH) * RECT_WIDTH_IN_MM
rect.Height = (Graphics.DpiY / MM_PER_INCH) * RECT_HEIGHT_IN_MM

rect.Location = New Point((Panel.Width - rect.Width) / 2, (Panel.Height - rect.Height) / 2)

MM_PER_INCH is a constant of 25.4F. It is the number of Millimeters in an Inch. The other constants are the actual size of the Rectangle.

Thanks for your help,
Eric
 
For printing this kind of thing is pretty easy. Printing is designed so, being aware of paper size, you can work with any units. No program that Im aware of attempts to accurately display measurements onscreen.

Youre probably best off allowing the user to select DPI (usually around 72 for monitors running at their recommended resolution) and then draw in pixels based on that.
 
72 dpi is normal for a monitor? Maybe just in England...? My monitor says "Normal Size 96dpi". I guess its another reason to not worry too much about the exact size on the screen :)

-nerseus
 
The Windows API method for this is ...

Essentially you need to look into the api method - GetDeviceCaps. That is the standard Windows API call that should be used for all measurement determinations.

Good luck!
 
graphics.PageUnit = GraphicsUnit.Millimeter
graphics.DrawRectangle(pen,rect)


esteuart said:
I understand that there is no FIXED value and that it is monitor and resolution dependent. That, in fact, is my problem. I want the rectangle that I draw (using GDI+ in VB.NET) to be actual size no matter what the resolution or monitor size is. I need a way to calculate what the size SHOULD be in pixels based on the actual size in Millimeters.

The graphics class will let me change the unit to Millimeters and then when I draw the rectangle it is the right size. The problem is then I need to center it in a Panel (a VB component). The panel is defined in Pixels, so I cant figure out where the top left corner of the rectangle should be.

The top-left corner is based on the following equation:
PanelWidth / 2 - RectangleWidth / 2 and
PanelHeight / 2 - RectangleHeight / 2.

This will give me the left and top points, respectively. The problem is that the rectangle is in Millimeters and the Panel is in Pixels, so the math wont work. Therefore I need to convert one or the other to the same unit of measure so that I can make the above equation work.

I should note that this needs to be dynamic because this application could be run on various monitors and resolutions.

Any suggestions?

Eric
 
Back
Top