Why does this error occur? Here is my code:

Imports System.IO
Public Class Form1

    Private Sub Button1_Click_1(sender As Object, e As EventArgs) Handles Button1.Click
        OpenFileDialog1.Title = "Select Image"
        OpenFileDialog1.InitialDirectory = "C:\temp"
        OpenFileDialog1.FileName = "Image File"
        OpenFileDialog1.Filter = "JPEG Images (*.jpg,*.jpeg)|*.jpg;*.jpeg|Gif Images (*.gif)|*.gif|PNG Images|*.png"
        OpenFileDialog1.ShowDialog()

    End Sub

    Private Sub OpenFileDialog1_FileOk(sender As Object, e As System.ComponentModel.CancelEventArgs)
        Dim strm As System.IO.Stream
        strm = OpenFileDialog1.OpenFile()
        TextBox1.Text = OpenFileDialog1.FileName.ToString()
        If Not (strm Is Nothing) Then
            ' Display the image
            PictureBox1.Image = Image.FromFile(OpenFileDialog1.FileName)
            Button2.Show()
            Button3.Show()
        End If
    End Sub

    Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
        OpenFileDialog1.Title = "Select Image"
        OpenFileDialog1.InitialDirectory = "C:\temp"
        OpenFileDialog1.FileName = "Image File"
        OpenFileDialog1.Filter = "JPEG Images (*.jpg,*.jpeg)|*.jpg;*.jpeg|Gif Images (*.gif)|*.gif|PNG Images|*.png"
        OpenFileDialog1.ShowDialog()
    End Sub

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click

        Dim clsRequest As System.Net.FtpWebRequest = _
            DirectCast(System.Net.WebRequest.Create("ftp://ftp.website.com/uploads"), System.Net.FtpWebRequest)
        clsRequest.Credentials = New System.Net.NetworkCredential("ftp_username", "password")
        clsRequest.Method = System.Net.WebRequestMethods.Ftp.UploadFile

        ' read in file...
        Dim bFile() As Byte = System.IO.File.ReadAllBytes(PictureBox1.ImageLocation.ToString())

        ' upload file...
        Dim clsStream As System.IO.Stream = _
        clsRequest.GetRequestStream()
        clsStream.Write(bFile, 0, bFile.Length)
        clsStream.Close()
        clsStream.Dispose()

    End Sub
End Class

Can anyone see what is causing this error? It makes the application useless, when I click the button to open the file I want to view in the PictureBox, the dialog to select the file opens but the image file I select does not open. I don't know why this is happening. Here is the debug window:

'pictureshare.vshost.exe' (Managed (v4.0.30319)): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_32\mscorlib\v4.0_4.0.0.0__b77a5c561934e089\mscorlib.dll'
'pictureshare.vshost.exe' (Managed (v4.0.30319)): Loaded 'C:\Windows\assembly\GAC_MSIL\Microsoft.VisualStudio.HostingProcess.Utilities\11.0.0.0__b03f5f7f11d50a3a\Microsoft.VisualStudio.HostingProcess.Utilities.dll'
'pictureshare.vshost.exe' (Managed (v4.0.30319)): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System.Windows.Forms\v4.0_4.0.0.0__b77a5c561934e089\System.Windows.Forms.dll'
'pictureshare.vshost.exe' (Managed (v4.0.30319)): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System.Drawing\v4.0_4.0.0.0__b03f5f7f11d50a3a\System.Drawing.dll'
'pictureshare.vshost.exe' (Managed (v4.0.30319)): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System\v4.0_4.0.0.0__b77a5c561934e089\System.dll'
'pictureshare.vshost.exe' (Managed (v4.0.30319)): Loaded 'C:\Windows\assembly\GAC_MSIL\Microsoft.VisualStudio.HostingProcess.Utilities.Sync\11.0.0.0__b03f5f7f11d50a3a\Microsoft.VisualStudio.HostingProcess.Utilities.Sync.dll'
'pictureshare.vshost.exe' (Managed (v4.0.30319)): Loaded 'C:\Windows\assembly\GAC_MSIL\Microsoft.VisualStudio.Debugger.Runtime\11.0.0.0__b03f5f7f11d50a3a\Microsoft.VisualStudio.Debugger.Runtime.dll'
'pictureshare.vshost.exe' (Managed (v4.0.30319)): Loaded 'C:\Users\Jacob\documents\visual studio 2012\Projects\pictureshare\pictureshare\bin\Debug\pictureshare.vshost.exe'
'pictureshare.vshost.exe' (Managed (v4.0.30319)): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_32\System.Data\v4.0_4.0.0.0__b77a5c561934e089\System.Data.dll'
'pictureshare.vshost.exe' (Managed (v4.0.30319)): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System.Core\v4.0_4.0.0.0__b77a5c561934e089\System.Core.dll'
'pictureshare.vshost.exe' (Managed (v4.0.30319)): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System.Deployment\v4.0_4.0.0.0__b03f5f7f11d50a3a\System.Deployment.dll'
'pictureshare.vshost.exe' (Managed (v4.0.30319)): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System.Xml\v4.0_4.0.0.0__b77a5c561934e089\System.Xml.dll'
'pictureshare.vshost.exe' (Managed (v4.0.30319)): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System.Xml.Linq\v4.0_4.0.0.0__b77a5c561934e089\System.Xml.Linq.dll'
'pictureshare.vshost.exe' (Managed (v4.0.30319)): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System.Data.DataSetExtensions\v4.0_4.0.0.0__b77a5c561934e089\System.Data.DataSetExtensions.dll'
'pictureshare.vshost.exe' (Managed (v4.0.30319)): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_MSIL\Microsoft.VisualBasic\v4.0_10.0.0.0__b03f5f7f11d50a3a\Microsoft.VisualBasic.dll'
The thread 'vshost.NotifyLoad' (0x1650) has exited with code 0 (0x0).
The thread '<No Name>' (0x360) has exited with code 0 (0x0).
The thread '<No Name>' (0x153c) has exited with code 0 (0x0).
The thread 'vshost.LoadReference' (0xe64) has exited with code 0 (0x0).
'pictureshare.vshost.exe' (Managed (v4.0.30319)): Loaded 'C:\Users\Jacob\documents\visual studio 2012\Projects\pictureshare\pictureshare\bin\Debug\pictureshare.exe', Symbols loaded.
'pictureshare.vshost.exe' (Managed (v4.0.30319)): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System.Runtime.Remoting\v4.0_4.0.0.0__b77a5c561934e089\System.Runtime.Remoting.dll'
A first chance exception of type 'System.IO.FileNotFoundException' occurred in System.Windows.Forms.dll
The thread 'vshost.RunParkingWindow' (0xb60) has exited with code 0 (0x0).
The thread '<No Name>' (0x1010) has exited with code 0 (0x0).
The program '[3844] pictureshare.vshost.exe: Program Trace' has exited with code 0 (0x0).
The program '[3844] pictureshare.vshost.exe: Managed (v4.0.30319)' has exited with code 0 (0x0).

Any ideas on how to fix this?

On which button click did the error occur?

Here are some recommended changes:

    Private Sub OpenFileDialog1_FileOk(sender As Object, e As System.ComponentModel.CancelEventArgs)
       ' Dim strm As System.IO.Stream       delete this and the next line
       ' strm = OpenFileDialog1.OpenFile()
        TextBox1.Text = OpenFileDialog1.FileName.ToString()
        If Not (strm Is Nothing) Then
            ' Display the image

            ' Since you are using ImageLocation in Button2_Click change the next line

           ' PictureBox1.Image = Image.FromFile(OpenFileDialog1.FileName)

           ' to this:

            PictureBox1.ImageLocation = OpenFileDialog1.FileName ' this will also load the file

            Button2.Show()
            Button3.Show()
        End If
    End Sub

Also: Private Sub OpenFileDialog1_FileOk(sender As Object, e As System.ComponentModel.CancelEventArgs)

should be: Private Sub OpenFileDialog1_FileOk(sender As Object, e As System.ComponentModel.CancelEventArgs) Handles OpenFileDialog1.FileOk

It works, but the image does not update. I clicked Button1 and the other buttons don't show.

Also, the code will not compile without this line:
Dim strm As System.IO.Stream

Sorry, I typed out those comments to quickly. If you have the OFD.CheckFileExists set to True (the default setting) then that code block could be like this:

    Private Sub OpenFileDialog1_FileOk(sender As Object, e As System.ComponentModel.CancelEventArgs) Handles OpenFileDialog.FileOk

        TextBox1.Text = OpenFileDialog1.FileName.ToString()

        ' Display the image

        PictureBox1.ImageLocation = OpenFileDialog1.FileName ' this will also load the file

        Button2.Show()
        Button3.Show()

    End Sub

Also, when I try to upload the images, I get a error saying URIExceptionFormat, screenshot below:
http://www.imgbomb.com/i/d15/svkuc.png

My host says that I have to put the URL like this:
ftp.mysite.com/

Anyway to do this?

**EDIT: I fixed this error, now it says I have a error with the "WebException was Unhandled" and it says below that "System Error", the code it highlighted that caused the error was:

Dim clsStream As System.IO.Stream = _
        clsRequest.GetRequestStream()
        clsStream.Write(bFile, 0, bFile.

**

Edited 3 Years Ago by Djmann1013: Edit/Update

I think you upload code is incorrect.
Here is a translated version of the code MS has at: How to: Upload Files with FTP

      ' Get the object used to communicate with the server.
      Dim request As Net.FtpWebRequest = CType(Net.WebRequest.Create("ftp://www.contoso.com/test.htm"), Net.FtpWebRequest)
      request.Method = Net.WebRequestMethods.Ftp.UploadFile

      ' This example assumes the FTP site uses anonymous logon.
      request.Credentials = New Net.NetworkCredential("anonymous", "janeDoe@contoso.com")

      ' Copy the contents of the file to the request stream.
      Dim sourceStream As New IO.StreamReader("testfile.txt")
      Dim fileContents() As Byte = System.Text.Encoding.UTF8.GetBytes(sourceStream.ReadToEnd())
      sourceStream.Close()
      request.ContentLength = fileContents.Length

      Dim requestStream As IO.Stream = request.GetRequestStream()
      requestStream.Write(fileContents, 0, fileContents.Length)
      requestStream.Close()

      Dim response As Net.FtpWebResponse = CType(request.GetResponse(), Net.FtpWebResponse)

      Console.WriteLine("Upload File Complete, status {0}", response.StatusDescription)

      response.Close()

It's giving me an error 553 when trying to upload with the code you gave me. What do I do to fix this?

This article has been dead for over six months. Start a new discussion instead.