ScaleX alternative

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Martin Horn

    ScaleX alternative

    Hi,

    can someone provide a VB .Net equivalent to the following line of code:

    Debug.Print ScaleX(10, vbMillimeters, vbPixels) which produces the
    result 37.79524

    I have an idea that the graphics object ScaleTransform function may be what
    I need to use, however I have been unable to locate an example that either
    applies to the above or that I can easily understand.

    Thanks,

    Martin Horn.


  • Larry Lard

    #2
    Re: ScaleX alternative

    Oops hit enter too soon... obviously MillimetersFrom Pixels will Return
    (Pixels / sgDpiX) * 25.4

    Comment

    • Larry Lard

      #3
      Re: ScaleX alternative


      Larry Lard wrote:[color=blue]
      > Oops hit enter too soon... obviously MillimetersFrom Pixels will[/color]
      Return[color=blue]
      > (Pixels / sgDpiX) * 25.4[/color]

      Or maybe I actually hit Cancel! In which case the above will make no
      sense!

      What was in my lost post is this:

      All I could find of relevance was that the Graphics object has a DpiX
      property that returns the dots (pixels for a screen) per inch. So if
      you have a Windows Form, add a

      Private sgDpiX As Single

      then during the form startup do

      sgDpiX = Me.CreateGraphi cs().DpiX

      then you can have a function

      Private Function MillimetersFrom Pixels(Pixels As Integer) As Double
      Return (Pixels / sgDpiX) * 25.4
      End Function

      --
      Larry Lard
      Replies to group please

      Comment

      • Jay B. Harlow [MVP - Outlook]

        #4
        Re: ScaleX alternative

        Martin,
        In addition to the other comments.

        The Graphics object supports 3 coordinate spaces:
        - Device
        - Page
        - World


        Graphics.PageUn it & Graphics.PageSc ale are used for Page Coordinate
        Spaces/Transforms, where you want to convert the units you are drawing with
        (millimeters) to the physical units on the output device (pixels).

        Graphics.ScaleT ransform (along with Graphics.Transl ateTransform &
        Graphics.Rotate Transform) are used for World Coordinate Spaces/Transforms,
        which simply is changing your point of view in relation to the drawing, for
        example I think of ScaleTransform as Zooming into & out of the image...


        For example if the "normal" resolution of my drawing was millimeters I would
        use the following to initialize my graphics object:

        Dim gr As Graphics
        gr.PageUnit = GraphicsUnit.Mi llimeter
        gr.PageScale = 1

        This way when ever I call any Draw or Fill method on this Graphics object,
        the units I supplied would be in Millimeters, and I don't need to be
        concerned with DPI & such...

        ' Draw a box 50 millimeters wide, 25 millimeters from the top & left
        gr.DrawRectangl e(Pens.White, 25, 25, 50, 50)

        If I wanted to zoom into or out of this drawing, I would use
        Graphics.ScaleT ransform:

        gr.ScaleTransfo rm(1 / 2, 1 / 2)

        gr.ScaleTransfo rm(2, 2)

        The drawing would first be adjusted by the World Transform (the
        ScaleTransform) then it would be adjust by the Page Transform (PageUnit).

        The PageScale above is useful when the "normal" resolution is a fraction of
        the Unit (for example 100th of an Inch).

        gr.PageScale = 0.01
        gr.PageUnit = GraphicsUnit.In ch


        You can use Graphics.Transf ormPoints to translate points to & from the
        various coordinate spaces:

        Dim bm As New Bitmap(100, 100)
        Dim gr As Graphics = Graphics.FromIm age(bm)

        gr.PageScale = 1
        gr.PageUnit = GraphicsUnit.Mi llimeter

        Dim origin As New Point(10, 10)

        Dim pts() As Point = New Point() {origin}

        gr.TransformPoi nts(Drawing2D.C oordinateSpace. Device,
        Drawing2D.Coord inateSpace.Page , pts)

        Debug.WriteLine (pts(0))

        Graphics.Transf ormPoints is overloaded for both Point & PointF structures.
        Watch the parameters to TransformPoints , the order is not what I would
        normally expect...

        I normally create helper functions that allow my to TransformSizes &
        TranformRectang les based on the needs of the project...

        I also normally include all the Graphics initialization in a common routine,
        so its easier to find...

        Charles Petzold's book "Programmin g Microsoft Windows With Microsoft Visual
        Basic .NET - Core Reference" from MS Press includes an extensive section on
        how Page & World transforms work.

        Hope this helps
        Jay


        "Martin Horn" <martin@nospam. mhorn.co.uk> wrote in message
        news:uNnNd.1081 $Cq.570@newsfe1-win.ntli.net...[color=blue]
        > Hi,
        >
        > can someone provide a VB .Net equivalent to the following line of code:
        >
        > Debug.Print ScaleX(10, vbMillimeters, vbPixels) which produces the
        > result 37.79524
        >
        > I have an idea that the graphics object ScaleTransform function may be
        > what I need to use, however I have been unable to locate an example that
        > either applies to the above or that I can easily understand.
        >
        > Thanks,
        >
        > Martin Horn.
        >[/color]


        Comment

        • Martin Horn

          #5
          Re: ScaleX alternative

          Thanks everyone

          "Martin Horn" <martin@nospam. mhorn.co.uk> wrote in message
          news:uNnNd.1081 $Cq.570@newsfe1-win.ntli.net...[color=blue]
          > Hi,
          >
          > can someone provide a VB .Net equivalent to the following line of code:
          >
          > Debug.Print ScaleX(10, vbMillimeters, vbPixels) which produces the
          > result 37.79524
          >
          > I have an idea that the graphics object ScaleTransform function may be
          > what I need to use, however I have been unable to locate an example that
          > either applies to the above or that I can easily understand.
          >
          > Thanks,
          >
          > Martin Horn.
          >[/color]


          Comment

          • Martin Horn

            #6
            Re: ScaleX alternative

            This is very helpful, thankyou.

            "Jay B. Harlow [MVP - Outlook]" <Jay_Harlow_MVP @msn.com> wrote in message
            news:eWde2NfDFH A.464@TK2MSFTNG P15.phx.gbl...[color=blue]
            > Martin,
            > In addition to the other comments.
            >
            > The Graphics object supports 3 coordinate spaces:
            > - Device
            > - Page
            > - World
            >
            >
            > Graphics.PageUn it & Graphics.PageSc ale are used for Page Coordinate
            > Spaces/Transforms, where you want to convert the units you are drawing
            > with (millimeters) to the physical units on the output device (pixels).
            >
            > Graphics.ScaleT ransform (along with Graphics.Transl ateTransform &
            > Graphics.Rotate Transform) are used for World Coordinate Spaces/Transforms,
            > which simply is changing your point of view in relation to the drawing,
            > for example I think of ScaleTransform as Zooming into & out of the
            > image...
            >
            >
            > For example if the "normal" resolution of my drawing was millimeters I
            > would use the following to initialize my graphics object:
            >
            > Dim gr As Graphics
            > gr.PageUnit = GraphicsUnit.Mi llimeter
            > gr.PageScale = 1
            >
            > This way when ever I call any Draw or Fill method on this Graphics object,
            > the units I supplied would be in Millimeters, and I don't need to be
            > concerned with DPI & such...
            >
            > ' Draw a box 50 millimeters wide, 25 millimeters from the top &
            > left
            > gr.DrawRectangl e(Pens.White, 25, 25, 50, 50)
            >
            > If I wanted to zoom into or out of this drawing, I would use
            > Graphics.ScaleT ransform:
            >
            > gr.ScaleTransfo rm(1 / 2, 1 / 2)
            >
            > gr.ScaleTransfo rm(2, 2)
            >
            > The drawing would first be adjusted by the World Transform (the
            > ScaleTransform) then it would be adjust by the Page Transform (PageUnit).
            >
            > The PageScale above is useful when the "normal" resolution is a fraction
            > of the Unit (for example 100th of an Inch).
            >
            > gr.PageScale = 0.01
            > gr.PageUnit = GraphicsUnit.In ch
            >
            >
            > You can use Graphics.Transf ormPoints to translate points to & from the
            > various coordinate spaces:
            >
            > Dim bm As New Bitmap(100, 100)
            > Dim gr As Graphics = Graphics.FromIm age(bm)
            >
            > gr.PageScale = 1
            > gr.PageUnit = GraphicsUnit.Mi llimeter
            >
            > Dim origin As New Point(10, 10)
            >
            > Dim pts() As Point = New Point() {origin}
            >
            > gr.TransformPoi nts(Drawing2D.C oordinateSpace. Device,
            > Drawing2D.Coord inateSpace.Page , pts)
            >
            > Debug.WriteLine (pts(0))
            >
            > Graphics.Transf ormPoints is overloaded for both Point & PointF structures.
            > Watch the parameters to TransformPoints , the order is not what I would
            > normally expect...
            >
            > I normally create helper functions that allow my to TransformSizes &
            > TranformRectang les based on the needs of the project...
            >
            > I also normally include all the Graphics initialization in a common
            > routine, so its easier to find...
            >
            > Charles Petzold's book "Programmin g Microsoft Windows With Microsoft
            > Visual
            > Basic .NET - Core Reference" from MS Press includes an extensive section
            > on how Page & World transforms work.
            >
            > Hope this helps
            > Jay
            >
            >
            > "Martin Horn" <martin@nospam. mhorn.co.uk> wrote in message
            > news:uNnNd.1081 $Cq.570@newsfe1-win.ntli.net...[color=green]
            >> Hi,
            >>
            >> can someone provide a VB .Net equivalent to the following line of code:
            >>
            >> Debug.Print ScaleX(10, vbMillimeters, vbPixels) which produces the
            >> result 37.79524
            >>
            >> I have an idea that the graphics object ScaleTransform function may be
            >> what I need to use, however I have been unable to locate an example that
            >> either applies to the above or that I can easily understand.
            >>
            >> Thanks,
            >>
            >> Martin Horn.
            >>[/color]
            >
            >[/color]


            Comment

            Working...