StreamExtensions.cs 909 B

12345678910111213141516171819202122232425262728293031323334
  1. using System.IO;
  2. namespace Masuit.Tools
  3. {
  4. public static class StreamExtensions
  5. {
  6. /// <summary>
  7. /// 将流转换为内存流
  8. /// </summary>
  9. /// <param name="stream"></param>
  10. /// <returns></returns>
  11. public static MemoryStream SaveAsMemoryStream(this Stream stream)
  12. {
  13. stream.Position = 0;
  14. return new MemoryStream(stream.ToArray());
  15. }
  16. /// <summary>
  17. ///
  18. /// </summary>
  19. /// <param name="stream"></param>
  20. /// <returns></returns>
  21. public static byte[] ToArray(this Stream stream)
  22. {
  23. stream.Position = 0;
  24. byte[] bytes = new byte[stream.Length];
  25. stream.Read(bytes, 0, bytes.Length);
  26. // 设置当前流的位置为流的开始
  27. stream.Seek(0, SeekOrigin.Begin);
  28. return bytes;
  29. }
  30. }
  31. }