Randy Vande Hei said:
Does anyone know how to change the the GDI+ coordinate system like you used
to be able to do in VB6 with the picturebox.scale method. The
picturebox.scale method took an x,y point defining the upper left corner and
a second x,y point defining the lower right corner.
Well, I sorta missed the old Scale method too so I decided to roll my own
once and for all. IIRC, if you wanted to (predictably) draw arcs in VB.OLD
you really needed to have "square" canvases so this method always uses the
same scaling factor horizontally and vertically. If you don't want/need that
you need to slightly change the code so that it calculates and uses 2
scalingfactors: one for the horizontal scaling and one for the vertical
scaling.
Private mWorld2DeviceXfm As New System.Drawing.Drawing2D.Matrix()
Private mDevice2WorldXfm As New System.Drawing.Drawing2D.Matrix()
Public Sub SetScale( _
ByVal xupperleft As Integer, ByVal yupperleft As Integer, _
ByVal xlowerright As Integer, ByVal ylowerright As Integer)
Dim XMirrorFactor As Single = CSng(Sign(xlowerright - xupperleft))
Dim ScaleFactor As Single
' compensate for non-square canvas
If Me.Size.Width < Me.Size.Height Then
ScaleFactor = CSng(Me.Size.Width) / Abs(xlowerright - xupperleft)
Else
ScaleFactor = CSng(Me.Size.Height) / Abs(yupperleft - ylowerright)
End If
' construct forward matrix (world coord -> device coord)
mWorld2DeviceXfm.Reset()
mWorld2DeviceXfm.Translate( CSng(-xupperleft), CSng(-yupperleft))
mWorld2DeviceXfm.Scale( XMirrorFactor * ScaleFactor, -1! * ScaleFactor, _
MatrixOrder.Append)
' also store inverse matrix (device coord -> world coord) for
' convenience when hit-testing
If mWorld2DeviceXfm.IsInvertible() Then
mDevice2WorldXfm = mWorld2DeviceXfm.Clone()
mDevice2WorldXfm.Invert()
End If
End Sub
Please note that this method is taken from a custom control that derives
from System.Windows.Forms.ScrollableControl. That's why Me.Size
is used to determine the physical size of the canvas. Your situation may be
different so take care. So now you can use this method in OnPaint like
this:
Protected Overrides Sub OnPaint( ByVal _
e As System.Windows.Forms.PaintEventArgs)
Dim xupleft As Integer, yupleft As Integer
Dim xloright As Integer, yloright As Integer
MyBase.OnPaint(e)
' calculate your xupleft, yupleft, xloright, yloright
.............................
Me.SetScale( xupleft, yupleft, xloright, yloright)
e.Graphics.Transform = mWorld2DeviceXfm
' draw your stuff on the e.Graphics here
..............................
End Sub
HTH
eltwo