none
FileStream or MemoryStream RRS feed

  • Question

  • Hi... I have and application that writes and reads images to a database. What I would like to do.. is display that image for the datagridview row selected in a picturebox. Would it be best to stream the image to a file on the disk and point the picturebox to look at that.. or Memory stream it to the picturebox somehow?

    Tuesday, October 13, 2015 3:22 PM

Answers

  • Thanks Thorsten... it is byte[].. figured it out.  and thank you for the suggestion of using memorystream.   the pictures are about 450 Kb each.. some records can have several pictures.. i'll just display one at a time though.  
    • Marked as answer by LandLord324 Tuesday, October 13, 2015 5:10 PM
    Tuesday, October 13, 2015 5:09 PM

All replies

  • I think you can skip the stream if you want to. Something like this:

        Private Sub DataGridView1_SelectionChanged(sender As Object, e As EventArgs) Handles DataGridView1.SelectionChanged
            Dim theFile As String = DataGridView1.Rows.Item(DataGridView1.CurrentCell.RowIndex).Cells(0).Value
            If theFile IsNot Nothing Then
                PictureBox1.Image = Image.FromFile(theFile)
            End If
        End Sub
    

    But maybe I don't understand all the details?

    Tuesday, October 13, 2015 3:47 PM
  • Hi,

    [assuming the image is saved to the db as byte[] in a blob]

    depends how large the image is. I ususally would use a MemoryStream, since reading and writing to the RAM normally is faster than reading/writing to disk.

    1st: Read the bytes from the database

    2nd: setup a MemoryStream with the byte-buffer

    3rd:Use Image.FromStream with the MemoryStream to get the Image object

    Regards,

      Thorsten


    Tuesday, October 13, 2015 3:48 PM
  • Hi,

    [assuming the image is saved to the db as byte[] in a blob]

    depends how large the image is. I ususally would use a MemoryStream, since reading and writing to the RAM normally is faster than reading/writing to disk.

    Regards,

      Thorsten


    Oh, I was thinking the file is on disk already. If it is in a database then you should probably do it this way.
    Tuesday, October 13, 2015 3:52 PM
  • Thanks Thorsten... it is byte[].. figured it out.  and thank you for the suggestion of using memorystream.   the pictures are about 450 Kb each.. some records can have several pictures.. i'll just display one at a time though.  
    • Marked as answer by LandLord324 Tuesday, October 13, 2015 5:10 PM
    Tuesday, October 13, 2015 5:09 PM
  • thanks for the reply. the file would have been on disk somewhere, multi users, could be anywhere on the network

    Tuesday, October 13, 2015 5:10 PM