PictureBox and Scroll bars with Visual Basic .NET

Posted on 2004-11-11
Last Modified: 2010-04-23
Alright, I am working on a program in my free time. I ask a similiar question like this only applying to Visual Basic 6, but I figured out .NET is well different. Here is my question:

In Visual Basic .NET, I want to use a PictureBox, lets say its 640 by 480 pixels big, for an example, but the size should be anything. Now I want to load a picture into this this box that is bigger than it, lets say 800 by 600. Here is what I am wondering, is there a way to display an image so I can scroll or displace it so that I can see the parts that are getting cut off? Like how Internet Explorer does it when you load an image to big for your screen resolution?

Code would help so much and would be awesome!

Thank you for any help or insight.

- Linky
Question by:Linky
    LVL 85

    Accepted Solution

    One approach is to set the SizeMode of the PictureBox to AutoSize and then add the PictureBox to a Panel that has its AutoScroll property set to True.  ScrollBars will automatically appear if necessary.  Here is an example:

    Public Class Form1
        Inherits System.Windows.Forms.Form

    #Region " Windows Form Designer generated code "

        Public Sub New()

            'This call is required by the Windows Form Designer.

            '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
                End If
            End If
        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 Panel1 As System.Windows.Forms.Panel
        Friend WithEvents Button1 As System.Windows.Forms.Button
        Friend WithEvents OpenFileDialog1 As System.Windows.Forms.OpenFileDialog
        <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
            Me.Panel1 = New System.Windows.Forms.Panel
            Me.Button1 = New System.Windows.Forms.Button
            Me.OpenFileDialog1 = New System.Windows.Forms.OpenFileDialog
            Me.Panel1.Anchor = CType((((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Bottom) _
                        Or System.Windows.Forms.AnchorStyles.Left) _
                        Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
            Me.Panel1.AutoScroll = True
            Me.Panel1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle
            Me.Panel1.Location = New System.Drawing.Point(8, 8)
            Me.Panel1.Name = "Panel1"
            Me.Panel1.Size = New System.Drawing.Size(280, 216)
            Me.Panel1.TabIndex = 0
            Me.Button1.Anchor = CType((System.Windows.Forms.AnchorStyles.Bottom Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
            Me.Button1.Location = New System.Drawing.Point(208, 232)
            Me.Button1.Name = "Button1"
            Me.Button1.Size = New System.Drawing.Size(80, 24)
            Me.Button1.TabIndex = 1
            Me.Button1.Text = "Select Image"
            Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
            Me.ClientSize = New System.Drawing.Size(296, 266)
            Me.Name = "Form1"
            Me.Text = "Scrollable PictureBox Example"

        End Sub

    #End Region

        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            OpenFileDialog1.Filter = "Image Files(*.BMP;*.JPG;*.GIF)|*.BMP;*.JPG;*.GIF|All files (*.*)|*.*"
            If OpenFileDialog1.ShowDialog = DialogResult.OK Then
                    Dim pb As New PictureBox
                    pb.Location = New Point(0, 0)
                    pb.SizeMode = PictureBoxSizeMode.AutoSize
                    pb.Image = Image.FromFile(OpenFileDialog1.FileName)
                Catch ex As Exception
                    MessageBox.Show(ex.Message, "Error Loading Image", MessageBoxButtons.OK, MessageBoxIcon.Error)
                End Try
            End If
        End Sub
    End Class
    LVL 2

    Author Comment

    It work like a charm. Thanks a billion.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Join & Write a Comment

    Suggested Solutions

    This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
    Article by: jpaulino
    XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String ( Literal, only instead of starting and ending with w…
    It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
    In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor ( If you're interested in additional methods for monitoring bandwidt…

    745 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now