/// <summary> /// 壓縮圖片 /// </summary> /// <param name="imageFile"></param> /// <param name="targetSize"></param> /// <returns></returns> public override void ResizeImageFile(byte[] imageFile, int targetSizeWidth, int targetSizeHeight, bool sameRatio, PlatformTask action) { byte[] pic = null; try { using (System.Drawing.Image oldImage = System.Drawing.Image.FromStream(new MemoryStream(imageFile))) { Size newSize; if (sameRatio) { float scale = GameTools.AutoSetScale(oldImage.Width, oldImage.Height, targetSizeWidth, targetSizeHeight); newSize = new Size(Convert.ToInt32(oldImage.Width * scale), Convert.ToInt32(oldImage.Height * scale)); } else { newSize = new Size(Convert.ToInt32(targetSizeWidth), Convert.ToInt32(targetSizeHeight)); } using (Bitmap newImage = new Bitmap(newSize.Width, newSize.Height, PixelFormat.Format24bppRgb)) { using (Graphics canvas = Graphics.FromImage(newImage)) { canvas.SmoothingMode = SmoothingMode.AntiAlias; canvas.InterpolationMode = InterpolationMode.HighQualityBicubic; canvas.PixelOffsetMode = PixelOffsetMode.HighQuality; canvas.DrawImage(oldImage, new RectangleF(new PointF(0, 0), newSize)); MemoryStream m = new MemoryStream(); newImage.Save(m, ImageFormat.Jpeg); pic = m.GetBuffer(); } } } } catch (Exception ex) { WebTools.TakeWarnMsg("缩放图像失败:" + ex.Message.NullToString(), "ResizeImageFile", ex); } if (action != null) { action.ParamArrayResultBytes = pic; action.Start(); } }