locked
How Do I Save An Image in a Picture Box to SQL Database Image Field RRS feed

  • Question

  • Using VB.net 2010, Windows 7, and SQL Server Express 2008 R2.

    I am trying to save an image from a picture box to an image field in SQL Server.  The line of code below produces an error.  What am I doing wrong?

    ...code

    objCommand.Parameters.AddWithValue("@ss_no", mskSS_No.Text)

    objCommand.Parameters.AddWithValue("@d_code", txtD_Code.Text)

    objCommand.Parameters.AddWithValue("@photo", picPhoto.Image)  <------- produces error

     

    ...more code

    Any help would be appreciated.

     

    Thursday, February 3, 2011 11:28 PM

Answers

  • Jeff,

    Jeff in order to save to the photo you have to create a byte array of the image and pass it into the SavePhoto Method.  Here is an example of how to do it:

    '- Create a new memory stream object 
    Dim m As New IO.MemoryStream
    
    '- Get the image from the picture control
     PictureBoxControl.Image.Save(m, System.Drawing.Imaging.ImageFormat.Jpeg)
    
    '- Convert the stream to the byte array (Blob for SQL)
     Dim b() As Byte = m.ToArray()
    
    '- Call the save
    SavePhoto(b())
    
    

    Hope that helps.  Best of luck


    Robert McArthur
    • Marked as answer by Jeff07 Tuesday, February 8, 2011 12:49 AM
    Friday, February 4, 2011 9:13 PM

All replies

    • Marked as answer by Jie Bao Friday, February 4, 2011 6:23 AM
    • Unmarked as answer by Jie Bao Friday, February 4, 2011 6:23 AM
    • Proposed as answer by Jie Bao Friday, February 4, 2011 6:23 AM
    Thursday, February 3, 2011 11:54 PM
  • Hello Jeff07,

    1) Make sure the data type in SQL is image
    2) Image Type in sql is acutally a Byte Array, so you store it in code as a byte()

    Example of reading in from SQL

    Dim oMemoryStream As New IO.MemoryStream(moEmployee.Photo)
    picEmployeePhoto.Image = Image.FromStream(oMemoryStream)
    

    For your isssue of saving:

    Public Sub SavePhoto(ByVal bytPhoto As Byte())
    
       '-Code Here for connection object
    
       '-Code here for command object
    
       '- Code for Parameters
       oCommand.Parameters.Add("@photo", SqlDbType.Image, bytPhoto.Length).Value = bytPhoto
    
       '- Update the database
       oCommand.ExecuteNonQuery()
    
    End Sub
    

    Hope this helps you,

     

    Robert McArthur


    Robert McArthur
    Friday, February 4, 2011 12:11 AM
  • I created a SavePhoto proceedure:

    Public Sub SavePhoto(ByVal bytPhoto As Byte())


            Dim objCommand As SqlCommand = New SqlCommand

            objCommand.Connection = objConnection

            objCommand.CommandText = "UPDATE names " & _
                    "SET photo = @photo " & _
                    "WHERE account_no = @account_no"

            objCommand.CommandType = CommandType.Text

            objCommand.Parameters.Add("@photo", SqlDbType.Image, bytPhoto.Length).Value = bytPhoto

            Try
                objCommand.ExecuteNonQuery()
            Catch SqlExceptionErr As SqlException
                MessageBox.Show(SqlExceptionErr.Message)
            End Try
        End Sub

     

    My newbie question is:  What goes in the parentheses of the SavePhoto() procedure.  My photo is in a picture box, picPhoto, and not in a file per se.

    Thanks for taking the time to answer my question.

    Friday, February 4, 2011 4:34 PM
  • Jeff,

    Jeff in order to save to the photo you have to create a byte array of the image and pass it into the SavePhoto Method.  Here is an example of how to do it:

    '- Create a new memory stream object 
    Dim m As New IO.MemoryStream
    
    '- Get the image from the picture control
     PictureBoxControl.Image.Save(m, System.Drawing.Imaging.ImageFormat.Jpeg)
    
    '- Convert the stream to the byte array (Blob for SQL)
     Dim b() As Byte = m.ToArray()
    
    '- Call the save
    SavePhoto(b())
    
    

    Hope that helps.  Best of luck


    Robert McArthur
    • Marked as answer by Jeff07 Tuesday, February 8, 2011 12:49 AM
    Friday, February 4, 2011 9:13 PM