/// <summary>
/// 将 Stream 转成 byte[]
/// </summary>
public static byte[] StreamToBytes(Stream stream)
{
byte[] bytes = new byte[stream.Length];
stream.Read(bytes, 0, bytes.Length);
// 设置当前流的位置为流的开始
stream.Seek(0, SeekOrigin.Begin);
return bytes;
}
/// <summary>
/// 将 byte[] 转成 Stream
/// </summary>
public static Stream BytesToStream(byte[] bytes)
{
Stream stream = new MemoryStream(bytes);
return stream;
}
/// <summary>
/// 文件保存
/// </summary>
/// <param name="datas">文件数据</param>
/// <param name="imgPath">文件保存路径</param>
/// <returns></returns>
public static bool ImageSave(byte[]? datas, string? imgPath)
{
if (datas == null)
{
return false;
}
using (FileStream fileStream = new FileStream(imgPath, FileMode.Create))
{
fileStream.Write(datas, 0, datas.Length);
}
return true;
}
c# stream和byte互换
于 2023-02-03 16:16:35 首次发布