You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

78 lines
2.4 KiB

1 month ago
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Drawing;
  4. using System.Drawing.Imaging;
  5. using System.IO;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. namespace ShenTun.ImageCollection.Common
  10. {
  11. public class ImageHelper
  12. {
  13. public static string ConvertImageToBase64(Image file)
  14. {
  15. using (MemoryStream memoryStream = new MemoryStream())
  16. {
  17. file.Save(memoryStream, file.RawFormat);
  18. byte[] imageBytes = memoryStream.ToArray();
  19. return Convert.ToBase64String(imageBytes);
  20. }
  21. }
  22. public static Image ConvertBase64ToImage(string base64String)
  23. {
  24. byte[] imageBytes = Convert.FromBase64String(base64String);
  25. using (MemoryStream ms = new MemoryStream(imageBytes, 0, imageBytes.Length))
  26. {
  27. ms.Write(imageBytes, 0, imageBytes.Length);
  28. return Image.FromStream(ms, true);
  29. }
  30. }
  31. public static Image GetImage(byte[] str)
  32. {
  33. MemoryStream ms = new MemoryStream(str);
  34. Image img = System.Drawing.Image.FromStream(ms);
  35. return img;
  36. }
  37. public static Image GetImage(MemoryStream mstr)
  38. {
  39. MemoryStream ms = mstr;
  40. Image img = System.Drawing.Image.FromStream(ms);
  41. return img;
  42. }
  43. public static ImageCodecInfo GetEncoder(ImageFormat format)
  44. {
  45. ImageCodecInfo[] codecs = ImageCodecInfo.GetImageDecoders();
  46. foreach (ImageCodecInfo codec in codecs)
  47. {
  48. if (codec.FormatID == format.Guid)
  49. {
  50. return codec;
  51. }
  52. }
  53. return null;
  54. }
  55. public static string GetLoaclPath(string path,string dirName)
  56. {
  57. DateTime dt = DateTime.Now;
  58. string year = dt.Year.ToString();
  59. string month = dt.Month.ToString();
  60. string day = dt.Day.ToString();
  61. string file_path = path + "\\" + year + "\\" + month + "\\" + day + "\\" + dirName;
  62. //如果日期的文件夹不存在
  63. if (!Directory.Exists(file_path))
  64. {
  65. //创建一个日期的文件夹
  66. Directory.CreateDirectory(file_path);
  67. }
  68. return file_path;
  69. }
  70. }
  71. }