如何在VB.NET(Windows窗体)中将PictureBox图像保存到SQL
我需要在其中保存一个表单,用户浏览图像&将其设置为 PictureBox 但在另一个按钮上,我需要将该图像保存到 SQL Server。我有一个带有插入命令的存储过程(带有图像数据类型)
来自桌面的浏览器图像,PictureBox 代码 :-
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 到已知托管提供程序本机类型的映射。”
I need to save a form in it user browse image & set it to a PictureBox But on another button I need save that image to SQL Server .I have a stored procedure with Insert Command (with Image Datatype)
Browser Image from Desktop, PictureBox Code :-
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
Save Button Code
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
But When I run the form it gives me error "No mapping exists from object type System.Drawing.Bitmap to a known managed provider native type."
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
试试这个:(将 MySqlConnection 更改为 SQLConnection 因为我在示例中使用 MySQL)
然后像这样使用它:
try this: (Change MySqlConnection to SQLConnection because i'm using MySQL in my example)
then use it like this: