我需要保存一個窗體用戶瀏覽圖像&將其設置爲PictureBox但在另一個按鈕上,我需要將該圖像保存到SQL Server。存儲過程用INSERT命令(image數據類型)從桌面如何在VB.NET(Windows窗體)中將PictureBox圖像保存到SQL
瀏覽圖片,圖片框代碼: -
Public Sub SelectImage()
With OpenFileDialog1
'.InitialDirectory = "C:\"
.Filter = "All Files|*.*|Bitmaps|*.bmp|GIFs|*.gif|JPEGs|*.jpg"
.FilterIndex = 4
End With
If OpenFileDialog1.ShowDialog() = DialogResult.OK Then
PictureBox1.Image = Image.FromFile(OpenFileDialog1.FileName)
PictureBox1.SizeMode = PictureBoxSizeMode.StretchImage
PictureBox1.BorderStyle = BorderStyle.Fixed3D
End If
End Sub
保存按鈕代碼
Public Sub Insert_Update_Personal()
Dim UploadImage As Bitmap = PictureBox1.Image
Dim ds As DataSet = New DataSet()
Dim cmd As SqlCommand = New SqlCommand("sp_Insert_Update_Personal", con)
con.Open()
cmd.CommandType = CommandType.StoredProcedure
cmd.Parameters.AddWithValue("@childrenage", TextBox10.Text)
cmd.Parameters.AddWithValue("@picture", UploadImage)
cmd.Parameters.AddWithValue("@hrcomments", TextBox5.Text)
cmd.ExecuteNonQuery()
con.Close()
cmd.Dispose()
End Sub
但是當我運行表單時,它給了我錯誤「沒有映射存在從對象類型System.Drawing.Bitmap到已知的託管提供程序本機類型」。
你需要在將圖像推入數據庫之前將圖像轉換爲字節數組。 – 2012-02-02 14:28:24
此外,從原始圖像,而不是從PictureBox。 – Alexander 2013-07-02 05:54:29