Converting to inches

  • Thread starter Thread starter Brent Hoskisson
  • Start date Start date
B

Brent Hoskisson

What is a unit of height and width of a form measured as
(pixels?) and how can I convert that measurement to inches?

Thanks
Brent
 
The size of a form is measured in pixels. The normal measurement is 96
pixels per inch (although this apparently isn't always true, see the link
below.)

However, if you're trying to determine the actual size of a form on the
monitor, that will be difficult -- it's unlikely that the user's monitor
will be displaying exactly 96 pixels per inch. Consider that you can change
the screen resolution, for example, from 800 x 600 to 1024 x 768. The form
might be the "right" size in inches at one resolution, but won't be at
another resolution. To calculate the actual size, you'd need to know the
viewable size of the monitor, along with the environment's screen
resolution.

http://msdn.microsoft.com/library/default.asp?url=/workshop/author/dhtml/overview/highdpi.asp

http://msdn.microsoft.com/library/d...dtransformations/typesofcoordinatesystems.asp
 
Hi Brent,

A Form's width is in pixels, as you've found. Each Form (actually Control)
has a drawing surface controlled by a Graphics object. This has a property
DpiX which is Dots-Per-Inch-Horizontal, and a corresponding DpiY.

Try this:
Dim g As Graphics
g = SomeForm.CreateGraphics

MsgBox ("DpiX " & g.DpiX & vbCrLf _
"Width (mm) " & SomeForm.Width & vbCrLf _
"Width (inches) = " & SomeForm.Width / g.DpiX)

Regards,
Fergus
 
Hello,

Fergus Cooney said:
Try this:
Dim g As Graphics
g = SomeForm.CreateGraphics

MsgBox ("DpiX " & g.DpiX & vbCrLf _
"Width (mm) " & SomeForm.Width & vbCrLf _
"Width (inches) = " & SomeForm.Width / g.DpiX)

\\\
g.Dispose()
///

SCNR
 
Hi Herfried,

Thanks, I'm glad you didn't resist. It <is> better to use Dispose rather
than leaving it to the garbage collector. :-)

Regards,
Fergus
 
Hello,

Fergus Cooney said:
Thanks, I'm glad you didn't resist. It <is> better to use Dispose rather
than leaving it to the garbage collector. :-)

For pens, brushes, graphics objects etc. the 'Dispose' method should always
(...) be called by the programmer, this will free unmanaged ressources.
 
Hi Herfried,

|| For pens, brushes, graphics objects etc. the 'Dispose'
|| method should always be called by the programmer.
|| This will free unmanaged ressources ...

.. earlier, and more timely, than the garbage collector - which will free
them when it eventually calls the object's Finalize routine.

Regards,
Fergus
 
Hi Herfried,

I've just discovered that C# has a special syntax for Dispose.

Graphics g = pic.CreateGraphics();
using (g)
{
g.DrawThisThatAndTheOther (x, y, p)
// Do other stuff with g.
: : :
} // g.Dispose is called automatically here.

Yet another shortcut that C# provides. I wonder why they left it out of
VB?

Regards,
Fergus
 
Hi Fergus,
Maybe to compensate all the extra possibilities VB got from
Microsoft.visualbasic
:-)
Cor
 
Hello,

Fergus Cooney said:
I've just discovered that C# has a special syntax for
Dispose.

Graphics g = pic.CreateGraphics();
using (g)
{
g.DrawThisThatAndTheOther (x, y, p)
// Do other stuff with g.
: : :
} // g.Dispose is called automatically here.

Yet another shortcut that C# provides. I wonder why they left
it out of VB?

I don't know, but I don't see big advantages in the 'using' syntax. It adds
one extra nesting level to the source code and hides the call to the
'Dispose' method.
 
Hello,

Cor said:
Maybe to compensate all the extra possibilities VB got from
Microsoft.visualbasic

LOL -- in C# they have operator overloading and unsafe code too... Maybe to
compensate all the extra possibilities VB got from 'Microsoft.VisualBasic'
too.

;-)
 
Hello,

Fergus Cooney said:
.. earlier, and more timely, than the garbage collector -
which will free them when it eventually calls the object's
Finalize routine.

Somewhere in the documentation I read that 'Dispose' should be called by the
programmer for all GDI+ objects. I think that was a "recommendation".

;-)
 
Hi Herfried,

You're right, dispose() <is> a recommendation. Why have GDI resources tied
up waiting for the non-deterministic GC when you can free them as soon as.

Regards,
Fergus
 
Back
Top