Hello all mate from daniweb, well today i got some trouble may be someone kind can help me i wanna know how to add scroll bars to the picture box so the we can scroll to view the whole image.

Dani AI

Generated

The HTML/CSS solution shown earlier (an overflowed div) is for web pages only and cannot be dropped straight into a WinForms app. As pointed out, the common WinForms pattern is to put the PictureBox inside a scrollable container (Panel or FlowLayoutPanel) and enable scrolling on that container. The key idea is that the picture control must be larger than the container for scrollbars to appear, or the container must be told the logical scroll size.

A compact VB.NET pattern that avoids locking the image file and lets the Panel show scrollbars looks like this:

' Form_Load
Dim img As Image
Using fs As New IO.FileStream("C:\images\big.jpg", IO.FileMode.Open, IO.FileAccess.Read)
    img = Image.FromStream(fs)
End Using

pictureBox1.Image = img
pictureBox1.Size = img.Size
pictureBox1.Location = New Point(0, 0)

panel1.AutoScroll = True
panel1.AutoScrollMinSize = img.Size

Troubleshooting notes and practical tips:

  • Scrollbars won't appear if the container is large enough to fit the image or if the PictureBox is docked to Fill. Keep the Panel smaller than the image, or set the PictureBox size to the image size (or use AutoSize on the PictureBox).
  • Using SizeMode = Zoom or StretchImage scales the image and usually prevents scrolling; use Normal/AutoSize when scrolling is needed.
  • Image.FromFile keeps the file locked; Image.FromStream (as above) or copying the stream avoids that lock. See the docs for details on file-locking behavior.
  • For very large images, consider loading a scaled copy or using tiling/virtualized rendering to reduce memory use.
  • Dispose images when no longer needed (for example on form closing) to free resources.

Relevant references: ScrollableControl.AutoScroll and Image.FromFile (file-lock note).

Recommended Answers

All 3 Replies

<div style="overflow:scroll;width:100px; height:100px">
  <img src="images/a1.jpg"/>
</div>
<div style="overflow:scroll;width:100px; height:100px">
  <img src="images/a1.jpg"/>
</div>

can i use this in vb.net win forms

Paste following code in form_load event

Dim pan As New Panel
        pan.Location = New Point(30, 30)
        pan.Size = New Size(200, 200)
        pan.AutoScroll = True

        Dim pic As New PictureBox
        'Change the path/name of image file
        pic.Image = Image.FromFile("c:\windows\web\wallpaper\friend.jpg")
        pic.SizeMode = PictureBoxSizeMode.AutoSize

        pan.Controls.Add(pic)
        Me.Controls.Add(pan)
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.