11-27-2011، 02:32 PM
کد:
' open a file stream for reading, and load into a memory stream
Public Shared Function StreamToMemory(ByVal path As String) As IO.MemoryStream
Dim input As IO.FileStream
Dim output As IO.MemoryStream
input = New IO.FileStream(path, IO.FileMode.Open, IO.FileAccess.Read, IO.FileShare.Read)
output = StreamToMemory(input)
input.Close()
Return output
End Function
' transfer contents of input stream to memory stream
Public Shared Function StreamToMemory(ByVal input As IO.Stream) As IO.MemoryStream
Dim buffer(1023) As Byte
Dim count As Integer = 1024
Dim output As IO.MemoryStream
' build a new stream
If input.CanSeek Then
output = New IO.MemoryStream(input.Length)
Else
output = New IO.MemoryStream
End If
' iterate stream and transfer to memory stream
Do
count = input.Read(buffer, 0, count)
If count = 0 Then Exit Do
output.Write(buffer, 0, count)
Loop
' rewind stream
output.Position = 0
' pass back
Return output
End Function