我假設你有一個按鈕,在Click事件上打開Picture_OpenFileDialog。所以,你的代碼必須是這樣的:
Imports System.ComponentModel
Imports System.IO
Imports System.Globalization
Public Class Form1
Dim extn As String
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Picture_OpenFileDialog.ShowDialog()
End Sub
Private Sub Picture_OpenFileDialog_FileOk(sender As Object, e As CancelEventArgs) Handles Picture_OpenFileDialog.FileOk
If (CheckFileType(Picture_OpenFileDialog.FileName)) Then
'Make sure the directory you want to save to exist, otherwise you will get DirectoryNotFoundException
'If this is the same directory that your code is running, I suggest you do it this way:
Dim desiredplace As String = Directory.GetCurrentDirectory & "\SAP\Images\" & ExtractFilename(Picture_OpenFileDialog.FileName)
'If not, use your desiredplace
'Dim desiredplace As String = "\\SAP\Images\" & ExtractFilename(Picture_OpenFileDialog.FileName)
FileSystem.FileCopy(Picture_OpenFileDialog.FileName, desiredplace)
Else
MessageBox.Show("Please select a picture with a file format extension of either Bmp, Jpg, Jpeg, Gif or Png.", "invalid image", MessageBoxButtons.OK, MessageBoxIcon.Error)
Exit Sub
End If
End Sub
Function CheckFileType(ByVal fileName As String) As Boolean
Dim ext As String = Path.GetExtension(fileName)
Select Case ext.ToLower()
Case ".gif"
extn = ".gif"
Return True
Case ".png"
extn = ".png"
Return True
Case ".jpg"
extn = ".jpg"
Return True
Case ".jpeg"
extn = ".jpeg"
Return True
Case ".bmp"
extn = ".bmp"
Return True
Case Else
Return False
End Select
End Function
Public Function ExtractFilename(filepath As String) As String
' If path ends with a "\", it's a path only so return String.Empty.
If filepath.Trim().EndsWith("\") Then Return String.Empty
' Determine where last backslash is.
Dim position As Integer = filepath.LastIndexOf("\"c)
' If there is no backslash, assume that this is a filename.
If position = -1 Then
' Determine whether file exists in the current directory.
If File.Exists(Environment.CurrentDirectory + Path.DirectorySeparatorChar + filepath) Then
Return filepath
Else
Return String.Empty
End If
Else
' Determine whether file exists using filepath.
If File.Exists(filepath) Then
' Return filename without file path.
Return filepath.Substring(position + 1)
Else
Return String.Empty
End If
End If
End Function
End Class
或者告訴我其他方法 –