Byte[],Stream,Ibuffer,IRandomAccessStream的互相轉換

---
date: Sep 07, 2014
tags:
-
---

今天做8.1程序時徹底弄混了IRandomAcessStream和Stream。。。於是乎,我找了一個可用的不同類型轉換。。。。

Stream 轉IRandomAccessStream

方法一:

1
2
3
4
5
byte[] bytes = StreamToBytes(stream);
InMemoryRandomAccessStream memoryStream = new InMemoryRandomAccessStream();
DataWriter datawriter = new DataWriter(memoryStream.GetOutputStreamAt(0));
datawriter.WriteBytes(bytes);
await datawriter.StoreAsync();

方法二:

1
2
3
var randomAccessStream = new InMemoryRandomAccessStream();
var outputStream = randomAccessStream.GetOutputStreamAt(0);
await RandomAccessStream.CopyAsync(stream.AsInputStream(), outputStream);

IRandomAccessStream 轉 Stream

1
Stream stream=WindowsRuntimeStreamExtensions.AsStreamForRead(randomStream.GetInputStreamAt(0));

Ibuffer轉Stream

1
Stream stream = WindowsRuntimeBufferExtensions.AsStream(buffer);

Stream轉Ibuffer

1
2
3
4
5
6
7
8
9
10
11
12
 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[]

1
byte[] bytes=WindowsRuntimeBufferExtensions.ToArray(buffer,0,(int)buffer.Length);

Byte[]轉Ibuffer

1
WindowsRuntimeBufferExtensions.AsBuffer(bytes,0,bytes.Length);

Ibuffer轉IrandomAccessStream

1
2
3
4
5
InMemoryRandomAccessStream inStream = new InMemoryRandomAccessStream();
DataWriter datawriter = new DataWriter(inStream.GetOutputStreamAt(0));
datawriter.WriteBuffer(buffer,0,buffer.Length);

await datawriter.StoreAsync();

IrandomAccessStream轉Ibuffer

1
2
3
4
5
6
7
8
9
10
11
12
13
Stream stream=WindowsRuntimeStreamExtensions.AsStreamForRead(randomStream.GetInputStreamAt(0));
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);

IRandomAccessStream轉FileInputStream

1
FileInputStream inputStream=randomStream.GetInputStreamAt(0) as FileInputStream;

IRandomAccessStream轉FileOutputStream

1
FileOutputStream outStream= randomStream.GetOutputStreamAt(0) as FileOutputStream;

Stream轉byte[]

1
2
3
4
5
6
7
8
9
10
11
12
13
public static byte[] ConvertStreamTobyte(Stream input)
{
byte[] buffer = new byte[16 * 1024];
using (MemoryStream ms = new MemoryStream())
{
int read;
while ((read = input.Read(buffer, 0, buffer.Length)) > 0)
{
ms.Write(buffer, 0, read);
}
return ms.ToArray();
}
}

Byte轉Stream

1
2
3
4
5
public Stream BytesToStream(byte[] bytes)
{
Stream stream = new MemoryStream(bytes);
return stream;
}

Stream轉MemoryStream

1
2
3
4
5
6
7
8
9
10
11
12
13
14
public static MemoryStream ConvertStreamToMemoryStream(Stream stream)
{
MemoryStream memoryStream = new MemoryStream();
if (stream != null)
{
byte[] buffer = ReadFully(stream);
if (buffer != null)
{
var binaryWriter = new BinaryWriter(memoryStream);
binaryWriter.Write(buffer);
}
}
return memoryStream;
}

IrandomAccessStream轉byte[]

1
2
3
4
Stream stream = WindowsRuntimeStreamExtensions.AsStreamForRead(randomStream.GetInputStreamAt(0));
MemoryStream ms = new MemoryStream();
await stream.CopyToAsync(ms);
byte[] bytes = ms.ToArray();

Navigation

Not the Footnote

The site is proudly powered by Hexo and using a site strcuture from the Hexo theme called Nayo.

The site is powered by the glorious CSS from bettermotherf**kingwebsite.com with some tweaks. Censored the f-word for good.

This site is hosted on Digital Ocean. Deploy your next app in seconds. Get $100 in cloud credits from @DigitalOcean using my link.

Custom Stickers, Die Cut Stickers, Bumper Stickers - Sticker Mule

Possibly the Footnote

Main | Access | RSS | Sitemap
2008 - 2020 | Patrick WU Jinming