public static string ResizeImage(string originImage, ImageResizeOptions options2) { string filename = string.Format("{0}_{1}x{2}{3}", new object[] { Path.GetFileNameWithoutExtension(originImage), options2.Width, options2.Height, Path.GetExtension(originImage) }); string str = Path.Combine(Path.GetDirectoryName(originImage), filename); ResizeImage(originImage, str, options2); CompressImage(str); File.Delete(originImage); return(filename); }
private static void ResizeImage(string originImage, string tatget, ImageResizeOptions opt) { try { lock (objectLock) { using (Image image = Image.FromFile(originImage)) { if (!opt.cut) { if (opt.Width > image.Width) { opt.Width = image.Width; opt.Height = image.Height; } else { opt.Height = (int)Math.Round(opt.Width * (Convert.ToDecimal(image.Height) / Convert.ToDecimal(image.Width))); } } else { decimal num51 = Convert.ToDecimal(opt.Height) / opt.Width; if (opt.Width > image.Width) { opt.Width = image.Width; opt.Height = Convert.ToInt32(opt.Width * num51); } } int x = 0; int y = 0; int width = 0; int height = 0; decimal num5 = opt.Width / Convert.ToDecimal(opt.Height); decimal num6 = image.Width / Convert.ToDecimal(image.Height); if (num6 > num5) { x = (int)Math.Round((decimal)((Convert.ToDecimal(image.Width) - (num5 * image.Height)) / 2M)); width = (int)Math.Round((decimal)(num5 * image.Height)); height = image.Height; } else if (num6 < num5) { y = (int)Math.Round((decimal)((Convert.ToDecimal(image.Height) - Convert.ToDecimal((decimal)(image.Width / num5))) / 2M)); width = image.Width; height = (int)Math.Round((decimal)(image.Width / num5)); } else if (num6 == num5) { width = image.Width; height = image.Height; } if (image.Width < (x + width)) { width = image.Width - x; } if (image.Height < (y + height)) { height = image.Height - y; } RectangleF cloneRect = new RectangleF(x, y, width, height); Rectangle rect = new Rectangle(x, y, width, height); using (Bitmap bitmap = new Bitmap(image)) { using (Bitmap bitmap2 = bitmap.Clone(cloneRect, bitmap.PixelFormat)) { using (Bitmap bitmap3 = KiResizeImage(bitmap2, opt.Width, opt.Height)) { int num7 = 0; Label_01C0 :; try { bitmap3.Save(tatget); } catch (Exception exception) { if ((num7 >= 10) || (exception.Message.IndexOf("GDI+ 中发生一般性错误。") <= -1)) { throw; } num7++; goto Label_01C0; } } } } } } } catch { } }