FileCommon.cs 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Drawing;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Runtime.Serialization.Formatters.Binary;
  7. using System.Security.Cryptography;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. namespace Ant.Service.Utility
  11. {
  12. public class FileCommon
  13. {
  14. public string ImageToBase64(Image image, System.Drawing.Imaging.ImageFormat format)
  15. {
  16. using (MemoryStream ms = new MemoryStream())
  17. {
  18. // Convert Image to byte[]
  19. image.Save(ms, format);
  20. byte[] imageBytes = ms.ToArray();
  21. // Convert byte[] to Base64 String
  22. string base64String = Convert.ToBase64String(imageBytes);
  23. return base64String;
  24. }
  25. }
  26. //public String ConvertPictureToString(string imagepath)
  27. //{
  28. // /**/
  29. // ////根据图片文件的路径使用文件流打开,并保存为String
  30. // byte[] byData = ConvertPictureToByte(imagepath);
  31. // String SData = Convert.ToBase64String(byData);
  32. // return SData;
  33. //}
  34. /// <summary>
  35. /// 将图片数据转换为Base64字符串
  36. /// </summary>
  37. /// <param name="sender"></param>
  38. /// <param name="e"></param>
  39. public static string ImgToBase64(Image img)
  40. {
  41. BinaryFormatter binFormatter = new BinaryFormatter();
  42. MemoryStream memStream = new MemoryStream();
  43. binFormatter.Serialize(memStream, img);
  44. byte[] bytes = memStream.GetBuffer();
  45. string base64 = Convert.ToBase64String(bytes);
  46. return base64;
  47. }
  48. /// <summary>
  49. /// 将图片转转成base64编码的字符串
  50. /// </summary>
  51. /// <param name="Imagefilename"></param>
  52. /// <returns></returns>
  53. public static string ImgToBase64String(string Imagefilename)
  54. {
  55. try
  56. {
  57. Bitmap bmp = new Bitmap(Imagefilename);
  58. MemoryStream ms = new MemoryStream();
  59. bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
  60. byte[] arr = new byte[ms.Length];
  61. ms.Position = 0;
  62. ms.Read(arr, 0, (int)ms.Length);
  63. ms.Close();
  64. return Convert.ToBase64String(arr);
  65. }
  66. catch (Exception ex)
  67. {
  68. return null;
  69. }
  70. }
  71. //base64编码的字符串转为图片
  72. public static Bitmap Base64StringToImage(string strbase64)
  73. {
  74. try
  75. {
  76. byte[] arr = Convert.FromBase64String(strbase64);
  77. MemoryStream ms = new MemoryStream(arr);
  78. Bitmap bmp = new Bitmap(ms);
  79. //bmp.Save("test.jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
  80. //bmp.Save("test.bmp", ImageFormat.Bmp);
  81. //bmp.Save("test.gif", ImageFormat.Gif);
  82. //bmp.Save("test.png", ImageFormat.Png);
  83. ms.Close();
  84. return bmp;
  85. }
  86. catch (Exception ex)
  87. {
  88. return null;
  89. }
  90. }
  91. /// <summary>
  92. /// 获取图片的宽度和高度
  93. /// </summary>
  94. /// <param name="phyPath"></param>
  95. /// <returns></returns>
  96. public static string GetThumbnail(string phyPath)
  97. {
  98. System.Drawing.Image image = System.Drawing.Image.FromFile(phyPath);
  99. int orignWidth = image.Width; //原图尺寸
  100. int orignHeight = image.Height;
  101. return orignWidth + ";" + orignHeight;
  102. }
  103. /// <summary>
  104. /// 计算文件的 MD5 值
  105. /// </summary>
  106. /// <param name="fileName">要计算 MD5 值的文件名和路径</param>
  107. /// <returns>MD5 值16进制字符串</returns>
  108. public static string MD5File(string fileName)
  109. {
  110. return HashFile(fileName, "md5");
  111. }
  112. /// <summary>
  113. /// 计算文件的 sha1 值
  114. /// </summary>
  115. /// <param name="fileName">要计算 sha1 值的文件名和路径</param>
  116. /// <returns>sha1 值16进制字符串</returns>
  117. public static string SHA1File(string fileName)
  118. {
  119. return HashFile(fileName, "sha1");
  120. }
  121. /// <summary>
  122. /// 获取文件的SHA值
  123. /// </summary>
  124. /// <param name="fileName"></param>
  125. /// <returns></returns>
  126. public static string GetFileSHA(string fileName)
  127. {
  128. string filehash = string.Empty;
  129. try
  130. {
  131. using (FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read))
  132. {
  133. byte[] buffer;
  134. using (HashAlgorithm hash = HashAlgorithm.Create())
  135. {
  136. buffer = hash.ComputeHash(fs);
  137. hash.Clear();
  138. }
  139. filehash = Convert.ToBase64String(buffer);
  140. }
  141. }
  142. catch (Exception ex)
  143. {
  144. }
  145. return filehash;
  146. }
  147. /// <summary>
  148. /// 计算文件的哈希值
  149. /// </summary>
  150. /// <param name="fileName">要计算哈希值的文件名和路径</param>
  151. /// <param name="algName">算法:sha1,md5</param>
  152. /// <returns>哈希值16进制字符串</returns>
  153. private static string HashFile(string fileName, string algName)
  154. {
  155. if (!System.IO.File.Exists(fileName))
  156. return string.Empty;
  157. System.IO.FileStream fs = new System.IO.FileStream(fileName, System.IO.FileMode.Open, System.IO.FileAccess.Read);
  158. byte[] hashBytes = HashData(fs, algName);
  159. fs.Close();
  160. return ByteArrayToHexString(hashBytes);
  161. }
  162. /// <summary>
  163. /// 计算哈希值
  164. /// </summary>
  165. /// <param name="stream">要计算哈希值的 Stream</param>
  166. /// <param name="algName">算法:sha1,md5</param>
  167. /// <returns>哈希值字节数组</returns>
  168. private static byte[] HashData(System.IO.Stream stream, string algName)
  169. {
  170. System.Security.Cryptography.HashAlgorithm algorithm;
  171. if (algName == null)
  172. {
  173. throw new ArgumentNullException("algName 不能为 null");
  174. }
  175. if (string.Compare(algName, "sha1", true) == 0)
  176. {
  177. algorithm = System.Security.Cryptography.SHA1.Create();
  178. }
  179. else
  180. {
  181. if (string.Compare(algName, "md5", true) != 0)
  182. {
  183. throw new Exception("algName 只能使用 sha1 或 md5");
  184. }
  185. algorithm = System.Security.Cryptography.MD5.Create();
  186. }
  187. return algorithm.ComputeHash(stream);
  188. }
  189. /// <summary>
  190. /// 字节数组转换为16进制表示的字符串
  191. /// </summary>
  192. private static string ByteArrayToHexString(byte[] buf)
  193. {
  194. return BitConverter.ToString(buf).Replace("-", "");
  195. }
  196. }
  197. }