code for opening and attaching an image file

im only new in VB. I need a code how to open an image such as .jpg, .gif, .bmp. etc. ,and attach or diplay the selected image to the PictureBox.

thanks!




Answer this question

code for opening and attaching an image file

  • SK1000

    Very simple - use the load method on the picturebox and supply it with the filename you wish to display.

    PictureBox1.Load("d:\blue.bmp")


  • Remus Radu

    thnx 4 the help guys! fortunately i figured out the simplest code:

    Private Sub buttton1_Click()
    dialog1.ShowOpen
    If dialogresult = ok Then
    Image1.Picture = LoadPicture(dialog1.FileName)
    End If
    End Sub



  • Joakim Gyllstedt

    OK the complete code for a dialog with filters and title which will display the graphic image in a picture box if the OK button is pressed is

    Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

    Dim x As New OpenFileDialog
    x.Title = "Select a Graphic File"
    x.Filter = "Graphic Files(*.bmp;*.jpg;*.bmp;*.gif)|*.bmp;*.jpg;*.bmp;*.gif|All Files|*.*"
    x.Multiselect = False

    If x.ShowDialog = Windows.Forms.DialogResult.OK Then
    Me.PictureBox1.Load(x.FileName)
    End If

    End Sub
    End Class


  • Gawaine79

    The only thing I'd add to the Squires code would be apropriate filter strings:

    For Executable files a Filter string would look like this:

    OPNFD.Filter = "Executable files (*.exe;*.cpl;*.bat)|*.exe;*.cpl;*.bat"

    Without a filter string, exceptions are quite possible by selecting inappropriate file types.



  • Robert3234

    There is a built-in dialog called OpenFileDialog that will probably do what you want. Put something like this in your button handler:

    Dim dlg as new OpenFileDialog

    If dlg.ShowDialog() = DialogResult.OK Then

    PictureBox.Load(dlg.Filename)

    End If


  • Shreyas Ranade (MSFT)

    thanks for the help spotty but i need a code how to open image from my directory. just like, for example, I can open an image in Microsoft Paint by clicking the "open" button then a dialogue box appears and I can choose which image I wanted to open.

    *for more details:

    On my form, I added one "command button" that i named "addpicture", and I also added one "PictureBox".

    If I click the command button, it should open a dialogue box that shows images on a particular path then if i selected an image it should be shown on the "PictureBox."



  • Li Wang

    the code still didnt work.

  • code for opening and attaching an image file