Click here to Skip to main content
15,899,314 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
can u give the suggestion how can i insert image from picturebox in binary
Posted

Try:
VB
Dim ms As New MemoryStream()
imageIn.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp)
Return ms.ToArray()
That converts an Image to an array of bytes.
And:
VB
Using con As New SqlConnection(strConnect)
	con.Open()
	Using com As New SqlCommand("INSERT INTO myTable (myColumn1, myColumn2) VALUES (@C1, @C2)", con)
		com.Parameters.AddWithValue("@C1", myValueForColumn1)
		com.Parameters.AddWithValue("@C2", myValueForColumn2)
		com.ExecuteNonQuery()
	End Using
End Using
That inserts items to a DB.

All you have to do is combine the two, and modify to fit your scheme.
 
Share this answer
 
Comments
_Amy 24-Jul-12 7:13am    
Nice Answer. +5!
tiggerc 24-Jul-12 10:07am    
Nicely done, saved me having to type it +5
Code for browse-save-retrieve image

VB
Private Sub btnBrowse_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBrowse.Click
       Dim a As New OpenFileDialog
       a.Filter = "Image(*.jpg,*.png,*.bmp,*.JPeg,*.Gif)|*.jpg;*.png;*.bmp;*.JPeg;*.Gif;"
       a.Title = "Browse icon for ..."
       If a.ShowDialog = DialogResult.OK Then
           Dim img As Bitmap = Image.FromFile(a.FileName)
           img.SetResolution(72, 72)
           PictureBox1.BackgroundImage = img
           PictureBox1.BackgroundImageLayout = ImageLayout.Stretch
           IsChanged = True
       End If
   End Sub



VB
'''read image from picturebox an Save image in sql in binary format
'note  IsChanged is boolean variable, declare on page level, set it true when image is changed. do not update image when there is no change in it.
        If IsChanged = True Then
            IsChanged = False
            Dim stream1 As New IO.MemoryStream
            Dim img As Bitmap
            img = PictureBox1.BackgroundImage
            Using img
                Using stream As New IO.MemoryStream
                    Try
                        img.Save(stream, Imaging.ImageFormat.Bmp)
                    Catch ex As Exception
                        Try
                            img.Save(stream, Imaging.ImageFormat.Png)
                        Catch ex1 As Exception
                            Try
                                img.Save(stream, Imaging.ImageFormat.Gif)
                            Catch ex2 As Exception
                                Try
                                    img.Save(stream, Imaging.ImageFormat.MemoryBmp)
                                Catch ex3 As Exception

                                End Try
                            End Try

                        End Try
                    End Try
                    stream1 = stream
                    stream.Dispose()
                End Using
            End Using
            Try
                Cmd.CommandText = "update Dtl set Img=@Img"
                Cmd.Parameters.Clear()
                Cmd.Parameters.AddWithValue("@Img", stream1.GetBuffer)
                Cmd.ExecuteNonQuery()
                Cmd.Parameters.Clear()
                stream1.Dispose()
            Catch ex As Exception
            End Try


VB
'''fatch image from database & display in picture box
            Try
                Dim dt As New DataTable
                dt = DBGetData("select * from Dtl")
                For i = 0 To dt.Rows.Count - 1
                    txtNm.Text = dt.Rows(i)("Nm")
                    txtAdd.Text = dt.Rows(i)("Add")
                    txtWebsite.Text = dt.Rows(i)("Website")
                    txtMailId.Text = dt.Rows(i)("MailId")
                    txtCont1.Text = dt.Rows(i)("ContNo1")
                    txtCont2.Text = dt.Rows(i)("ContNo2")

                    Dim ImgData As Byte() = DirectCast(dt.Rows(i)("Img"), Byte())

                    Dim stream As New IO.MemoryStream(ImgData)
                    Using stream
                        Img = Image.FromStream(stream)
                    End Using
                    stream.Dispose()
                    PictureBox1.BackgroundImage = Img
                    PictureBox1.BackgroundImageLayout = ImageLayout.Stretch
                Next
            Catch ex As Exception
            End Try


Happy Coding!
:)
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900