using System; using System.Drawing; using System.Drawing.Drawing2D; using System.Drawing.Imaging; using System.IO; namespace ManagerService.Helpers { public static class ImageHelper { public static Size ResizeKeepAspect(this Size src, int maxWidth, int maxHeight, bool enlarge = false) { maxWidth = enlarge ? maxWidth : Math.Min(maxWidth, src.Width); maxHeight = enlarge ? maxHeight : Math.Min(maxHeight, src.Height); decimal rnd = Math.Min(maxWidth / (decimal)src.Width, maxHeight / (decimal)src.Height); return new Size((int)Math.Round(src.Width * rnd), (int)Math.Round(src.Height * rnd)); } // TO Byte[] or Image type public static dynamic ResizeImage(Image image, /* note changed names */ int canvasWidth, int canvasHeight, /* new */ int originalWidth, int originalHeight, bool isImage = false) { byte[] convertedToBytes; System.Drawing.Image thumbnail = new Bitmap(canvasWidth, canvasHeight); // changed parm names System.Drawing.Graphics graphic = System.Drawing.Graphics.FromImage(thumbnail); graphic.InterpolationMode = InterpolationMode.HighQualityBicubic; graphic.SmoothingMode = SmoothingMode.HighQuality; graphic.PixelOffsetMode = PixelOffsetMode.HighQuality; graphic.CompositingQuality = CompositingQuality.HighQuality; /* ------------------ new code --------------- */ // Figure out the ratio double ratioX = (double)canvasWidth / (double)originalWidth; double ratioY = (double)canvasHeight / (double)originalHeight; // use whichever multiplier is smaller double ratio = ratioX < ratioY ? ratioX : ratioY; // now we can get the new height and width int newHeight = Convert.ToInt32(originalHeight * ratio); int newWidth = Convert.ToInt32(originalWidth * ratio); // Now calculate the X,Y position of the upper-left corner // (one of these will always be zero) int posX = Convert.ToInt32((canvasWidth - (originalWidth * ratio)) / 2); int posY = Convert.ToInt32((canvasHeight - (originalHeight * ratio)) / 2); graphic.Clear(Color.White); // white padding graphic.DrawImage(image, posX, posY, newWidth, newHeight); /* ------------- end new code ---------------- */ System.Drawing.Imaging.ImageCodecInfo[] info = ImageCodecInfo.GetImageEncoders(); EncoderParameters encoderParameters; encoderParameters = new EncoderParameters(1); encoderParameters.Param[0] = new EncoderParameter(Encoder.Quality, 100L); if (isImage) { return thumbnail; } using (MemoryStream updatedImageMemorySteam = new MemoryStream()) { thumbnail.Save(updatedImageMemorySteam, System.Drawing.Imaging.ImageFormat.Jpeg); convertedToBytes = updatedImageMemorySteam.ToArray(); } return convertedToBytes; } public static byte[] ResizeAndAddWatermark(Byte[] bytes, bool watermark, int maxWidth, int maxHeight) { byte[] convertedToBytes; using (MemoryStream originalImageMemoryStream = new MemoryStream(bytes)) { using (Image image = Image.FromStream(originalImageMemoryStream)) { Image currentImage = image; if (image.Width > maxWidth || image.Height > maxHeight) { Size newSize = ResizeKeepAspect(image.Size, maxWidth, maxHeight); Image resizedImage = ImageHelper.ResizeImage(image, newSize.Width, newSize.Height, image.Width, image.Height, true); currentImage = resizedImage; } if(watermark) { Font font = new Font("Arial", 25, FontStyle.Italic, GraphicsUnit.Pixel); Color color = Color.DarkBlue; Point point = new Point(currentImage.Width /2, (int)Math.Round(currentImage.Height - currentImage.Height * 0.1)); SolidBrush brush = new SolidBrush(color); using (Graphics graphics = Graphics.FromImage(currentImage)) { StringFormat stringFormat = new StringFormat(); stringFormat.Alignment = StringAlignment.Center; stringFormat.LineAlignment = StringAlignment.Center; graphics.DrawString("fortsaintheribert.be", font, brush, point, stringFormat); } } using (MemoryStream updatedImageMemorySteam = new MemoryStream()) { currentImage.Save(updatedImageMemorySteam, System.Drawing.Imaging.ImageFormat.Jpeg); convertedToBytes = updatedImageMemorySteam.ToArray(); } } } return convertedToBytes; } } }