The code for this is:

'IMAGE HANDLING
                Dim bytes() As Byte = DS.Tables("Location").Rows(0).Item(4)
                Dim ProductImage As Image = Image.FromStream(New System.IO.MemoryStream(bytes))
                pic_ProductImageDisplay.Image = ProductImage
                pic_ProductImageDisplay.Load()

The error occurs line 3 stating "ArgumentException was unhandled: Parameter is not valid."

Any idea on a solution to this problem?

Recommended Answers

All 8 Replies

I'm not sure if this is the same thing, but it's how I do it in my app.

Me.ImageList1.Images.Add(FilePath, ImageFromBase64String(r2("DBImage")))

Not entirely sure myself if that is the same thing either, never seen it written that way myself.

Here's my full code:

Write it with this.

Dim connectionString As String = DBConnectionString
            Dim sqlConn As New SQLiteConnection(connectionString)

            For i As Integer = 0 To ofd.FileNames.Length - 1
                Dim SQLComm As New SQLiteCommand("INSERT INTO Pictures (PMachineGuid, PictureLocation, PictureComment, " & _
                "PictureName, DateModified, ModifiedBy, DBImage) VALUES (@PMachineGuid, @PictureLocation, @PictureComment, " & _
                "@PictureName, @DateModified, @ModifiedBy, @DBImage)", sqlConn)
                sFileName = IO.Path.GetFileName(ofd.FileNames(i))

                SQLComm.Parameters.Add(New SQLiteParameter("@PMachineGuid", gMachineGuid))
                SQLComm.Parameters.Add(New SQLiteParameter("@PictureLocation", IO.Path.GetDirectoryName((ofd.FileNames(i)))))
                SQLComm.Parameters.Add(New SQLiteParameter("@PictureComment", sFileName))
                SQLComm.Parameters.Add(New SQLiteParameter("@PictureName", ofd.FileNames(i)))
                SQLComm.Parameters.Add(New SQLiteParameter("@DateModified", Now()))
                SQLComm.Parameters.Add(New SQLiteParameter("@ModifiedBy", GetCurrentUser))

                Try
                    'TODO: ## Memory error on the line below ##
                    SQLComm.Parameters.Add(New SQLiteParameter("@DBImage", ImageToBase64String(ResizeImage(Image.FromFile(ofd.FileNames(i)), 800), System.Drawing.Imaging.ImageFormat.Jpeg)))
                Catch ex As Exception
                    MsgBox(ex.Message & " - Import limit reached.  Try importing fewer images at one time.")
                    GoTo Jump
                End Try

Read it with this.

Dim SQLComm2 As New SQLiteCommand("SELECT * FROM Pictures WHERE " & _
                    "ProjectGuid =" & gProjectGuid & " AND PHRNGuid IS NULL", sqlConn2)

            sqlConn2.Open()

            Dim r2 As SQLiteDataReader = SQLComm2.ExecuteReader()

            While r2.Read()

                FileName = r2("PictureName")
                FileFolder = r2("PictureLocation")
                FilePath = FileFolder & "\" & FileName

                Me.ImageList1.Images.Add(FilePath, ImageFromBase64String(r2("DBImage")))

                Dim li As ListViewItem
                li = ListView1.Items.Add(FileName, FilePath)
                li.Tag = r2("PictureGuid")

            End While
Member Avatar for Unhnd_Exception

Try this. I know this works.
If this doesn't work you have a problem with your byte data.

Dim BitmapFromBytes As Bitmap
  Dim BitmapBytes As Byte()
  Dim BitmapStream As System.IO.MemoryStream

  ' BitmapBytes = DS.Tables("Location").Rows(0).Item(4)
  BitmapBytes = New Byte() {1} 'Replace with above.
  BitmapStream = New System.IO.MemoryStream(BitmapBytes)
  BitmapFromBytes = New Bitmap(BitmapStream)

@Unhnd_Exception

Using your code i still receive the same error, therefore I can also assume its the byte data. Do you have a method you use for converting the image to byte data to store in the database as it seems my current code hasn't worked properly.

Would be most appreciated,
Mike

This is the code i use to write a image to a sql table in binary along with come other information. I have a fileuplaod control on the page with a button that adds the data which calls this code. Make sure your column in your sql table that is going to hold the image data is set to data type 'image'

Dim FilePath As String = FileUpload.PostedFile.FileName.ToString
        Dim FileSizeTemp As Double = (FileUpload.PostedFile.ContentLength / 1000)
        Dim FileType As String = FileUpload.PostedFile.ContentType
        Dim FileBytes(FileUpload.PostedFile.InputStream.Length) As Byte
        FileUpload.PostedFile.InputStream.Read(FileBytes, 0, FileBytes.Length)
        Dim FileExt As String
        Dim FileName As String
        Dim FileSize As String

        FileSize = Math.Round(FileSizeTemp, 0) & " KB"

        FileName = FilePath.Substring(FilePath.LastIndexOf("\"), FilePath.Length - FilePath.LastIndexOf("\"))
        FileName = FileName.Replace("\", "")

        FileExt = FileName.Substring(FileName.LastIndexOf("."), FileName.Length - FileName.LastIndexOf("."))

        Dim sqlcmd As SqlCommand
        Dim SQL As String

        SQL = "INSERT INTO PropertyFiles(PropId,Filename, FileType, FileData, FileSize, FilePath, FileExt) "
        SQL = SQL + "VALUES ('" & Session("PropID") & "', '" & FileName & "', '" & FileType & "',@ImageData,'" & FileSize & "','" & FilePath & "','" & FileExt & "')"

        sqlcmd = New SqlCommand(SQL, SQLconn)

        sqlcmd.Parameters.Add("@ImageData", SqlDbType.Image).Value = FileBytes

        SQLconn.Open()

        sqlcmd.ExecuteNonQuery()
        SQLconn.Close()

Then to retrieve the image use something like this

Dim SQL As String = ""

        Dim fileds As New DataSet
        SQLconn.Open()
        SQL = "SELECT FileID, FileName, FileType, FileSize, DateAdded, FileExt, FileData FROM PropertyFiles WHERE FileID = " & FileID
        Dim filecmd As New SqlDataAdapter(SQL, SQLconn)
        filecmd.Fill(fileds)

        If fileds.Tables(0).Rows.Count > 0 Then
            Response.ContentType = fileds.Tables(0).Rows(0).Item(2).ToString()
            Response.BinaryWrite(fileds.Tables(0).Rows(0).Item(6))
        Else
            MsgBox("Errors")

        End If

        SQLconn.Close()
commented: that is a complete solution +8

Many thanks ninjaimp, that helped alot :)

Member Avatar for Unhnd_Exception

Just to add a matching set to my example.

To get the byte data to store.

Dim OriginalBitmap As New Bitmap(100, 100)
 Dim MemoryStream As New System.IO.MemoryStream

 OriginalBitmap.Save(MemoryStream, Imaging.ImageFormat.Png)

 Dim BytesReadyToStoreInTable As Byte() = MemoryStream.ToArray
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.