Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. Saving & Retrieving An Image

Saving & Retrieving An Image

Scheduled Pinned Locked Moved Visual Basic
databasesql-serversysadminhelptutorial
4 Posts 4 Posters 2 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • U Offline
    U Offline
    User 3672257
    wrote on last edited by
    #1

    I need some help with saving and retrieving images with microsoft sql server. I have created a form that captures personal information and a picture. I am having problems with how to saving this data with the picture. I will also like to display this data at a later time or in another form. Please help.

    L R 2 Replies Last reply
    0
    • U User 3672257

      I need some help with saving and retrieving images with microsoft sql server. I have created a form that captures personal information and a picture. I am having problems with how to saving this data with the picture. I will also like to display this data at a later time or in another form. Please help.

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      There are a lot of examples out there for just that, such as Visual Basic .NET Code Sample: Read and Write Images from a Database[^].

      1 Reply Last reply
      0
      • U User 3672257

        I need some help with saving and retrieving images with microsoft sql server. I have created a form that captures personal information and a picture. I am having problems with how to saving this data with the picture. I will also like to display this data at a later time or in another form. Please help.

        R Offline
        R Offline
        Rupesh Kumar Swami
        wrote on last edited by
        #3

        hi, follow step by step 1. first of all i assume PictureBox1 hold the picture data. So use following statement Dim PictureBox1Data As Byte() If PictureBox1.Image Is Nothing Then PictureBox1Data = Nothing Else PictureBox1Data = imageToByteArray(PictureBox1.Image)'See below this function End If 2.Now write the SQL query as following Dim QueryText As String Dim QueryValue As String Dim command As System.Data.SqlClient.SqlCommand = New System.Data.SqlClient.SqlCommand() QueryText = "INSERT INTO Settings(PrintCompany,IsUploadLogo,LogoGraphic )" QueryValue = "Values(@PrintCompany, @IsUploadLogo,@LogoGraphic)" command.CommandText = QueryText & QueryValue 3. Supply Value to parameter which is in above query, as following command.Parameters.Add("@PrintCompany", SqlDbType.TinyInt, 10).Value = True command.Parameters.Add("@IsUploadLogo", SqlDbType.TinyInt, 10).Value =True If Not PictureBox1Data Is Nothing Then command.Parameters.Add("@LogoGraphic", SqlDbType.Image, PictureBox1Data.Length).Value = PictureBox1Data Else command.Parameters.Add("@LogoGraphic", SqlDbType.Image, 0).Value = System.DBNull.Value End If 4. Execute query as following command.ExecuteNonQuery() step 1 to 4 read data of picture box,convert them byte array and store in related table.Step 1 use following function to convert an image into byte array Public Function imageToByteArray(ByVal ImageIn As System.Drawing.Image) As Byte() Dim ms As MemoryStream = New MemoryStream() Dim FormatImage1 As Imaging.ImageFormat = ImageIn.RawFormat ImageIn.Save(ms, FormatImage1) 'ImageIn.Save(ms, Imaging.ImageFormat.Bmp) Return ms.ToArray() End Function 5. Now Use following statement to get dataset str = "select * from Settings" Dim ds As New DataSet ds = mdIns.GetResultFromQuery(str)'this function return dataset 6. use dataset record as following If ds.Tables(0).Rows.Count > 0 Then chkPrintCompanyName.Checked = CBool(ds.Tables(0).Rows(0).Item(0)) chkLogoGraphic.Checked = CBool(ds.Tables(0).Rows(0).Item(1)) If Not IsDBNull(ds.Tables(0).Rows(0).Item(4)) And Not ds.Tables(0).Rows(0).Item(2) Is Nothing Then PictureBox1.Image = ByteArrayToimage(ds.Tables(0).Rows(0).Item(2))

        E 1 Reply Last reply
        0
        • R Rupesh Kumar Swami

          hi, follow step by step 1. first of all i assume PictureBox1 hold the picture data. So use following statement Dim PictureBox1Data As Byte() If PictureBox1.Image Is Nothing Then PictureBox1Data = Nothing Else PictureBox1Data = imageToByteArray(PictureBox1.Image)'See below this function End If 2.Now write the SQL query as following Dim QueryText As String Dim QueryValue As String Dim command As System.Data.SqlClient.SqlCommand = New System.Data.SqlClient.SqlCommand() QueryText = "INSERT INTO Settings(PrintCompany,IsUploadLogo,LogoGraphic )" QueryValue = "Values(@PrintCompany, @IsUploadLogo,@LogoGraphic)" command.CommandText = QueryText & QueryValue 3. Supply Value to parameter which is in above query, as following command.Parameters.Add("@PrintCompany", SqlDbType.TinyInt, 10).Value = True command.Parameters.Add("@IsUploadLogo", SqlDbType.TinyInt, 10).Value =True If Not PictureBox1Data Is Nothing Then command.Parameters.Add("@LogoGraphic", SqlDbType.Image, PictureBox1Data.Length).Value = PictureBox1Data Else command.Parameters.Add("@LogoGraphic", SqlDbType.Image, 0).Value = System.DBNull.Value End If 4. Execute query as following command.ExecuteNonQuery() step 1 to 4 read data of picture box,convert them byte array and store in related table.Step 1 use following function to convert an image into byte array Public Function imageToByteArray(ByVal ImageIn As System.Drawing.Image) As Byte() Dim ms As MemoryStream = New MemoryStream() Dim FormatImage1 As Imaging.ImageFormat = ImageIn.RawFormat ImageIn.Save(ms, FormatImage1) 'ImageIn.Save(ms, Imaging.ImageFormat.Bmp) Return ms.ToArray() End Function 5. Now Use following statement to get dataset str = "select * from Settings" Dim ds As New DataSet ds = mdIns.GetResultFromQuery(str)'this function return dataset 6. use dataset record as following If ds.Tables(0).Rows.Count > 0 Then chkPrintCompanyName.Checked = CBool(ds.Tables(0).Rows(0).Item(0)) chkLogoGraphic.Checked = CBool(ds.Tables(0).Rows(0).Item(1)) If Not IsDBNull(ds.Tables(0).Rows(0).Item(4)) And Not ds.Tables(0).Rows(0).Item(2) Is Nothing Then PictureBox1.Image = ByteArrayToimage(ds.Tables(0).Rows(0).Item(2))

          E Offline
          E Offline
          EngrImad
          wrote on last edited by
          #4

          I have got same issue exactly, while retrieving image from Database to PitcureBox getting error saying that "Parameters is Not Valid", it is look like there is an issue when save the image and converted to string in DB, hope to have some solutions,

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups