internal static Image ResizeImage(Image image, int width, int height, ThumbnailMakerBorder border) { var nw = (float)image.Width; var nh = (float)image.Height; if (nw > width) { nh = width * nh / nw; nw = width; } if (nh > height) { nw = height * nw / nh; nh = height; } var result = new Bitmap( border == ThumbnailMakerBorder.Bordered ? width : (int)nw, border == ThumbnailMakerBorder.Bordered ? height : (int)nh ); try { try { result.SetResolution(image.HorizontalResolution, image.VerticalResolution); } catch (Exception ex) { LogManager.GetLogger(typeof(ThumbnailMaker)).Debug("Failed to set resolution", ex); } using (var graphics = Graphics.FromImage(result)) { if (result.Width > image.Width && result.Height > image.Height) { graphics.CompositingQuality = Drawing2D.CompositingQuality.HighQuality; graphics.InterpolationMode = Drawing2D.InterpolationMode.High; } else { graphics.CompositingQuality = Drawing2D.CompositingQuality.HighSpeed; graphics.InterpolationMode = Drawing2D.InterpolationMode.Bicubic; } var rect = new Rectangle( (int)(result.Width - nw) / 2, (int)(result.Height - nh) / 2, (int)nw, (int)nh ); graphics.SmoothingMode = Drawing2D.SmoothingMode.HighSpeed; graphics.FillRectangle( Brushes.Black, new Rectangle(0, 0, result.Width, result.Height)); graphics.DrawImage(image, rect); } return(result); } catch (Exception) { result.Dispose(); throw; } }
internal static Image ResizeImage(Image image, int width, int height, ThumbnailMakerBorder border) { var nw = (float)image.Width; var nh = (float)image.Height; if (nw > width) { nh = width * nh / nw; nw = width; } if (nh > height) { nw = height * nw / nh; nh = height; } var result = new Bitmap( border == ThumbnailMakerBorder.Bordered ? width : (int)nw, border == ThumbnailMakerBorder.Bordered ? height : (int)nh ); try { try { result.SetResolution(image.HorizontalResolution, image.VerticalResolution); } catch (Exception ex) { _logger.Debug("Failed to set resolution", ex); } using (var graphics = Graphics.FromImage(result)) { if (result.Width > image.Width && result.Height > image.Height) { graphics.CompositingQuality = Drawing2D.CompositingQuality.HighQuality; graphics.InterpolationMode = Drawing2D.InterpolationMode.High; } else { graphics.CompositingQuality = Drawing2D.CompositingQuality.HighSpeed; graphics.InterpolationMode = Drawing2D.InterpolationMode.Bicubic; } var rect = new Rectangle( (int)(result.Width - nw) / 2, (int)(result.Height - nh) / 2, (int)nw, (int)nh ); graphics.SmoothingMode = Drawing2D.SmoothingMode.HighSpeed; graphics.FillRectangle( Brushes.Black, new Rectangle(0, 0, result.Width, result.Height)); graphics.DrawImage(image, rect); } return result; } catch (Exception) { result.Dispose(); throw; } }