Hello, I am new to this community as well as Visual Basic. My class was assigned a project that is to use Panning and Zooming of a Image. However the code the teacher supplied us zooms to the Upper Left corner of the image instead of the Center of the Current Viewable area. I have tried to using AutoScrollPosition to adjust for zooming to the center however, I was unsuccesfull. Any help would be greatly apprecitated. I am using Visual Basic (From Visual Studio 2008)
Here is the code we were supplied with :
Imports System
Imports System.Collections
Imports System.ComponentModel
Imports System.Drawing
Imports System.Drawing.Drawing2D
Imports System.Windows.Forms
Public Class ZoomPicBox
Inherits ScrollableControl
Private _image As Image
Public Property Image() As Image
Get
Return _image
End Get
Set(ByVal value As Image)
_image = value
UpdateScaleFactor()
Invalidate()
End Set
End Property
Private _zoom As Single = 1.0F
Public Property Zoom() As Single
Get
Return _zoom
End Get
Set(ByVal value As Single)
If value < 0 OrElse value < 0.00001 Then
value = 0.00001F
End If
_zoom = value
UpdateScaleFactor()
Invalidate()
End Set
End Property
Private Sub UpdateScaleFactor()
If _image Is Nothing Then
Me.AutoScrollMargin = Me.Size
Else
Me.AutoScrollMinSize = New Size(CInt(Me._image.Width * _zoom + 0.5F), CInt(Me._image.Height * _zoom + 0.5F))
End If
End Sub 'UpdateScaleFactor
Private _interpolationMode As InterpolationMode = InterpolationMode.High
Public Property InterpolationMode() As InterpolationMode
Get
Return _interpolationMode
End Get
Set(ByVal value As InterpolationMode)
_interpolationMode = value
End Set
End Property
Protected Overrides Sub OnPaintBackground(ByVal pevent As PaintEventArgs)
End Sub 'OnPaintBackground
Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs)
'if no image, don't bother
If _image Is Nothing Then
MyBase.OnPaintBackground(e)
Return
End If
'Set up a zoom matrix
Dim mx As New Matrix(_zoom, 0, 0, _zoom, 0, 0)
mx.Translate(Me.AutoScrollPosition.X / _zoom, Me.AutoScrollPosition.Y / _zoom)
e.Graphics.Transform = mx
e.Graphics.InterpolationMode = _interpolationMode
e.Graphics.DrawImage(_image, New Rectangle(0, 0, Me._image.Width, Me._image.Height), _
0, 0, _image.Width, _image.Height, GraphicsUnit.Pixel)
MyBase.OnPaint(e)
End Sub 'OnPaint
Public Sub New()
'Double buffer the control
Me.SetStyle(ControlStyles.AllPaintingInWmPaint Or ControlStyles.UserPaint Or _
ControlStyles.ResizeRedraw Or ControlStyles.UserPaint Or ControlStyles.DoubleBuffer, True)
Me.AutoScroll = True
End Sub 'New
End Class
Edit: I Tried adjusting the AutoScrollPosition in the UpdateScaleFactor() Subroutine. I cannot figure out the actual arithmetic though.