2013-07-13 68 views
2

就這麼簡單,我怎麼能在VBNET轉換一個UnmanagedMemoryStream字節數組轉換UnmanagedMemoryStream到字節數組

Dim bytes() As Byte = My.Resources.AudioFile 

例外:

Value of type 'System.IO.UnmanagedMemoryStream' cannot be converted to '1-dimensional array of Byte'. 

回答

5

您可以直接轉換System.IO.MemoryStreamByte()陣列,通過使用:

Dim myMemStream As New System.IO.MemoryStream 
My.Resources.AudioFile.CopyTo(myMemStream) 
Dim myBytes() As Byte = myMemStream.ToArray 
1

試試這種方法。我還沒有驗證它,但它遵循類似於MSDN的文章,並進行了一些修改。 http://msdn.microsoft.com/en-us/library/system.io.unmanagedmemorystream.aspx

Dim audioBytes() as Byte 
Dim audioStreamReader As System.IO.UnmanagedMemoryStream = CType(My.Resources.AudioFile, System.IO.UnmanagedMemoryStream) 
Dim length As Long = audioStreamReader.Length 
audioStreamReader.Position = 0 
audioStreamReader.Read(bytes, 0, length); 
'At this point, audioBytes contains the data.