Hi guys sory for being ooff grade 9 was a little ruf and yet gr 10 its still the same anyway>For my question
I ve got a picture box and in some size modes we all know you cant see everything so aamm then i fought about scrolling and i saw the crtl didnt have tht setting build in so amm ya if yuu guys could give me a example it would be gladly appreciated;)

Recommended Answers

All 14 Replies

What do you mean? what are you trying to say? Do u want the picture box to have a scroll? please clarify your complaint... thanks

picture boxes don't have scrolls if that's what you want

I think he is wanting the picture to adjust to the size of the PictureBox. To do this, your code would look something like this:

Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'Sets the Title of the OpenFileDialog Box
        OpenFileDialog1.Title = "Choose Image to Open"
        'Sets the FileName(the Text in the TextBox of the OpenFileDialog Box)
        OpenFileDialog1.FileName = "Please Open an Image..."
        'Sets the Filter to 3 Types of images
        OpenFileDialog1.Filter = ".JPEG (.jpg)|*.jpg|.GIF (.gif)|*.gif|.PNG (.png)|*.png"
        'Shows the Dialog
        OpenFileDialog1.ShowDialog()
    End Sub

    Private Sub OpenFileDialog1_FileOk(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles OpenFileDialog1.FileOk
        'Loads the Image into the PictureBox
        PictureBox1.Load(OpenFileDialog1.FileName)
        'Centers the Image
        Me.PictureBox1.SizeMode = PictureBoxSizeMode.CenterImage
        'Stretches it to fit the PictureBox
        PictureBox1.SizeMode = PictureBoxSizeMode.StretchImage
    End Sub

    Private Sub PictureBox1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox1.Click
        'Opend the image in your default viewer
        Process.Start(OpenFileDialog1.FileName)
    End Sub
End Class

In this code, I made it where the OpenFileDialog opens when you start the application and then when you open the image, it will stretch and fit the PictureBox component. Also, I added it where when you click the image it opens up to the original size. You can view my notes for guidance if you would like.

If this is not what you are wanting to accomplish, then please continue to provide us with more information to assist you on you issue.

Member Avatar for Unhnd_Exception

If you want a scrollable picturebox then do the following:

1. Add a panel to the form
2. Set the panel to the size you want the picturebox to be.
3. Set the panel's autoscroll = true
4. Set the panel's borderstyle = none
5. Add a picturebox to the panel.
6. Set the picturebox's location to 0,0
7. Set the picturebox's anchor to left or top, if not already
8. Set the picturebox's sizemode = autosize.
9. Set the panels borderstyle to the desired style.

When the picture is to big the picturebox will increase in size and the scrollbars will appear on the panel. You should handle the picturebox's MouseEnter event to set focus to the panel. That way the mouse wheel will scroll the picture up and down.

aamm cool guys but i found a picbox control

IM gona show code soon becuase there is a problem im currently Converting it from C# to vb2010 code

Imports System
Imports System.Collections
Imports System.ComponentModel
Imports System.Drawing
Imports System.Data
Imports System.Windows.Forms

Namespace PictureViewer
    Public Enum SizeMode
        Scrollable
        RatioStretch
    End Enum

    Public Class Viewer
        Inherits System.Windows.Forms.UserControl
        Private pictureBox1 As System.Windows.Forms.PictureBox
        Private components As System.ComponentModel.IContainer
        Private sizeMode As SizeMode

        Public Sub New()
            ' This call is required by the Windows.Forms Form Designer.
            InitializeComponent()
            Me.ImageSizeMode = SizeMode.RatioStretch
        End Sub

        Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
            If disposing Then
                If components IsNot Nothing Then
                    components.Dispose()
                End If
            End If
            MyBase.Dispose(disposing)
        End Sub

        Public Property Image() As Image
            Get
                Return Me.pictureBox1.Image
            End Get
            Set(ByVal value As Image)
                Me.pictureBox1.Image = value
                Me.SetLayout()
            End Set
        End Property

        Public Property ImageSizeMode() As SizeMode
            Get
                Return Me.sizeMode
            End Get
            Set(ByVal value As SizeMode)
                Me.sizeMode = value
                Me.AutoScroll = (Me.sizeMode = SizeMode.Scrollable)
                Me.SetLayout()
            End Set
        End Property

        Private Sub RatioStretch()
            Dim pRatio As Single = CSng(Me.Width) / Me.Height
            Dim imRatio As Single = CSng(Me.pictureBox1.Image.Width) / Me.pictureBox1.Image.Height
            If Me.Width >= Me.pictureBox1.Image.Width AndAlso Me.Height >= Me.pictureBox1.Image.Height Then
                Me.pictureBox1.Width = Me.pictureBox1.Image.Width
                Me.pictureBox1.Height = Me.pictureBox1.Image.Height
            ElseIf Me.Width > Me.pictureBox1.Image.Width AndAlso Me.Height < Me.pictureBox1.Image.Height Then
                Me.pictureBox1.Height = Me.Height
                Me.pictureBox1.Width = CInt(Fix(Me.Height * imRatio))
            ElseIf Me.Width < Me.pictureBox1.Image.Width AndAlso Me.Height > Me.pictureBox1.Image.Height Then
                Me.pictureBox1.Width = Me.Width
                Me.pictureBox1.Height = CInt(Fix(Me.Width / imRatio))
            ElseIf Me.Width < Me.pictureBox1.Image.Width AndAlso Me.Height < Me.pictureBox1.Image.Height Then
                If Me.Width >= Me.Height Then
                    If Me.pictureBox1.Image.Width >= Me.pictureBox1.Image.Height AndAlso imRatio >= pRatio Then
                        Me.pictureBox1.Width = Me.Width
                        Me.pictureBox1.Height = CInt(Fix(Me.Width / imRatio))
                    Else
                        Me.pictureBox1.Height = Me.Height
                        Me.pictureBox1.Width = CInt(Fix(Me.Height * imRatio))
                    End If
                Else
                    If Me.pictureBox1.Image.Width < Me.pictureBox1.Image.Height AndAlso imRatio < pRatio Then
                        Me.pictureBox1.Height = Me.Height
                        Me.pictureBox1.Width = CInt(Fix(Me.Height * imRatio))
                    Else
                        Me.pictureBox1.Width = Me.Width
                        Me.pictureBox1.Height = CInt(Fix(Me.Width / imRatio))
                    End If
                End If
            End If
            Me.CenterImage()
        End Sub

        Private Sub Scrollable()
            Me.pictureBox1.Width = Me.pictureBox1.Image.Width
            Me.pictureBox1.Height = Me.pictureBox1.Image.Height
            Me.CenterImage()
        End Sub

        Private Sub SetLayout()
            If Me.pictureBox1.Image Is Nothing Then
                Return
            End If
            If Me.sizeMode = sizeMode.RatioStretch Then
                Me.RatioStretch()
            Else
                Me.AutoScroll = False
                Me.Scrollable()
                Me.AutoScroll = True

            End If
        End Sub

        Private Sub CenterImage()
            Dim top As Integer = CInt(Fix((Me.Height - Me.pictureBox1.Height) / 2.0))
            Dim left As Integer = CInt(Fix((Me.Width - Me.pictureBox1.Width) / 2.0))
            If top < 0 Then
                top = 0
            End If
            If left < 0 Then
                left = 0
            End If
            Me.pictureBox1.Top = top
            Me.pictureBox1.Left = left
        End Sub

#Region "Component Designer generated code"
        ''' <summary> 
        ''' Required method for Designer support - do not modify 
        ''' the contents of this method with the code editor.
        ''' </summary>
        Private Sub InitializeComponent()
            Me.pictureBox1 = New System.Windows.Forms.PictureBox()
            Me.SuspendLayout()
            ' 
            ' pictureBox1
            '
            Me.pictureBox1.Cursor = System.Windows.Forms.Cursors.Default
            Me.pictureBox1.Location = New System.Drawing.Point(24, 32)
            Me.pictureBox1.Name = "pictureBox1"
            Me.pictureBox1.Size = New System.Drawing.Size(296, 208)
            Me.pictureBox1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage
            Me.pictureBox1.TabIndex = 0
            Me.pictureBox1.TabStop = False
            Me.pictureBox1.ImageLocation = ""
            ' 
            ' Viewer
            ' 
            Me.AutoScroll = True
            Me.BackColor = System.Drawing.Color.Black
            Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.pictureBox1})
            Me.Name = "Viewer"
            Me.Size = New System.Drawing.Size(352, 272)
            Me.ResumeLayout(False)
        End Sub
#End Region

        Private Sub Viewer_Load(ByVal sender As Object, ByVal e As System.EventArgs)
            Me.pictureBox1.Width = 0
            Me.pictureBox1.Height = 0
            Me.SetLayout()
        End Sub

        Private Sub Viewer_Resize(ByVal sender As Object, ByVal e As System.EventArgs)
            Me.SetLayout()
        End Sub
    End Class
End Namespace

The problem here is tht he didnt include Image Location i would really like too know why
it doesnt have it in:?: and maybe if i can put this property back:idea:

Member Avatar for Unhnd_Exception

You can add the property back by simply adding another property named ImageLocation. Look at the other properties.


You can also do the steps I provided and add this to your form. It should do the same thing as that.

Private Sub PictureBox1_MouseEnter(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox1.MouseEnter
        Panel1.Focus()
    End Sub

    Private Sub PictureBox1_SizeModeChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles PictureBox1.SizeModeChanged

        Select Case PictureBox1.SizeMode

            Case PictureBoxSizeMode.AutoSize, PictureBoxSizeMode.Normal
                'Make scrollable
                PictureBox1.SizeMode = PictureBoxSizeMode.AutoSize
                Panel1.AutoScroll = True
                PictureBox1.Dock = DockStyle.None

            Case Else
                'if strecth then strech it. otherwise center it with the
                'same aspect ratio
                If PictureBox1.SizeMode <> PictureBoxSizeMode.StretchImage Then
                    PictureBox1.SizeMode = PictureBoxSizeMode.Zoom
                End If

                Panel1.AutoScroll = False
                PictureBox1.Dock = DockStyle.Fill

        End Select

    End Sub
commented: +=1 for .beyond the wit of a "With". :) +12

Google on how to change the location. It is not difficult and can easily be added.

Before i Make this thread solved there's just one more thing
How can put thee ohter orignal propertys of size mode back and make it sought of a Picture box control Extended becuase his version disincludes it:?:
:-/ I wonder is it possible

Oh and by the way thanks for the Tip .net junkie i didnt think of the propertys and now image location works like a charm :icon_wink:

Oh and by the way thanks for the Tip .net junkie i didnt think of the propertys and now image location works like a charm :icon_wink:

Always here to help ;)

Before i Make this thread solved there's just one more thing
How can put thee ohter orignal propertys of size mode back and make it sought of a Picture box control Extended becuase his version disincludes it:?:
:-/ I wonder is it possible

Do you mean how to make it where it opens in a larger window when you click it and you can make it minimize back into its original size?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.