none
DATATABLE COLOUMN (IMAGE)OF TYPE SYSTEM.BYTE[] TO MEMORYSTREAM RRS feed

  • Question

  • Hello everyone,

    I have a table in a database that has a coloumn with images (BLOB). What i am trying to do is Binding it to a Datagridview (already done) and on the click event of any row i get the image field of that row displayed in a picture box. I tried many things and tried searching for similar situations but am not able making it work.

    The coloumn item are of type System.Byte[] and i am trying to get them to Memorystream and then simply PIC_BOX.Image = Image.FromStream(ms). I tried a couple codes that i thought would work.

    However it seems like i am missing something because i get this error:

    Code 1:

    Dim bytes() As Byte = DGV_ALL_CHEQUES.CurrentRow.Cells("Image").Value

            Using ms As New MemoryStream()
                Using source As New MemoryStream(bytes)

                    source.CopyToAsync(ms).GetAwaiter()
                    PB_BROWSE_IMAGE.Image = Image.FromStream(ms)
                End Using
            End Using

    Code 2:

    Dim bytes() As Byte = DGV_ALL_CHEQUES.CurrentRow.Cells("Image").Value

    Using ms As New MemoryStream()
                Dim count As Integer = 0
                While (count < bytes.Length)
                    ms.WriteByte(bytes(count))
                    count += 1
                End While
                     PB_BROWSE_IMAGE.Image = Image.FromStream(ms)
            End Using

    Code 3:

        Dim bytes() As Byte = DGV_ALL_CHEQUES.CurrentRow.Cells("Image").Value

           Using ms As New MemoryStream()
                           ms.Write(DGV_ALL_CHEQUES.CurrentRow.Cells("Image").Value, 0, bytes.Length)
                PB_BROWSE_IMAGE.Image = Image.FromStream(ms)
            End Using

    Error for any of the above : Parameter is not valid (line: PB_BROWSE_IMAGE.Image = Image.FromStream(ms))

    Thanks in advance for any suggestion.

    Tuesday, July 9, 2019 12:37 AM

Answers


  • I Just found where the mistake was.

    I did tiny coding mistake that made me lose a loooooot of time.

    All the above code i tried works fine...the issue is that the table i filled in the database had the Image field filled wrongly. I forgot to add the .ToArray() to convert the memorystream to a byte array while filling the table in the database.

    That caused the subsequent error because when i retrieved the Image from the datatable i read a system.byte[] type of data but it was not the right byte array.

    How should i proceed to put it as solved question ?

    Thanks anyway for your suggestions :) And by the way your code looks awesome !!
    • Marked as answer by eliaray Wednesday, July 10, 2019 1:35 AM
    Wednesday, July 10, 2019 1:21 AM

All replies

  • Hello,

    Take a look at the following article, code is in the following Github repository. The binding is done here on the image from SQL-Server.


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    • Proposed as answer by Alex.F.Young Wednesday, July 10, 2019 1:53 AM
    • Unproposed as answer by Alex.F.Young Wednesday, July 10, 2019 1:53 AM
    Tuesday, July 9, 2019 1:38 AM
    Moderator
  • Hi,

    I tried your code,it works well,my image format is png.FromStream is expecting data in one of these formats:BMP,GIF,JPEG,PNG,TIFF,see the following link:

    https://stackoverflow.com/questions/15479071/system-drawing-image-fromstream-parameter-is-not-valid-on-windows-xp

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, July 9, 2019 1:51 AM
    Moderator
  • Thanks for your feedback !

    Could you kindly advise which of the code i provided you tried ?

    However, Concerning the file format, the images are all PNG or JPEG, but still I am getting the error. I tried putting a breakpoint and debugging all the lines of code and looking at each and every value passed. Things goes well till the line where i write the bytearray to the memorystream. After that line the error occurs.

    Tuesday, July 9, 2019 8:09 PM
  • Hi

    Stab in the dark. Can't hurt to try. DGV can be tricky with respect to the objects returned. This is an attempt to cast to a Byte() array. I can't actually try this out here (I have NO database stuff at all)

    Dim bytes() As Byte = CType(DGV_ALL_CHEQUES.CurrentRow.Cells("Image").Value, Byte())
    					
    Using ms As New IO.MemoryStream(bytes)
    PictureBox1.Image = Image.FromStream(ms)			
    End Using


    Regards Les, Livingston, Scotland


    • Edited by leshay Tuesday, July 9, 2019 8:29 PM
    Tuesday, July 9, 2019 8:28 PM
  • Hello,

    Since I've no indication you tried what I suggested I will make it easy. 

    Class for reading data

    Imports System.Data.SqlClient
    Namespace Classes
        Public Class DataOperations
    
            Public Function DataTable() As DataTable
    
                Dim dt As New DataTable
    
                Dim selectStatement = "SELECT id ,[Description], Picture FROM dbo.Fruits"
    
                Using cn As New SqlConnection With
                    {
                        .ConnectionString =
                            "Data Source=KARENS-PC;Initial Catalog=WorkingImages;" &
                            "Integrated Security=True"
                    }
                    Using cmd As New SqlCommand With {.Connection = cn, .CommandText = selectStatement}
    
                        cn.Open()
                        dt.Load(cmd.ExecuteReader)
                        dt.Columns("Id").ColumnMapping = MappingType.Hidden
                        dt.Columns("Picture").ColumnMapping = MappingType.Hidden
                        dt.Columns("Picture").ReadOnly = True
    
                    End Using
                End Using
    
                Return dt
    
            End Function
        End Class
    End Namespace
    

    Form to read data from above class and sync current row in DataGridView to a PictureBox. 

    Public Class DataGridViewForm
        Private ReadOnly _ops As DataOperations = New DataOperations
        Private ReadOnly _bs As BindingSource = New BindingSource
        Private emptyBitmap As New Bitmap(200, 100)
        Private Sub DataGridViewForm_Load(sender As Object, e As EventArgs) _
            Handles MyBase.Load
    
            Graphics.FromImage(emptyBitmap)
    
            _bs.DataSource = _ops.DataTable()
            DataGridView1.AutoGenerateColumns = False
            DataGridView1.DataSource = _bs
    
            Dim imageBinding As New Binding("Image", _bs, "Picture")
            AddHandler imageBinding.Format, AddressOf BindImage
    
            PictureBoxForDataGridView.DataBindings.Add(imageBinding)
    
        End Sub
        Private Sub BindImage(sender As Object, e As ConvertEventArgs)
            If IsDBNull(e.Value) Then
                e.Value = emptyBitmap
                Exit Sub
            End If
            If e.DesiredType Is GetType(Image) Then
                Using ms As New MemoryStream(CType(e.Value, Byte()))
                    Dim logo = Image.FromStream(ms)
                    e.Value = logo
                End Using
            End If
        End Sub
    End Class


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Tuesday, July 9, 2019 10:59 PM
    Moderator

  • I Just found where the mistake was.

    I did tiny coding mistake that made me lose a loooooot of time.

    All the above code i tried works fine...the issue is that the table i filled in the database had the Image field filled wrongly. I forgot to add the .ToArray() to convert the memorystream to a byte array while filling the table in the database.

    That caused the subsequent error because when i retrieved the Image from the datatable i read a system.byte[] type of data but it was not the right byte array.

    How should i proceed to put it as solved question ?

    Thanks anyway for your suggestions :) And by the way your code looks awesome !!
    • Marked as answer by eliaray Wednesday, July 10, 2019 1:35 AM
    Wednesday, July 10, 2019 1:21 AM