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(""), 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?

Dani AI

Generated

A quick note on that Output window line: a first-chance FileNotFoundException just means the debugger saw an exception at the moment it was thrown; the framework may handle it and your app can keep running. It is a signal for debugging, not proof your code failed. You can control when VS breaks on these in Exception Settings. (devblogs.microsoft.com)

On the image picker, is on the right track. If you prefer not to rely on FileOk, a simple pattern is to react to the dialog result and load the picture without holding the file open:

If OpenFileDialog1.ShowDialog(Me) = DialogResult.OK Then
    PictureBox1.Image = Nothing
    PictureBox1.LoadAsync(OpenFileDialog1.FileName) 'non-blocking load
    TextBox1.Text = OpenFileDialog1.FileName
    Button2.Visible = True : Button3.Visible = True
End If

Load/LoadAsync sets ImageLocation under the hood and avoids the common file-lock surprises of Image.FromFile (GDI+ keeps the file locked until the image is disposed). (learn.microsoft.com)

For the upload: your URI must include the ftp scheme and a target filename, not just the folder. A missing scheme (e.g., "ftp.mysite.com/") causes a UriFormatException, and not including a filename commonly triggers server errors. (learn.microsoft.com)

Here is a minimal VB upload that works with binary images and a full ftp URL:

Dim local = OpenFileDialog1.FileName
Dim remote = New Uri("ftp://ftp.website.com/uploads/" & IO.Path.GetFileName(local))

Dim req = CType(Net.WebRequest.Create(remote), Net.FtpWebRequest)
req.Method = Net.WebRequestMethods.Ftp.UploadFile
req.Credentials = New Net.NetworkCredential("ftp_username", "password")
req.UseBinary = True : req.KeepAlive = False

Using src As IO.FileStream = IO.File.OpenRead(local),
      dst As IO.Stream = req.GetRequestStream()
    src.CopyTo(dst)
End Using

Using resp = CType(req.GetResponse(), Net.FtpWebResponse)
    'optional: check resp.StatusDescription
End Using

If you still get 553, the server is saying "file name not allowed" (often due to path/permission issues or disallowed characters). Try uploading to an existing directory, ensure the name is simple (letters, numbers, dot), and verify write permissions. (rfc-editor.org)

Also, consider the standard ShowDialog pattern when opening files; it is the recommended approach in WinForms. (learn.microsoft.com)

Recommended Answers

All 7 Replies

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:

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.

**

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(""), 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?

Sorry about this
Bump

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.