// Default parameters are almost the highest quality setting. public static Bitmap Resize(this Image src, int w, int h, SmoothingMode smoothingMode = SmoothingMode.AntiAlias, InterpolationMode interpolationMode = InterpolationMode.HighQualityBicubic, PixelOffsetMode pixelOffsetMode = PixelOffsetMode.HighQuality) { var newImage = new Bitmap(w, h); using (var gr = Graphics.FromImage(newImage)) { gr.SmoothingMode = smoothingMode; gr.InterpolationMode = interpolationMode; gr.PixelOffsetMode = pixelOffsetMode; gr.DrawImage(src, 0, 0, w, h); } return newImage; }
// resize image based on given percentage public static Bitmap ResizedByPercentage(this Image src, double percent) { var rW = (int)Math.Round(src.Width * percent, 0); var rH = (int)Math.Round(src.Height * percent, 0); return src.Resize(rW, rH); } // resize image limited in given w, h parameter public static Bitmap ResizeImageIn(this Image src, int w, int h) { var rW = (double)w / src.Width; var rH = (double)h / src.Height; var rWIsSmall = rW < rH; return src.Resize(rWIsSmall ? w : (int)(src.Width * rH), rWIsSmall ? (int)(src.Height * rW) : h); }
コメント