ScaleX alternative

  • Thread starter Thread starter Martin Horn
  • Start date Start date
M

Martin Horn

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.
 
Oops hit enter too soon... obviously MillimetersFromPixels will Return
(Pixels / sgDpiX) * 25.4
 
Larry said:
Oops hit enter too soon... obviously MillimetersFromPixels will Return
(Pixels / sgDpiX) * 25.4

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.CreateGraphics().DpiX

then you can have a function

Private Function MillimetersFromPixels(Pixels As Integer) As Double
Return (Pixels / sgDpiX) * 25.4
End Function
 
Martin,
In addition to the other comments.

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


Graphics.PageUnit & Graphics.PageScale 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.ScaleTransform (along with Graphics.TranslateTransform &
Graphics.RotateTransform) 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.Millimeter
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.DrawRectangle(Pens.White, 25, 25, 50, 50)

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

gr.ScaleTransform(1 / 2, 1 / 2)

gr.ScaleTransform(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.Inch


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

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

gr.PageScale = 1
gr.PageUnit = GraphicsUnit.Millimeter

Dim origin As New Point(10, 10)

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

gr.TransformPoints(Drawing2D.CoordinateSpace.Device,
Drawing2D.CoordinateSpace.Page, pts)

Debug.WriteLine(pts(0))

Graphics.TransformPoints 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 &
TranformRectangles 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 "Programming 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
 
This is very helpful, thankyou.

Jay B. Harlow said:
Martin,
In addition to the other comments.

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


Graphics.PageUnit & Graphics.PageScale 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.ScaleTransform (along with Graphics.TranslateTransform &
Graphics.RotateTransform) 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.Millimeter
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.DrawRectangle(Pens.White, 25, 25, 50, 50)

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

gr.ScaleTransform(1 / 2, 1 / 2)

gr.ScaleTransform(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.Inch


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

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

gr.PageScale = 1
gr.PageUnit = GraphicsUnit.Millimeter

Dim origin As New Point(10, 10)

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

gr.TransformPoints(Drawing2D.CoordinateSpace.Device,
Drawing2D.CoordinateSpace.Page, pts)

Debug.WriteLine(pts(0))

Graphics.TransformPoints 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 &
TranformRectangles 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 "Programming 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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top