Read MemoryStream - load image \\ byte and read it

2019-06-07 02:04发布

问题:

ok i have image that i bind info in it and i want to read the info

now from file (FileStream) its work

but i want to do it not from file so i need to use MemoryStream

here the example that work and how i do it now how i make it work with MemoryStream (with byte = My.Resources or PictureBox1.image)

Using FS As New IO.FileStream(image, IO.FileMode.Open)
            FS.Seek(0, IO.SeekOrigin.End)
            While Not FS.ReadByte = Asc("|")
                FS.Position -= 2
            End While
            Dim s As String = Nothing
            While Not FS.Position = FS.Length - 4
                s &= Chr(FS.ReadByte.ToString)
            End While
            Dim Ext As String = Nothing
            FS.Seek(0, IO.SeekOrigin.End)
            While Not FS.ReadByte = Asc("*")
                FS.Position -= 2
            End While
            While Not FS.Position = FS.Length
                Ext &= Chr(FS.ReadByte.ToString)
            End While
            FS.Seek(FS.Length - ((s.Length + s) + 5), IO.SeekOrigin.Begin)

            While Not FS.Position = FS.Length - (s.Length + 5)

                Dim Data As Byte() = New Byte(FS.Position) {}
                FS.Read(Data, 0, Data.Length)
                FS.Close()

            End While

at the end save the byte to file

i try to use it like this

Using FS As New IO.MemoryStream(image) 'image = byte()

but not work

how i can do it read it again in memory

thanks

回答1:

This will convert a ByteArray to MemoryStream to Image

  Public Function byteArrayToImage(byteArrayIn As Byte()) As Image
          Dim ms As New MemoryStream(byteArrayIn)
          Dim returnImage As Image = Image.FromStream(ms)
      Return returnImage
  End Function