Converting Units 
Author Message
 Converting Units

Hi, how many pixels are there in 1 inch? And how many pixels are there in 1
millimeter?

Thanks
Tony



Mon, 07 Feb 2005 00:30:28 GMT  
 Converting Units
This would depend entirely on the resolution that your display or printer
is running (ie, at 640x480, there are fewer pixels per inch than at
1024x768).

If you'll specify the circumstances under which you need to determine this
value, I can assist further.

Steven Bras, MCSD
Microsoft Developer Support/Visual Basic WebData

This posting is provided "AS IS" with no warranties, and confers no rights.

Are you secure? For information about the Microsoft Strategic Technology
Protection Program and to order your FREE Security Tool Kit, please visit
http://www.microsoft.com/security.



Mon, 07 Feb 2005 00:58:23 GMT  
 Converting Units
I need to build a ruler control.  However, how do I get the resolution of
the user's monitor?  Is there a formular that I can use to calculate the
pixels per inch at a given resolution?

I tried the dpiX and the dpiY properties of the Graphics object, but that
doesn't seems to work...

Thanks in advance
Tony



Quote:
> This would depend entirely on the resolution that your display or printer
> is running (ie, at 640x480, there are fewer pixels per inch than at
> 1024x768).

> If you'll specify the circumstances under which you need to determine this
> value, I can assist further.

> Steven Bras, MCSD
> Microsoft Developer Support/Visual Basic WebData

> This posting is provided "AS IS" with no warranties, and confers no
rights.

> Are you secure? For information about the Microsoft Strategic Technology
> Protection Program and to order your FREE Security Tool Kit, please visit
> http://www.microsoft.com/security.



Mon, 07 Feb 2005 01:57:11 GMT  
 Converting Units

Quote:
> Hi, how many pixels are there in 1 inch? And how many pixels are
> there in 1 millimeter?

That's device dependent. For the screen:

    Dim HScreen As Integer
    Dim XPPI, YPPI As Integer

    HScreen = Win32.GetDC(0)
    XPPI = Win32.GetDeviceCaps(HScreen, Win32.LOGPIXELSX)
    YPPI = Win32.GetDeviceCaps(HScreen, Win32.LOGPIXELSY)
    Win32.ReleaseDC(0, HScreen)

    MsgBox(String.Format("XPPI: {0}, YPPI: {1}", XPPI, YPPI))

...

Public Class Win32

   Public Const LOGPIXELSX As Integer = 88
   Public Const LOGPIXELSY As Integer = 90

   Public Declare Function GetDC Lib "user32" Alias "GetDC" _
      (ByVal hwnd As Integer) As Integer

   Public Declare Function ReleaseDC Lib "user32" Alias "ReleaseDC" _
      (ByVal hwnd As Integer, ByVal hdc As Integer) As Integer

   Public Declare Function GetDeviceCaps Lib "gdi32" _
      Alias "GetDeviceCaps" (ByVal hdc As Integer, _
      ByVal nIndex As Integer) As Integer

End Class

The result depends on the value the user selects in the system's displays
settings in control panel because the OS can't know the real value. The
unit is pixels per inch. Divide the value by 25.4 and you'll have the pixels
per millimeter.

Armin



Mon, 07 Feb 2005 01:58:13 GMT  
 Converting Units
Thanks Armin! Good answer.

Steven Bras, MCSD
Microsoft Developer Support/Visual Basic WebData

This posting is provided "AS IS" with no warranties, and confers no rights.

Are you secure? For information about the Microsoft Strategic Technology
Protection Program and to order your FREE Security Tool Kit, please visit
http://www.microsoft.com/security.



Mon, 07 Feb 2005 03:41:10 GMT  
 Converting Units

Quote:
> I need to build a ruler control.  However, how do I get the
> resolution of the user's monitor?  Is there a formular that I
> can use to calculate the pixels per inch at a given resolution?

> I tried the dpiX and the dpiY properties of the Graphics object,
> but that doesn't seems to work...

I didn't see dpiX and dpiY so far that's why I pointed you to GetDeviceCaps.
But these properties should work.

Hey, I wrote something for you:
Here's a ruler control you can use for your application. It enables the user
to adjust the real pixels-per-inch-relation using the left mouse button only
for your application and not for the whole system. You can put the control
somewhere in an options dialog for the application. Use the value of the
PPIX property whenever you want to calculate the pixels per inch on the
screen.

It isn't the prettiest one yet and doesn't take dpiy into account, but here
it is:

Public Class Ruler
   Inherits System.Windows.Forms.PictureBox

#Region "Private Class DragData"
   Private Class DragData
      Public ReadOnly MouseX As Integer      'Not yet used
      Public ReadOnly Inches As Single
      Public ReadOnly PPIX As Single

      Public Sub New( _
         ByVal MouseX As Integer, _
         ByVal Inches As Single, _
         ByVal PPIX As Single)

         Me.MouseX = MouseX
         Me.Inches = Inches
         Me.PPIX = PPIX
      End Sub
   End Class
#End Region

   Public Event PPIXChanged()

   Private mPPIX As Single
   Private mDragData As DragData
   Private mFixedTextSize As Boolean

   Public Sub New()
      MyBase.New()
      ResetPPIX()
      CreateImage()
      Render()
   End Sub

   Protected Overrides Sub OnResize(ByVal e As System.EventArgs)
      CreateImage()
      Render()
   End Sub

   Private Sub CreateImage()
      MyBase.Image = New Bitmap(Me.ClientSize.Width, Me.ClientSize.Height)
   End Sub

   Private Sub Render()

      Dim TextHeight As Integer     'Unit: pixels
      Dim g As Graphics
      Dim ClientWidth, ClientHeight, h2 As Integer
      Dim i As Integer
      Dim Font As Font
      Dim Brush As New SolidBrush(SystemColors.WindowText)
      Dim Pen As New Pen(SystemColors.WindowText)

      If mFixedTextSize Then
         TextHeight = CInt(10 * DefaultPPIX / 72)
      Else
         TextHeight = CInt(10 * mPPIX / 72)
      End If

      Font = New Font("Arial", TextHeight, GraphicsUnit.Pixel)

      ClientWidth = Me.ClientSize.Width
      ClientHeight = Me.ClientSize.Height
      h2 = ClientHeight \ 2

      g = Graphics.FromImage(MyBase.Image)
      g.Clear(BackColor)
      g.DrawLine(Pen, 0, h2, ClientWidth, h2)

      'Draw lines. Unit: Inches
      Do
         Dim x As Integer
         x = CInt(i * mPPIX)
         If x > ClientWidth Then
            Exit Do
         End If
         g.DrawLine(Pen, x, 0, x, h2)
         g.DrawString(i.ToString, Font, Brush, x + 2, 0)
         i += 1
      Loop

      'Draw lines. Unit: cm

      i = 0
      Dim TextY As Integer = ClientHeight - TextHeight - 2
      Do
         Dim x As Integer
         x = CInt(i * mPPIX / 2.54)
         If x > ClientWidth Then
            Exit Do
         End If
         g.DrawLine(Pen, x, ClientHeight, x, h2)
         g.DrawString(i.ToString, Font, Brush, x + 2, TextY)
         i += 1
      Loop

      g.Dispose()
      Font.Dispose()
      Brush.Dispose()

      Me.Refresh()
   End Sub

   Public Property PPIX() As Single
      Get
         Return mPPIX
      End Get
      Set(ByVal Value As Single)
         If Value <= 0 Then
            Throw New ArgumentException( _
               "Value must be greater than zero", "Value" _
            )
         End If

         Value = Math.Min(Math.Max(Value, 10), 1000)

         If Value <> mPPIX Then
            mPPIX = Value
            RaiseEvent PPIXChanged()
            Render()
         End If
      End Set
   End Property

   Protected Overrides Sub OnMouseDown( _
      ByVal e As System.Windows.Forms.MouseEventArgs)

      If (e.Button And MouseButtons.Left) > 0 Then
         If mDragData Is Nothing Then
            mDragData = New DragData(e.X, PixelsToInches(e.X), mPPIX)
            Me.Cursor = Cursors.Hand
         End If
      ElseIf (e.Button And MouseButtons.Right) > 0 Then
         If Not mDragData Is Nothing Then
            PPIX = mDragData.PPIX
            StopDragging()
         End If
      End If
   End Sub

   Private Function PixelsToInches(ByVal Pixels As Integer) As Single
      Return Pixels / mPPIX
   End Function

   Protected Overrides Sub OnMouseMove( _
      ByVal e As System.Windows.Forms.MouseEventArgs)

      If Not mDragData Is Nothing Then
         If (e.Button And MouseButtons.Left) > 0 Then
            PPIX = Math.Max(e.X, 1) / mDragData.Inches
         Else
            StopDragging()
         End If
      End If
   End Sub

   Private Sub StopDragging()
      Me.Cursor = Cursors.Default
      mDragData = Nothing
   End Sub

   Protected Overrides Sub OnMouseUp( _
      ByVal e As System.Windows.Forms.MouseEventArgs)

      If (e.Button And MouseButtons.Left) > 0 _
         AndAlso Not mDragData Is Nothing Then

         StopDragging()
      End If
   End Sub

   Public Overrides Property BackColor() As System.Drawing.Color
      Get
         Return MyBase.BackColor
      End Get
      Set(ByVal Value As System.Drawing.Color)
         MyBase.BackColor = Value
         Render()
      End Set
   End Property

   Public Shadows ReadOnly Property Image() As Image
      Get
         Return MyBase.Image
      End Get
   End Property

   Public ReadOnly Property DefaultPPIX() As Single
      Get
         Dim g As Graphics = CreateGraphics()
         DefaultPPIX = g.DpiX
         g.Dispose()
      End Get
   End Property

   Public Sub ResetPPIX()
      mPPIX = DefaultPPIX
   End Sub

   Public Property FixedTextSize() As Boolean
      Get
         Return mFixedTextSize
      End Get
      Set(ByVal Value As Boolean)
         If Value <> mFixedTextSize Then
            mFixedTextSize = Value
            Render()
         End If
      End Set
   End Property

End Class



Mon, 07 Feb 2005 09:18:03 GMT  
 
 [ 6 post ] 

 Relevant Pages 

1. Converting english to si units

2. How do I convert decimals into units?

3. How to convert from device units to Centimeters/inches/Twips

4. Macro to set resource units to Max units

5. Change default unit of measurement from inches to twips

6. VBA for lookup unit of measure

7. Acc2.0:Getting CD drive unit letter

8. Getting CD unit letter Acc 2.0

9. Need to count the number of units per task

10. Determining Units for Resource StandardRate

11. Calculating resource units

12. Assignment Units for Materials resources

 

 
Powered by phpBB® Forum Software