2

I want a C# library for that which is open source and free.

I want this library to do optimization or thumbnail creation based on real algorithms that are standard.

If it supports a range of well known extensions like jpg, bmp, gif, png, svg and tiff I would appreciate that.

3

I suggest Magick.NET:

  • supports over 100 major file formats
  • Open source
  • Linked with ImageMagick, which is the reference in this domain, most image transformations (thumbnails, optimisations) on the web are powered by ImageMagick
  • Example operations: Rezize, convert, combine, watermark, colorize, draw text and shape, EXIF
  • Lossless compression if needed

logo

Example code to resize an image:

// Read from file
using (MagickImage image = new MagickImage("input.jpg"))
{
    MagickGeometry size = new MagickGeometry(100, 100);

    // Resize the image to a fixed size without maintaining the aspect ratio.
    // Normally an image will be resized to fit inside the specified size.
    size.IgnoreAspectRatio = true;

    image.Resize(size);

    // Save the result
    image.Write("output_100x100.png");
}
| improve this answer | |
0

You can use this function for thumbnails and others to use this function for the original aspect ratio. You can return Bitmap but delete (Image) before return function and before function name.

public static Image ResizeImageOriginalRatio(Image image, int width, int height)
    {

        int oraginal_Height = image.Height;
        int oraginal_width = image.Width;
        int percent_Height = oraginal_Height/height;
        int percent_Width = oraginal_width/width;

        if ((oraginal_width / oraginal_Height) >= 1)
        {
            //Portrait
            width = oraginal_width / percent_Width;
        }
        else {
            //Landscape
            height = oraginal_Height / percent_Height;
        }

        var destRect = new Rectangle(0, 0, width, height);
        var destImage = new Bitmap(width, height);

        destImage.SetResolution(image.HorizontalResolution, image.VerticalResolution);

        using (var graphics = Graphics.FromImage(destImage))
        {
            graphics.CompositingMode = CompositingMode.SourceCopy;
            graphics.CompositingQuality = CompositingQuality.HighQuality;
            graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
            graphics.SmoothingMode = SmoothingMode.HighQuality;
            graphics.PixelOffsetMode = PixelOffsetMode.HighQuality;

            using (var wrapMode = new ImageAttributes())
            {
                wrapMode.SetWrapMode(WrapMode.TileFlipXY);
                graphics.DrawImage(image, destRect, 0, 0, image.Width, image.Height, GraphicsUnit.Pixel, wrapMode);
            }
        }

        return (Image)destImage;
    }
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.