将 UnmanagedMemoryStream 转换为字节数组

Posted

技术标签:

【中文标题】将 UnmanagedMemoryStream 转换为字节数组【英文标题】:Convert UnmanagedMemoryStream to Byte-Array 【发布时间】:2013-07-13 05:25:57 【问题描述】:

就这么简单,我如何在 VBNET 中将 UnmanagedMemoryStream 转换为 Byte-Array ?:

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

例外:

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

【问题讨论】:

【参考方案1】:

您可以使用以下方法将System.IO.MemoryStream 直接转换为Byte() 数组:

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

【讨论】:

【参考方案2】:

试试这个方法。我尚未对其进行验证,但它遵循与 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.

【讨论】:

以上是关于将 UnmanagedMemoryStream 转换为字节数组的主要内容,如果未能解决你的问题,请参考以下文章

Python 将中文字母转成数字

将字符串中大写转小写,小写转大写

Java 将图片转二进制再将二进制转成图片

PHP:数组——二维转一维,二维转三维,将特定的数据作为键名

115怎么转码

在线将文字转成语音的方法都有哪些?