2

i am looking for a class (with c#) that check my image sizes (width & height)(or image size in kb) and if they are not match with my favorite sizes , resize them (mean with Constrain and relative width & height new size like in photoshop image size = so we will not lose image appearance)

there are many classes for this job / but which one is better?

thanks in future advance

SilverLight
  • 19,668
  • 65
  • 192
  • 300
  • Everyone who [uses this library loves it](http://imageresizing.net), at least according to their e-mails :) It's free, and it includes sample code for exactly what you want to do. – Lilith River May 28 '11 at 16:17

3 Answers3

9

This is the method I'm using for resizing, when I specify desired width:

private Image ResizeImage(Image original, int targetWidth)
{
    double percent = (double)original.Width / targetWidth;
    int destWidth = (int)(original.Width / percent);
    int destHeight = (int)(original.Height / percent);

    Bitmap b = new Bitmap(destWidth, destHeight);
    Graphics g = Graphics.FromImage((Image)b);
    try
    {
        g.InterpolationMode = InterpolationMode.HighQualityBicubic;
        g.SmoothingMode = SmoothingMode.HighQuality;
        g.PixelOffsetMode = PixelOffsetMode.HighQuality;
        g.CompositingQuality = CompositingQuality.HighQuality;

        g.DrawImage(original, 0, 0, destWidth, destHeight);
    }
    finally
    {
        g.Dispose();
    }

    return (Image)b;
}
Tushar Gupta - curioustushar
  • 58,085
  • 24
  • 103
  • 107
Andrey
  • 20,487
  • 26
  • 108
  • 176
3

This one seems to be very highly regarded around these parts.

The other answers to that question include links to several other options, including Imagemagick.

Community
  • 1
  • 1
Town
  • 14,706
  • 3
  • 48
  • 72
0
public static class clsImageResize
{
    public static int TnewWidth { get; set; }
    public static int TnewHeight { get; set; }
    public static string TfilePath { get; set; }

    public static void Resize(string filePath, int newWidth, int newHeight)
    {
        TfilePath = filePath;
        TnewWidth = newWidth;
        TnewHeight = newHeight;
        Bitmap image = new Bitmap(TfilePath);
        //Image m = Image.FromFile(TfilePath);
        Save(image, TnewWidth, TnewHeight, 8, TfilePath);
    }

    private static void Save(Bitmap image, int maxWidth, int maxHeight, int quality, string filePath)
    {
        // Get the image's original width and height
        int originalWidth = image.Width;
        int originalHeight = image.Height;

        // To preserve the aspect ratio
        float ratioX = (float)maxWidth / (float)originalWidth;
        float ratioY = (float)maxHeight / (float)originalHeight;
        float ratio = Math.Min(ratioX, ratioY);

        // New width and height based on aspect ratio
        int newWidth = (int)(originalWidth * ratio);
        int newHeight = (int)(originalHeight * ratio);

        // Convert other formats (including CMYK) to RGB.
        Bitmap newImage = new Bitmap(newWidth, newHeight, PixelFormat.Format24bppRgb);

        // Draws the image in the specified size with quality mode set to HighQuality
        using (Graphics graphics = Graphics.FromImage(newImage))
        {
            graphics.CompositingQuality = CompositingQuality.HighQuality;
            graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
            graphics.SmoothingMode = SmoothingMode.HighQuality;
            graphics.DrawImage(image, 0, 0, newWidth, newHeight);
        }

        // Get an ImageCodecInfo object that represents the JPEG codec.
        ImageCodecInfo imageCodecInfo = GetEncoderInfo(ImageFormat.Jpeg);

        // Create an Encoder object for the Quality parameter.
        Encoder encoder = Encoder.Quality;

        // Create an EncoderParameters object. 
        EncoderParameters encoderParameters = new EncoderParameters(1);

        image.Dispose();
        // Save the image as a JPEG file with quality level.
        EncoderParameter encoderParameter = new EncoderParameter(encoder, quality);
        encoderParameters.Param[0] = encoderParameter;
        newImage.Save(filePath, imageCodecInfo, encoderParameters);
    }

    private static ImageCodecInfo GetEncoderInfo(ImageFormat format)
    {
        return ImageCodecInfo.GetImageDecoders().SingleOrDefault(c => c.FormatID == format.Guid);
    }
}
vicky
  • 1,546
  • 1
  • 18
  • 35