我从表单浏览照片(比如C:/ pictures /)并将其保存到另一个文件夹(D:/ savedimages /)。如何在savedimages文件夹中获取图像位置?我需要在SQL中保存路径。
这是我在获取和保存img时使用的代码:
Dim filename As String
Dim getlocation As String
Sub saveimg()
'Get next studid
Dim cmd As New MySqlCommand("SELECT id FROM student", conn)
Dim reader As MySqlDataReader
reader = cmd.ExecuteReader()
While reader.Read()
filename = (reader.GetInt32(0))
End While
reader.Close()
'Save image
Dim path As String = ".\Photos\"
If (Not System.IO.Directory.Exists(path)) And PictureBox1.ImageLocation <> Nothing Then
System.IO.Directory.CreateDirectory(path)
PictureBox1.Image.Save(".\Photos\" & filename & ".png")
ElseIf PictureBox1.ImageLocation <> Nothing Then
PictureBox1.Image.Save(".\Photos\" & filename & ".png")
End If
getlocation = ".\Photos\" & filename & ".png"
End Sub
我将路径保存到数据库时的代码,遗憾的是它没有保存任何数据。
Dim list As ListViewItem
Sub addstud()
Dim strings As String = "INSERT INTO student (img) VALUES ('" & getlocation & "')"
Dim sqlcommand As New MySqlCommand
Dim sqladapter As New MySqlDataAdapter
Dim Table As New DataTable
With sqlcommand
.CommandText = strings
.Connection = conn
End With
With sqladapter
.SelectCommand = sqlcommand
.Fill(Table)
End With
Main.lvStudent.Items.Clear()
For x As Integer = 0 To Table.Rows.Count - 1
list = Main.lvStudent.Items.Add(Table(x)("id").ToString.PadLeft(4, "0"))
With list
.SubItems.Add(Table(x)("lname").ToString)
.SubItems.Add(Table(x)("fname").ToString)
.SubItems.Add(Table(x)("mname").ToString)
.SubItems.Add(Table(x)("yearlevel").ToString)
.SubItems.Add(Table(x)("section").ToString)
End With
With Main.lvStudent
.Items(x).Font = New Font("Century Gothic", 8, FontStyle.Regular)
End With
Next
saveimg()
MessageBox.Show("Student successfully added!")
End Sub
我想要发生的事例: 我从C:/ pictures /浏览了image1.jpg,我将它保存在D:/ savedimages /中,现在我想在sql = D中保存路径:/savedimages/image1.jpg
谢谢!
答案 0 :(得分:0)
尝试使用System.IO.DirectoryInfo,如下所示
Dim path As String = ".\Photos\"
Dim dir As System.IO.DirectoryInfo 'for saving folder name
If (Not System.IO.Directory.Exists(path)) And PictureBox1.ImageLocation <> Nothing Then
dir = System.IO.Directory.CreateDirectory(path)
getlocation = dir.FullName & filename & ".png"
PictureBox1.Image.Save(getlocation)
ElseIf PictureBox1.ImageLocation <> Nothing Then
getlocation = New System.IO.DirectoryInfo(path).FullName & filename & ".png"
PictureBox1.Image.Save(getlocation)
End If