C# Stream 和 byte[] 之间的转换

来源:互联网 发布:武汉cnc编程培训 编辑:程序博客网 时间:2024/05/26 12:04

C# Stream 和 byte[] 之间的转换


/// 将 Stream 转成 byte[]

public byte[] StreamToBytes(Stream stream)
{
    byte[] bytes = new byte[stream.Length];
    stream.Read(bytes, 0, bytes.Length);


    // 设置当前流的位置为流的开始
    stream.Seek(0, SeekOrigin.Begin);
    return bytes;
}


/// 将 byte[] 转成 Stream

public Stream BytesToStream(byte[] bytes)
{
    Stream stream = new MemoryStream(bytes);
    return stream;
}

原创粉丝点击