Image - Scale
An Image extension method that scales an image to the specific ratio.
public static void Main() { var bitmap = new Bitmap(2, 2); //C# Extension Method: Image - Scale Image value1 = bitmap.Scale(2); Image value2 = bitmap.Scale(3,5); Console.WriteLine("Height: {0}, Width: {1}", value1.Height, value1.Width); Console.WriteLine("Height: {0}, Width: {1}", value2.Height, value2.Width); }
View Source
using System; #if !NETSTANDARD using System.Drawing; using System.Drawing.Drawing2D; #endif public static partial class Extensions { #if !NETSTANDARD /// <summary> /// An Image extension method that scales an image to the specific ratio. /// </summary> /// <param name="this">The @this to act on.</param> /// <param name="ratio">The ratio.</param> /// <returns>The scaled image to the specific ratio.</returns> public static Image Scale(this Image @this, double ratio) { int width = Convert.ToInt32(@this.Width*ratio); int height = Convert.ToInt32(@this.Height*ratio); var r = new Bitmap(width, height); using (Graphics g = Graphics.FromImage(r)) { g.CompositingQuality = CompositingQuality.HighQuality; g.SmoothingMode = SmoothingMode.HighQuality; g.InterpolationMode = InterpolationMode.HighQualityBicubic; g.DrawImage(@this, 0, 0, width, height); } return r; } /// <summary> /// An Image extension method that scales an image to a specific with and height. /// </summary> /// <param name="this">The @this to act on.</param> /// <param name="width">The width.</param> /// <param name="height">The height.</param> /// <returns>The scaled image to the specific width and height.</returns> public static Image Scale(this Image @this, int width, int height) { var r = new Bitmap(width, height); using (Graphics g = Graphics.FromImage(r)) { g.CompositingQuality = CompositingQuality.HighQuality; g.SmoothingMode = SmoothingMode.HighQuality; g.InterpolationMode = InterpolationMode.HighQualityBicubic; g.DrawImage(@this, 0, 0, width, height); } return r; } #endif }