[英]How to save PictureBox Image to SQL in VB.NET ( Windows Form )
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) 我需要在其中保存一个表单,用户浏览图像并将其设置为PictureBox,但是在另一个按钮上,我需要将该图像保存到SQL Server。我有一个带有Insert Command(带有Image数据类型)的存储过程。
Browser Image from Desktop, PictureBox Code :- 来自桌面的浏览器图像,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
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." 但是,当我运行表单时,它给我错误“从对象类型System.Drawing.Bitmap到已知的托管提供程序本机类型不存在映射”。
try this: ( Change MySqlConnection to SQLConnection because i'm using MySQL in my example ) 试试这个:(将MySqlConnection更改为SQLConnection,因为我在示例中使用MySQL )
Public Shared Function ByteToImage(ByVal blob() As Byte) As Bitmap
Dim mStream As New MemoryStream
Dim pData() As Byte = DirectCast(blob, Byte())
mStream.Write(pData, 0, Convert.ToInt32(pData.Length))
Dim bm As Bitmap = New Bitmap(mStream, False)
mStream.Dispose()
Return bm
End Function
then use it like this: 然后像这样使用它:
Private Function InsertImage(ByVal ImagePath As String, ByVal oIDNum As String) As Boolean
Dim iPhoto() As Byte = jwImage.FileImageToByte(ImagePath)
Dim xBool As Boolean = False
Using xConn As New MySqlConnection(ConnectionClass.ConnectionString)
Try
Dim xComm As New MySqlCommand
With xComm
.CommandText = "InsertImage"
.Connection = xConn
.CommandType = CommandType.StoredProcedure
.Parameters.AddWithValue("xID", oIDNum)
.Parameters.AddWithValue("xImage", iPhoto)
End With
xConn.Open()
xComm.ExecuteNonQuery()
xComm.Dispose()
xBool = True
Catch ex As MySqlException
MessageBox.Show(ex.Message, "MySQL Error: " & ex.Number, MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
xBool = False
End Try
End Using
Return xBool
End Function
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.