locked
Upload Image RRS feed

  • Question

  • I have a picturebox on a form and I am trying to upload a image to database. 

    My question is how to fit a image into a picturebox even though the size of image is bigger or smaller than that. If It is bigger than the picturebox, then how to get a scrollbar.

    Below is my code.  


        Private Sub Button_Browse_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button_Browse.Click

            With OpenFileDialog
                .Title = "Choose a image"
                .InitialDirectory = My.Computer.FileSystem.SpecialDirectories.MyDocuments
                .FileName = ""
                .Filter = "All Files|*.*|JPEGs|*.jpg|Bitmaps|*.bmp|GIFs|*.gif"
                .FilterIndex = 2

            End With

            If OpenFileDialog.ShowDialog() = DialogResult.OK Then
                With PictureBoxDocument
                    .Image = Image.FromFile(OpenFileDialog.FileName)
                    .SizeMode = PictureBoxSizeMode.CenterImage
                    .BorderStyle = BorderStyle.Fixed3D
                End With
                lblFilePath.Text = OpenFileDialog.FileName
            End If
        End Sub

    • Edited by Hoorayo Thursday, May 28, 2009 1:51 PM
    Thursday, May 28, 2009 1:42 PM

Answers

  • Try this.  Start a new Windows Forms Application and replace the Form1 code with this:

    Public Class Form1
      Dim PB As New PictureBox
      Dim Pnl As New Panel
      Public Sub New()
        InitializeComponent()
        Pnl.Parent = Me
        Pnl.Dock = DockStyle.Fill
        Pnl.AutoScroll = True
        PB.Parent = Pnl
        PB.SizeMode = PictureBoxSizeMode.AutoSize
        PB.Image = Image.FromFile("C:\Users\Public\Pictures\Sample Pictures\Forest.jpg")
      End Sub
    End Class

    Change the file location to the location of "Forest.jpg" on your system.  When you press F5, you should have a picture of a forest with scrollbars.

    • Proposed as answer by Cor Ligthert Saturday, May 30, 2009 7:18 PM
    • Marked as answer by Xingwei Hu Tuesday, June 2, 2009 10:34 AM
    Friday, May 29, 2009 6:11 PM

All replies

  • Thanks for your post, but since you don't have a question, please change the post icon to "Comment".
    Thursday, May 28, 2009 1:48 PM
  • I modified it and there is my question.

    If I repeat here..

    1. How to fit a image into a picturebox even though the size of image is bigger or smaller than that

    2.  If the image is bigger than the picturebox, then how to get a scrollbar.

    Thursday, May 28, 2009 1:52 PM
  • 1.  SizeMode = Zoom
    2.  Put the PB in a Panel with AutoScroll = True.

    Thursday, May 28, 2009 1:56 PM
  • Hi ,

    Instead Of Using Image of Picture Box Use BackGroundImage Property And BackGroundImageLayout As Stretch. Then How Much Bigger image It May Be  , It Will Fit In Oicture Box .. Try It
    Thanks And Regards













    Satya Ranjan Biswal
    Thursday, May 28, 2009 2:00 PM
  • Sorry! They are not working.
    Thursday, May 28, 2009 2:29 PM
  • Did you try the answer from John Wein?
    Thursday, May 28, 2009 2:31 PM
  • Yes, I tried both.

    I put the PictureBox on a panel and set Autoscroll = True. The bigger picture did not fit in the smaller picturebox.
    Thursday, May 28, 2009 2:34 PM
  • 2.  Put the PB in a Panel with AutoScroll = True.  Set the PB's SizeMode = AutoSize.
    Thursday, May 28, 2009 3:04 PM
  • Does those options work for your test?

    I tried all those options, but it didn't work.
    Thursday, May 28, 2009 8:32 PM
  • What does "didn't work" mean?
    Thursday, May 28, 2009 8:34 PM
  • I have a bigger image than Picturebox and I try to have scroll bar if that's the case.
    I tired all those options that you suggested, but they did not work. I don't get a scroll bar
    Thursday, May 28, 2009 8:36 PM
  • If you place an image that is larger than the panel into a picturebox contained within the panel, the panel's AutoScroll property is set to true and the picturebox's SizeMode is set to Autosize, a standard WindowsFormsApplication will show scrollbars on the panel..
    Thursday, May 28, 2009 8:46 PM
  • I can see the scrollbar if the PictureBox is not set for "Dock = Fill".

    And I also tried "Put the PB in a Panel with AutoScroll = True.  Set the PB's SizeMode = AutoSize", but the picture box does not get bigger even though the image is bigger than the picturebox. Dock is not Fill.

    Friday, May 29, 2009 4:28 PM
  • I  have no idea what you are doing.

    Friday, May 29, 2009 4:37 PM
  • Try this.  Start a new Windows Forms Application and replace the Form1 code with this:

    Public Class Form1
      Dim PB As New PictureBox
      Dim Pnl As New Panel
      Public Sub New()
        InitializeComponent()
        Pnl.Parent = Me
        Pnl.Dock = DockStyle.Fill
        Pnl.AutoScroll = True
        PB.Parent = Pnl
        PB.SizeMode = PictureBoxSizeMode.AutoSize
        PB.Image = Image.FromFile("C:\Users\Public\Pictures\Sample Pictures\Forest.jpg")
      End Sub
    End Class

    Change the file location to the location of "Forest.jpg" on your system.  When you press F5, you should have a picture of a forest with scrollbars.

    • Proposed as answer by Cor Ligthert Saturday, May 30, 2009 7:18 PM
    • Marked as answer by Xingwei Hu Tuesday, June 2, 2009 10:34 AM
    Friday, May 29, 2009 6:11 PM
  • You are right! That works.

    I guess I have to find out what I have to change.
    Thanks.
    Saturday, May 30, 2009 3:56 PM