How to get a stream from a byte array in a windows8 store app

前端 未结 3 934
南旧
南旧 2021-01-16 01:02

I have been trying get a stream from a byte array in metro style app using the following code.

InMemoryRandomAccessStream memoryStream = new InMemoryRandomAc         


        
3条回答
  •  天涯浪人
    2021-01-16 01:55

    You can also use the BinaryWriter/BinaryReader to read and write from and to byte[] and Streams.

        private Stream ConvertToStream(byte[] raw)
        {
            Stream streamOutput = new MemoryStream();
            using (BinaryWriter writer = new BinaryWriter(streamOutput))
            {
                writer.Write(raw);
            }
            return streamOutput;
        }
    

    Another option is to use built in extension methods as Marc Gravell already mentioned:

        private Stream ConvertToStream(byte[] raw)
        {
            return raw.AsBuffer().AsStream();
        }
    

    The extension methods are commented with [Security Critical] which may indicate a later change. However, after looking around a bit I couldn't find any additional information on the security code comment.

提交回复
热议问题