Reputation: 23
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."
Upvotes: 1
Views: 17620
Reputation: 263683
try this: (Change MySqlConnection to SQLConnection because i'm using MySQL in my example)
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
Upvotes: 1