locked
Changing the Bitmap Size RRS feed

  • Question

  • Hi, here is what i want to do. I want to allow the user to be able select the Image path by clicking on the textbox. Hence the selected image path will be shown on the picturebox. But the selected image size is not the same with the PictureBox. So my question is, how can i change the size of the selected Image to fit in nice into picturebox? Here is my code. I'm not sure what im doing is correct or not :

     

     Private Sub TextBox4_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox4.Click
            Dim ImageURL As String
            Dim ImagePath As OpenFileDialog = New OpenFileDialog()
            ImagePath.Title = "Browse Your Pictures"
            ImagePath.InitialDirectory = "C:\Documents and Settings\All Users\Documents\My Pictures\Sample Pictures"
            ImagePath.Filter = "All files (*.*)|*.*|All files (*.*)|*.*"
            ImagePath.FilterIndex = 2
            ImagePath.RestoreDirectory = True
            If ImagePath.ShowDialog() = DialogResult.OK Then
                TextBox4.Text = ImagePath.FileName
                ImageURL = ImagePath.FileName
            End If
    
            Try
                Dim Picture As Bitmap = New Bitmap(PictureBox1.Width, PictureBox1.Height)
                Picture = Image.FromFile(ImageURL)
                Dim PictureSize As Graphics = Graphics.FromImage(Picture)
               
                PictureSize.DrawImage(PictureBox1.Image, 0, 0, PictureBox1.Width, PictureBox1.Height)
    
                PictureBox1.Image = Picture
    
    
            Catch ex As Exception
                MsgBox(ex.ToString)
            End Try
    
        End Sub

    Monday, March 22, 2010 10:35 AM

Answers

All replies

  • You even can do that in the designer properties

    Sizemode StretchImage

    http://msdn.microsoft.com/en-us/library/system.windows.forms.pictureboxsizemode.aspx


    Success
    Cor
    • Proposed as answer by jtorrecilla Monday, March 22, 2010 10:46 AM
    • Marked as answer by Yeong Ren Tuesday, March 23, 2010 12:55 AM
    Monday, March 22, 2010 10:43 AM
  • Use the PictureBox.SizeMode property.

    PictureBox1.SizeMode = PictureBoxSizeMode.Zoom

    This will resize the image automatically to the size of your PictureBox. If you want to resize the PictureBox to the size of image then use PictureBoxSizeMode.AutoSize

    Regards


    Life would have been much easier if I had the source-code !!
    • Marked as answer by Yeong Ren Tuesday, March 23, 2010 12:55 AM
    Monday, March 22, 2010 10:47 AM
  • Thanks alot to both of you. Got it done. Now i can move on. : )
    Tuesday, March 23, 2010 12:55 AM
  • Use the PictureBox.SizeMode property.

    PictureBox1.SizeMode = PictureBoxSizeMode.Zoom

    This will resize the image automatically to the size of your PictureBox. If you want to resize the PictureBox to the size of image then use PictureBoxSizeMode.AutoSize

    Regards


    Life would have been much easier if I had the source-code !!

    I'd prefer this reply as I am not very familiar with designer properties. And I should say thank you to Cor too. Thank your for your information. I can just learn about it.
    Friday, June 25, 2010 6:47 AM