//String 转 Buffer private IBuffer GetBufferFromString(String str) { using (InMemoryRandomAccessStream memoryStream = new InMemoryRandomAccessStream()) { using (DataWriter dataWriter = new DataWriter(memoryStream)) { dataWriter.WriteString(str); return dataWriter.DetachBuffer(); } } } //Buffer 转 String private String GetStringFromBuffer(IBuffer buffer) { using (DataReader dataReader = DataReader.FromBuffer(buffer)) { return dataReader.ReadString(buffer.Length); } } //Stream 转 IInputStream IInputStream inputStream = stream.AsInputStream(); //IInputStream 转 Stream stream = inputStream.AsStreamForRead(); //Stream 转 IOutputStream IOutputStream outputStream = stream.AsOutputStream(); //IOutputStream 转 Stream stream = outputStream.AsStreamForRead(); //Stream 转 IRandomAccessStream IBuffer buffer = null; var inputstream = stream.AsInputStream(); using (var dataReader = new DataReader(inputstream)) { await dataReader.LoadAsync((uint)stream.Length); buffer = dataReader.DetachBuffer(); } var iRandomAccessStream = new InMemoryRandomAccessStream(); await iRandomAccessStream.WriteAsync(buffer); // IRandomAccessStream 转 Stream stream = iRandomAccessStream.AsStream(); //Ibuffer转byte[] byte[] bytes = WindowsRuntimeBufferExtensions.ToArray(buffer,0,(int)buffer.Length); //Byte[]转Ibuffer IBuffer buffer = WindowsRuntimeBufferExtensions.AsBuffer(bytes,0,bytes.Length); //Ibuffer转Stream Stream stream = WindowsRuntimeBufferExtensions.AsStream(buffer); //Stream转Ibuffer MemoryStream memoryStream = new MemoryStream(); if (stream != null) { byte[] bytes = ReadFully(stream); if (bytes != null) { var binaryWriter = new BinaryWriter(memoryStream); binaryWriter.Write(bytes); } } IBuffer buffer=WindowsRuntimeBufferExtensions.GetWindowsRuntimeBuffer(memoryStream,0,(int)memoryStream.Length); //Ibuffer转byte[] byte[] bytes=WindowsRuntimeBufferExtensions.ToArray(buffer,0,(int)buffer.Length); //Byte[]转Ibuffer WindowsRuntimeBufferExtensions.AsBuffer(bytes,0,bytes.Length); //Ibuffer转IrandomAccessStream InMemoryRandomAccessStream inStream = new InMemoryRandomAccessStream(); DataWriter datawriter = new DataWriter(inStream.GetOutputStreamAt(0)); datawriter.WriteBuffer(buffer,0,buffer.Length); await datawriter.StoreAsync(); //IrandomAccessStream转Ibuffer Stream stream=WindowsRuntimeStreamExtensions.AsStreamForRead(randomStream.GetInputStreamAt(0)); MemoryStream memoryStream = new MemoryStream(); if (stream != null) { byte[] bytes = await ConvertStreamTobyte(stream); if (bytes != null) { var binaryWriter = new BinaryWriter(memoryStream); binaryWriter.Write(bytes); } } IBuffer buffer=WindowsRuntimeBufferExtensions.GetWindowsRuntimeBuffer(memoryStream,0,(int)memoryStream.Length); //Stream转byte[] public async Task<byte[]> ConvertStreamTobyte(Stream stream) { MemoryStream ms = new MemoryStream(); await stream.CopyToAsync(ms); return ms.ToArray(); } //Byte转Stream public Stream BytesToStream(byte[] bytes) { Stream stream = new MemoryStream(bytes); return stream; } //Byte[] -> BitmapImage public async Task<BitmapImage> ByteArrayToImageAsync(byte[] pixeByte) { using (InMemoryRandomAccessStream stream = new InMemoryRandomAccessStream()) { stream.Seek(0); IBuffer buffer = WindowsRuntimeBufferExtensions.AsBuffer(pixeByte, 0, pixeByte.Length); await stream.WriteAsync(buffer); BitmapImage image = new BitmapImage(); image.SetSource(stream); return image; } }