Hi, I am developping an image viewer and everithing is working fine.
Now, i associated this application with all jpg files. It's fine too. But when the application is launched, the image is not being opened.
I supose that i have to do something like this:

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs)
dim filepath as string= (That is what i need. How to get the file from where the application has been launched)
picturebox1.image=system.drawing.image.fromfile(filepath)
End Sub

Anyone can help me?
Thanks,

Dani AI

Generated

Short answer: Windows hands the file you double-click as a command-line argument, so read the parsed arguments (not StartupPath, and don't split the raw command line by spaces). 's StartupPath points at the EXE; was on the right track with command-line use but splitting Environment.CommandLine is fragile for paths with spaces or quotes.

The simplest, robust pattern in VB is to read the parsed arguments and safely load the image into a PictureBox without locking the file:

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
  If My.Application.CommandLineArgs.Count = 0 Then Exit Sub
  Dim imgPath As String = My.Application.CommandLineArgs(0)

  Try
    Using fs As New IO.FileStream(imgPath, IO.FileMode.Open, IO.FileAccess.Read, IO.FileShare.Read)
      Using src As Image = Image.FromStream(fs)
        If PictureBox1.Image IsNot Nothing Then PictureBox1.Image.Dispose()
        PictureBox1.Image = New Bitmap(src) 'decouple from stream to avoid locks
      End Using
    End Using
  Catch ex As Exception
    ' handle missing file / invalid image / permissions
  End Try
End Sub

If you want a single-instance app (so double-clicking a file while the viewer is already running hands the path to the running instance), enable the VB application framework single-instance option and handle StartupNextInstance in ApplicationEvents.vb; the new file path appears in e.CommandLine and you can forward it to the main form.

Troubleshooting tips: verify the file association actually passes the filename (the open command must include "%1" and be quoted); check multiple args if users drag-select several files; always dispose previous images to avoid memory leaks; and catch exceptions around file access and image decoding. This approach resolves the common issues raised by , ties back to 's folder remarks (use only if you ship default images), and replaces fragile raw-splitting with a reliable argument API.

Recommended Answers

All 4 Replies

try using this

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
dim filepath as string= Application.StartupPath & "filename"
picturebox1.image=system.drawing.image.fromfile(filepath)
    End Sub

That's geting the application exe file path and not the image file i opened the aplication from.
Thanks however.
Any new suggestions?

Hi,

When you have created a new image folder in the Bin folder of your application, then you need to use this part of code. foldername is your image folder and imagename is the image you want to load at first.

PictureBox1.Image = System.Drawing.Image.FromFile(Application.StartupPath & "\foldername\imagename.jpg")

However, when you added directly your image in the bin folder then you need to use the code like this:

PictureBox1.Image = System.Drawing.Image.FromFile(Application.StartupPath & "\imagename.jpg")

You can pass commandline arguments to WinForms apps:

Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    ' 
    Dim Args() As String

    Args = Environment.CommandLine.Split(CChar(" ")) ' Split arguments (space separated)

    ' Args(0) is application's name
    If Args.Length > 1 Then
      ' Pass the image file's name as an argument
      PictureBox1.Image = Image.FromFile(Args(1))
    End If

  End Sub

The whole commandline is in the Environment.CommandLine string, which you'll have to split in order to get each separate argument. The first "argument" is always the executable's name.

HTH

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.