Imports System.Drawing
Imports System.Drawing.Drawing2D
Imports System.Windows.Forms
public class UseScrollBarToControlTheScrollingOfAnImage
public Shared Sub Main
Application.Run(New Form1)
End Sub
End class
Public Class Form1
Inherits System.Windows.Forms.Form
#Region " Windows Form Designer generated code "
Public Sub New()
MyBase.New()
'This call is required by the Windows Form Designer.
InitializeComponent()
'Add any initialization after the InitializeComponent() call
End Sub
'Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer
'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
Friend WithEvents PictureBox1 As System.Windows.Forms.PictureBox
Friend WithEvents Label1 As System.Windows.Forms.Label
Friend WithEvents HScrollBar1 As System.Windows.Forms.HScrollBar
Friend WithEvents VScrollBar1 As System.Windows.Forms.VScrollBar
Friend WithEvents Button1 As System.Windows.Forms.Button
Friend WithEvents OpenFileDialog1 As System.Windows.Forms.OpenFileDialog
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.PictureBox1 = New System.Windows.Forms.PictureBox
Me.Label1 = New System.Windows.Forms.Label
Me.HScrollBar1 = New System.Windows.Forms.HScrollBar
Me.VScrollBar1 = New System.Windows.Forms.VScrollBar
Me.Button1 = New System.Windows.Forms.Button
Me.OpenFileDialog1 = New System.Windows.Forms.OpenFileDialog
Me.SuspendLayout()
'
'PictureBox1
'
Me.PictureBox1.Location = New System.Drawing.Point(64, 72)
Me.PictureBox1.Name = "PictureBox1"
Me.PictureBox1.Size = New System.Drawing.Size(168, 144)
Me.PictureBox1.TabIndex = 0
Me.PictureBox1.TabStop = False
'
'Label1
'
Me.Label1.Font = New System.Drawing.Font("Microsoft Sans Serif", 24.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
Me.Label1.Location = New System.Drawing.Point(0, 0)
Me.Label1.Name = "Label1"
Me.Label1.Size = New System.Drawing.Size(264, 48)
Me.Label1.TabIndex = 1
Me.Label1.Text = "Handling Images"
'
'HScrollBar1
'
Me.HScrollBar1.Location = New System.Drawing.Point(64, 200)
Me.HScrollBar1.Name = "HScrollBar1"
Me.HScrollBar1.Size = New System.Drawing.Size(168, 16)
Me.HScrollBar1.TabIndex = 2
Me.HScrollBar1.Visible = False
'
'VScrollBar1
'
Me.VScrollBar1.Location = New System.Drawing.Point(216, 72)
Me.VScrollBar1.Name = "VScrollBar1"
Me.VScrollBar1.Size = New System.Drawing.Size(16, 128)
Me.VScrollBar1.TabIndex = 3
Me.VScrollBar1.Visible = False
'
'Button1
'
Me.Button1.Location = New System.Drawing.Point(112, 248)
Me.Button1.Name = "Button1"
Me.Button1.TabIndex = 4
Me.Button1.Text = "Load Image"
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(292, 273)
Me.Controls.Add(Me.Button1)
Me.Controls.Add(Me.VScrollBar1)
Me.Controls.Add(Me.HScrollBar1)
Me.Controls.Add(Me.Label1)
Me.Controls.Add(Me.PictureBox1)
Me.Name = "Form1"
Me.Text = "Form1"
Me.ResumeLayout(False)
End Sub
#End Region
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
PictureBox1.Image = Image.FromFile("yourfile.jpg")
HScrollBar1.Maximum = PictureBox1.Image.Width - PictureBox1.Width + HScrollBar1.Height
VScrollBar1.Maximum = PictureBox1.Image.Height - PictureBox1.Height + VScrollBar1.Width
VScrollBar1.Visible = True
HScrollBar1.Visible = True
If PictureBox1.Height > PictureBox1.Image.Height Then
VScrollBar1.Visible = False
End If
If PictureBox1.Width > PictureBox1.Image.Width Then
HScrollBar1.Visible = False
End If
End Sub
Private Sub HScrollBar1_Scroll(ByVal sender As System.Object, ByVal e As System.Windows.Forms.ScrollEventArgs) Handles HScrollBar1.Scroll
Dim gphPictureBox As Graphics = PictureBox1.CreateGraphics()
gphPictureBox.DrawImage(PictureBox1.Image, New Rectangle(0, 0, _
PictureBox1.Width - HScrollBar1.Height, _
PictureBox1.Height - VScrollBar1.Width), _
New Rectangle(HScrollBar1.Value, VScrollBar1.Value, _
PictureBox1.Width - HScrollBar1.Height, _
PictureBox1.Height - VScrollBar1.Width), GraphicsUnit.Pixel)
End Sub
Private Sub VScrollBar1_Scroll(ByVal sender As System.Object, ByVal e As System.Windows.Forms.ScrollEventArgs) Handles VScrollBar1.Scroll
Dim gphPictureBox As Graphics = PictureBox1.CreateGraphics()
gphPictureBox.DrawImage(PictureBox1.Image, New Rectangle(0, 0, _
PictureBox1.Width - HScrollBar1.Height, _
PictureBox1.Height - VScrollBar1.Width), _
New Rectangle(HScrollBar1.Value, VScrollBar1.Value, _
PictureBox1.Width - HScrollBar1.Height, _
PictureBox1.Height - VScrollBar1.Width), GraphicsUnit.Pixel)
End Sub
End Class