ImFileHelper.cs 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. using System;
  2. using System.Drawing;
  3. using System.IO;
  4. namespace GxPress.Common.Tools
  5. {
  6. public static class ImFileHelper
  7. {
  8. /// <summary>
  9. /// 得到图片的 Size 值
  10. /// </summary>
  11. /// <param name="bytes">图片的二进制数据</param>
  12. /// <returns>图片的 Size 值</returns>
  13. public static Size GetSize(byte[] bytes)
  14. {
  15. var size = Size.Empty;
  16. if (bytes != null && bytes.Length > 0)
  17. {
  18. using var ms = new MemoryStream(bytes);
  19. size = GetSize(ms);
  20. }
  21. return size;
  22. }
  23. /// <summary>
  24. /// 得到图片的 Size 值
  25. /// </summary>
  26. /// <param name="stream">图片的文件数据流</param>
  27. /// <returns>图片的 Size 值</returns>
  28. public static Size GetSize(Stream stream)
  29. {
  30. Size size;
  31. using (var originalImage = Image.FromStream(stream))
  32. {
  33. size = originalImage.Size;
  34. }
  35. return size;
  36. }
  37. /// <summary>
  38. /// 保存文件
  39. /// </summary>
  40. /// <param name="path">文件保存路径</param>
  41. /// <param name="bytes">文件二进制数据</param>
  42. /// <returns>保存文件是否成功!</returns>
  43. public static void Save(string path, byte[] bytes)
  44. {
  45. if (string.IsNullOrWhiteSpace(path))
  46. throw new ArgumentException("文件保存路径不能为空");
  47. if (bytes == null || bytes.Length == 0)
  48. throw new ArgumentException("文件二进制数据不能为空");
  49. var info = new FileInfo(path);
  50. if (info.Directory != null && !info.Directory.Exists) info.Directory.Create();
  51. info.Delete();
  52. File.WriteAllBytes(info.FullName, bytes);
  53. }
  54. }
  55. }