public static Bitmap Resize(string path, Interpolations interpolation, int width, int height) { Bitmap original = new Bitmap(path); Bitmap resized = new Bitmap(width, height, System.Drawing.Imaging.PixelFormat.Format32bppArgb); double wScale = (double)resized.Width / original.Width; double hScale = (double)resized.Height / original.Height; bool needsScaling = false; switch (interpolation) { case Interpolations.NearestNeighbor: // Applies nearest neighbor sampling straight for (int i = 0; i < resized.Width; i++) { for (int j = 0; j < resized.Height; j++) { resized.SetPixel(i, j, original.GetPixel((int)(i / wScale), (int)(j / hScale))); } } break; case Interpolations.Bilinear: if (wScale >= 0.5) { // Bilinear Interpolation resized = BilinearResize(original, wScale, hScale); // If scaling smaller than 1, applies blurring when bilinear is done if (wScale < 1) { resized = Effects.Blur(resized, wScale); } } else { // Box Filtering resized = BoxFilterResize(original, wScale, hScale); } break; case Interpolations.Bicubic: // If scaling is smaller than 0.25, // enlarges image using box filtering first needsScaling = wScale < 0.25; // First, applies box filter scaling to incrase it by 4 of its size // Finally, applies bicubic resizing resized = BicubicResize( needsScaling ? BoxFilterResize(original, 4, 4) : original, needsScaling ? 4 * wScale : wScale, needsScaling ? 4 * hScale : hScale); break; case Interpolations.BicubicSmoother: // If the scaling is smaller than 0.25, // enlarges image using box filtering first needsScaling = wScale < 0.25; // First, applies box filter scaling to incrase it by 4 of its size // Then, applies bicubic resizing resized = BicubicResize( needsScaling ? BoxFilterResize(original, 4, 4) : original, needsScaling ? 4 * wScale : wScale, needsScaling ? 4 * hScale : hScale); // Finally, applies blurring resized = Effects.Blur(resized, 1.15); break; case Interpolations.BicubicSharper: // If the scaling is smaller than 0.25, // enlarges image using box filtering first needsScaling = wScale < 0.25; // First, applies box filter scaling to incrase it by 4 of its size // Then, applies bicubic resizing resized = BicubicResize( needsScaling ? BoxFilterResize(original, 4, 4) : original, needsScaling ? 4 * wScale : wScale, needsScaling ? 4 * hScale : hScale); // Finally, applies blurring resized = Effects.Blur(resized, 1.05); break; } return(resized); }