i have seen many article which show how to save an image into sql database but i haven't seen such an article which tell me how to update an opened image from database after applying an effect on it
so i have written small program to do this task
i create a table in Northwind database using tis code
Dim cmd As String = "create table Picture" & "(" & "PictureID int IDENTITY(0,1) NOT NULL PRIMARY KEY, " & "PictureName varchar(40) NOT NULL, " & "Picture Image NOT NULL" & ");"
Dim c As SqlConnection = New SqlConnection(c_string)
Dim cm As SqlCommand = New SqlCommand(cmd, c)
after creating table in the database,i save into that table using this code
Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
Dim dlg As OpenFileDialog = New OpenFileDialog()
dlg.Filter = "All Pictures|*.bmp;*.gif;*.jpg|Bitmaps|*.bmp|GIFs|*.gif|JPEGs|*.jpg"
If dlg.ShowDialog() = Windows.Forms.DialogResult.OK Then
PictureBox1.Image = New Bitmap(dlg.FileName)
Dim name As String = dlg.FileName.Substring(dlg.FileName.LastIndexOf("\") + 1, dlg.FileName.Length - dlg.FileName.LastIndexOf("\") - 1)
End If
Dim mstr As IO.MemoryStream = New IO.MemoryStream()
PictureBox1.Image.Save(mstr, PictureBox1.Image.RawFormat)
Dim arrImage As Byte() = mstr.GetBuffer()
Dim cmd As String = "insert into Picture (PictureName, Picture) values (@PName, @Pic)"
Dim c As SqlConnection = New SqlConnection(c_string)
Dim comm As SqlCommand = New SqlCommand(cmd, c)
comm.Parameters.Add(New SqlParameter("@PName", SqlDbType.VarChar, 40)).Value = Name
comm.Parameters.Add(New SqlParameter("@Pic", SqlDbType.Image)).Value = arrImage
Try
c.Open()
comm.ExecuteNonQuery()
Catch err As SqlException
MessageBox.Show(err.Message)
Finally
c.Close()
End Try
listBox1.Items.Add(Name)
End Sub
above code saves an image into the database
i have another button for Update
so i have written this code under its click event
Dim mstr As IO.MemoryStream = New IO.MemoryStream()
PictureBox1.Image.Save(mstr, PictureBox1.Image.RawFormat)
Dim arrImage As Byte() = mstr.GetBuffer()
Dim cmd As String = "insert into Picture (PictureName, Picture) values (@PName, @Pic)"
Dim c As SqlConnection = New SqlConnection(c_string)
Dim comm As SqlCommand = New SqlCommand(cmd, c)
comm.Parameters.Add(New SqlParameter("@PName", SqlDbType.VarChar, 40)).Value = Name
comm.Parameters.Add(New SqlParameter("@Pic", SqlDbType.Image)).Value = arrImage
Try
c.Open()
comm.ExecuteNonQuery()
Catch err As SqlException
MessageBox.Show(err.Message)
Finally
c.Close()
End Try
but it is not updating my image and comes this error
Value cannot be null.
Parameter name: encoder
on the following line
PictureBox1.Image.Save(mstr, PictureBox1.Image.RawFormat)
any body can help me??