/// <summary> /// 이미지 사이즈 변경 /// </summary> /// <param name="newWidth">가로</param> /// <param name="newHeight">세로</param> /// <param name="path">이미지 경로</param> /// <returns></returns> public static Image ResizeImage(int newWidth, int newHeight, string path, ImageResolutionOption option = ImageResolutionOption.Nomal, AutoBackground backgroundOption = AutoBackground.None) { if (path.Contains("http")) { return(ResizeImage(newWidth, newHeight, getImageFromURL(path), option, backgroundOption)); } else { return(ResizeImage(newWidth, newHeight, new Bitmap(path), option, backgroundOption)); } }
/// <summary> /// 이미지 사이즈 변경 /// </summary> /// <param name="newWidth">가로</param> /// <param name="newHeight">세로</param> /// <param name="image">이미지</param> /// <returns></returns> public static Image ResizeImage(int newWidth, int newHeight, Image image, ImageResolutionOption option = ImageResolutionOption.Nomal, AutoBackground backgroundOption = AutoBackground.None) { var ratioX = (double)newWidth / image.Width; var ratioY = (double)newHeight / image.Height; var ratio = Math.Min(ratioX, ratioY); var maxWidth = (int)(image.Width * ratio); var maxHeight = (int)(image.Height * ratio); var newImage = new Bitmap(newWidth, newWidth); using (var graphics = Graphics.FromImage(newImage)) { switch (option) { case ImageResolutionOption.HighQuality: graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic; graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; graphics.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.HighQuality; graphics.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality; break; case ImageResolutionOption.High: graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High; graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; graphics.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.HighQuality; graphics.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality; break; case ImageResolutionOption.Nomal: graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.Default; break; case ImageResolutionOption.Row: graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.Low; break; case ImageResolutionOption.PreferCapacity: graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.Low; graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighSpeed; graphics.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.HighSpeed; graphics.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighSpeed; break; } switch (backgroundOption) { case AutoBackground.None: break; case AutoBackground.BlackFill: graphics.Clear(ColorTranslator.FromHtml("#000000")); break; case AutoBackground.WhiteFill: graphics.Clear(ColorTranslator.FromHtml("#FFFFFF")); break; case AutoBackground.TransparencyFill: graphics.Clear(Color.Transparent); break; case AutoBackground.AutoFill: string backgroundColor = PixelColorFrequency(image).Keys.First(); graphics.Clear(ColorTranslator.FromHtml(backgroundColor.Equals('#') ? backgroundColor : "#" + backgroundColor)); break; } // Calculate x and y which center the image int y = (newHeight / 2) - maxHeight / 2; int x = (newWidth / 2) - maxWidth / 2; // Draw image on x and y with newWidth and newHeight graphics.DrawImage(image, x, y, maxWidth, maxHeight); } return(newImage); }