public static byte[] CompressImageNotChange(string sourceFileFullName, long maxLength, out bool hasCompress) { hasCompress = false; byte[] imageToByteArrayByFile = ImageCompress.GetImageToByteArrayByFile(sourceFileFullName); if (imageToByteArrayByFile == null || (long)imageToByteArrayByFile.Length <= maxLength) { return(imageToByteArrayByFile); } hasCompress = true; return(ImageCompress.CompressImageNotChange(imageToByteArrayByFile, maxLength, ref sourceFileFullName)); }
public static byte[] CompressImage(string sourceFileFullName, long maxLength, out bool hasCompress) { hasCompress = false; byte[] array = ImageCompress.GetImageToByteArrayByFile(sourceFileFullName); if (array == null || (long)array.Length <= maxLength) { return(array); } System.Drawing.Image image = null; string text = string.Empty; if (!string.IsNullOrEmpty(sourceFileFullName) && sourceFileFullName.LastIndexOf(".") > 0) { text = sourceFileFullName.Substring(sourceFileFullName.LastIndexOf(".")); } if (string.IsNullOrEmpty(text)) { text = ".jpeg"; } FileInfo fileInfo = new FileInfo(sourceFileFullName); string text2 = fileInfo.DirectoryName; if (!text2.EndsWith("\\")) { text2 += "\\"; } if (!Directory.Exists(text2)) { Directory.CreateDirectory(text2); } System.Drawing.Imaging.ImageFormat formatByExtension = ImageCompress.GetFormatByExtension(ref text); string text3 = string.Concat(new object[] { text2, "Compress", DateTime.Now.Ticks, text }); while (File.Exists(text3)) { text3 = string.Concat(new object[] { text2, "Compress", DateTime.Now.Ticks, text }); } bool flag = false; do { try { using (MemoryStream memoryStream = new MemoryStream(array)) { image = System.Drawing.Image.FromStream(memoryStream); if (image == null) { break; } double towidth; double toheight; if (!flag) { towidth = (double)image.Width; toheight = (double)image.Height; } else { if (image.Width > 800) { towidth = 800.0; toheight = (double)image.Height * 800.0 / (double)image.Width; } else { towidth = (double)image.Width * 0.9; toheight = (double)image.Height * 0.9; } } array = ImageCompress.MakeThumbnail(image, text3, towidth, toheight, formatByExtension); flag = true; } } catch (Exception ex) { Log.WriteLog(ex); image.Dispose(); break; } finally { if (image != null) { image.Dispose(); } } }while ((long)array.Length >= maxLength); hasCompress = true; return(array); }