当前位置:   article > 正文

C# 上传到azure 容器_c# 将文件上传到azure blob 中

c# 将文件上传到azure blob 中
  1. public class ImageHelper
  2. {
  3. public static string SaveImage(string base64String,string imageDir)
  4. {
  5. string picPath = "";
  6. try
  7. {
  8. byte[] bytes = System.Convert.FromBase64String(base64String);//base64转为byte数组
  9. MemoryStream ms = new MemoryStream(bytes);//创建内存流,将图片编码导入
  10. Image image = Image.FromStream(ms);
  11. Random ran = new Random((int)DateTime.Now.Ticks);
  12. string fileName = DateTime.Now.ToString("yyyyMMddhhmmssms") + ran.Next() + ".jpeg";
  13. //文件保存位置及命名,精确到毫秒并附带一组随机数,防止文件重名,数据库保存路径为此变量
  14. string serverPath = imageDir + "\\" + fileName;
  15. string fullPath = (System.AppDomain.CurrentDomain.BaseDirectory + serverPath);
  16. ImageFormat imgfor = ImageFormat.Jpeg;//设置图片格式
  17. image.Save(fullPath, imgfor); //保存图片
  18. ms.Close();
  19. ms.Dispose();
  20. image.Dispose();
  21. picPath = imageDir + "/" + fileName;
  22. }
  23. catch (Exception err)
  24. {
  25. }
  26. return picPath;
  27. }
  28. public static byte[] GetImageAsByteArray(string imageFilePath)
  29. {
  30. using (FileStream fileStream = new FileStream(imageFilePath, FileMode.Open, FileAccess.Read))
  31. {
  32. using (BinaryReader binaryReader = new BinaryReader(fileStream))
  33. {
  34. return binaryReader.ReadBytes((int)fileStream.Length);
  35. }
  36. }
  37. }
  38. public static string GetBase64String(byte[] data)
  39. {
  40. return Convert.ToBase64String(data);
  41. }
  42. /// <summary>
  43. /// 无损压缩图片
  44. /// </summary>
  45. /// <param name="sFile">原图片地址</param>
  46. /// <param name="dFile">压缩后保存图片地址</param>
  47. /// <param name="flag">压缩质量(数字越小压缩率越高)1-100</param>
  48. /// <param name="size">压缩后图片的最大大小</param>
  49. /// <param name="sfsc">是否是第一次调用</param>
  50. /// <returns></returns>
  51. public static bool CompressImage(string sFile, string dFile, int flag = 97, int size = 500, bool sfsc = true)
  52. {
  53. //如果是第一次调用,原始图像的大小小于要压缩的大小,则直接复制文件,并且返回true
  54. FileInfo firstFileInfo = new FileInfo(sFile);
  55. if (sfsc == true && firstFileInfo.Length < size * 1024)
  56. {
  57. firstFileInfo.CopyTo(dFile);
  58. return true;
  59. }
  60. Image iSource = Image.FromFile(sFile);
  61. ImageFormat tFormat = iSource.RawFormat;
  62. int dHeight, dWidth;
  63. if(iSource.Height > 1500 || iSource.Width > 1500)
  64. {
  65. dHeight = iSource.Height * 2 / 3;
  66. dWidth = iSource.Width * 2 / 3;
  67. }
  68. else
  69. {
  70. dHeight = iSource.Height;
  71. dWidth = iSource.Width;
  72. }
  73. int sW = 0, sH = 0;
  74. //按比例缩放
  75. Size tem_size = new Size(iSource.Width, iSource.Height);
  76. if (tem_size.Width > dHeight || tem_size.Width > dWidth)
  77. {
  78. if ((tem_size.Width * dHeight) > (tem_size.Width * dWidth))
  79. {
  80. sW = dWidth;
  81. sH = (dWidth * tem_size.Height) / tem_size.Width;
  82. }
  83. else
  84. {
  85. sH = dHeight;
  86. sW = (tem_size.Width * dHeight) / tem_size.Height;
  87. }
  88. }
  89. else
  90. {
  91. sW = tem_size.Width;
  92. sH = tem_size.Height;
  93. }
  94. Bitmap ob = new Bitmap(dWidth, dHeight);
  95. Graphics g = Graphics.FromImage(ob);
  96. g.Clear(Color.WhiteSmoke);
  97. g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
  98. g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
  99. g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
  100. g.DrawImage(iSource, new Rectangle((dWidth - sW) / 2, (dHeight - sH) / 2, sW, sH), 0, 0, iSource.Width, iSource.Height, GraphicsUnit.Pixel);
  101. g.Dispose();
  102. //以下代码为保存图片时,设置压缩质量
  103. EncoderParameters ep = new EncoderParameters();
  104. long[] qy = new long[1];
  105. qy[0] = flag;//设置压缩的比例1-100
  106. EncoderParameter eParam = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, qy);
  107. ep.Param[0] = eParam;
  108. try
  109. {
  110. ImageCodecInfo[] arrayICI = ImageCodecInfo.GetImageEncoders();
  111. ImageCodecInfo jpegICIinfo = null;
  112. for (int x = 0; x < arrayICI.Length; x++)
  113. {
  114. if (arrayICI[x].FormatDescription.Equals("JPEG"))
  115. {
  116. jpegICIinfo = arrayICI[x];
  117. break;
  118. }
  119. }
  120. if (jpegICIinfo != null)
  121. {
  122. ob.Save(dFile, jpegICIinfo, ep);//dFile是压缩后的新路径
  123. FileInfo fi = new FileInfo(dFile);
  124. if (fi.Length > 1024 * size)
  125. {
  126. flag = flag - 10;
  127. CompressImage(sFile, dFile, flag, size, false);
  128. }
  129. }
  130. else
  131. {
  132. ob.Save(dFile, tFormat);
  133. }
  134. return true;
  135. }
  136. catch
  137. {
  138. return false;
  139. }
  140. finally
  141. {
  142. iSource.Dispose();
  143. ob.Dispose();
  144. }
  145. }
  146. /// <summary>
  147. /// 根据路径删除文件
  148. /// </summary>
  149. /// <param name="path"></param>
  150. public static void DeleteFile(string path)
  151. {
  152. FileAttributes attr = File.GetAttributes(path);
  153. if (attr == FileAttributes.Directory)
  154. {
  155. Directory.Delete(path, true);
  156. }
  157. else
  158. {
  159. File.Delete(path);
  160. }
  161. }
  162. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/从前慢现在也慢/article/detail/138194
推荐阅读
相关标签
  

闽ICP备14008679号