12

I have used Umbraco and there is very nice ImageGen library there which allows to resize images 'on the fly' and cashes processed images.

Is there something similar to it I can use outside Umbraco?

(I thought I could use ImageGen without Umbraco but it looks like it is not free)

Thanks

Burjua
  • 12,506
  • 27
  • 80
  • 111

3 Answers3

13

I realise this question is quite old, but since I still stumbled across it, I thought I should post my findings.

http://imageprocessor.org/ seems to be a relatively new library that is free and open source, and has quite a number of features too!

Paul McLean
  • 3,450
  • 6
  • 26
  • 36
  • Thank you for that free tool. It does not need a paid license; that is great. – Thomas.Benz Feb 28 '16 at 15:32
  • This appears to be perfect for my needs. It is also slightly more popular (read: more likely to be maintained) than ImageResizer, according to their NuGet pages: https://www.nuget.org/packages/ImageResizer/ https://www.nuget.org/packages/ImageProcessor/ – sfarbota Apr 27 '16 at 15:27
  • Second this. We are moving to imageprocessor since our usage with imageresizer now will require SaaS license ($7,000!!!) on the new version. – DPac May 13 '16 at 14:17
  • This has now been superseded by ImageSharp - https://github.com/SixLabors/ImageSharp – richardwhatever May 06 '19 at 08:45
6

I wrote the ImageResizer HttpModule, which is free, open-source, and freely supported through the StackOverflow imageresizer tag.

It has 39 plugins, some of which require a license, but all of the source code for everything is available at http://imageresizing.net/download.

It works with all .NET CMSes, including Umbraco.

Unlike ImageGen, it is designed to scale up to millions of images.

Community
  • 1
  • 1
Lilith River
  • 16,204
  • 2
  • 44
  • 76
3

The .NET Framework includes support for image resizing.

Below is some sample code from my book, Ultra-Fast ASP.NET:

using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;
using System.IO;

namespace Samples
{
    public class ImageResizer
    {
        private static ImageCodecInfo jpgEncoder;

        public static void ResizeImage(string inFile, string outFile, double maxDimension, long level)
        {
            //
            // Load via stream rather than Image.FromFile to release the file
            // handle immediately
            //
            using (Stream stream = new FileStream(inFile, FileMode.Open))
            {
                using (Image inImage = Image.FromStream(stream))
                {
                    double width;
                    double height;

                    if (inImage.Height < inImage.Width)
                    {
                        width = maxDimension;
                        height = (maxDimension / (double)inImage.Width) * inImage.Height;
                    }
                    else
                    {
                        height = maxDimension;
                        width = (maxDimension / (double)inImage.Height) * inImage.Width;
                    }
                    using (Bitmap bitmap = new Bitmap((int)width, (int)height))
                    {
                        using (Graphics graphics = Graphics.FromImage(bitmap))
                        {
                            graphics.SmoothingMode = SmoothingMode.HighQuality;
                            graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
                            graphics.DrawImage(inImage, 0, 0, bitmap.Width, bitmap.Height);
                            if (inImage.RawFormat.Guid == ImageFormat.Jpeg.Guid)
                            {
                                if (jpgEncoder == null)
                                {
                                    ImageCodecInfo[] ici = ImageCodecInfo.GetImageDecoders();
                                    foreach (ImageCodecInfo info in ici)
                                    {
                                        if (info.FormatID == ImageFormat.Jpeg.Guid)
                                        {
                                            jpgEncoder = info;
                                            break;
                                        }
                                    }
                                }
                                if (jpgEncoder != null)
                                {
                                    EncoderParameters ep = new EncoderParameters(1);
                                    ep.Param[0] = new EncoderParameter(Encoder.Quality, level);
                                    bitmap.Save(outFile, jpgEncoder, ep);
                                }
                                else
                                    bitmap.Save(outFile, inImage.RawFormat);
                            }
                            else
                            {
                                //
                                // Fill with white for transparent GIFs
                                //
                                graphics.FillRectangle(Brushes.White, 0, 0, bitmap.Width, bitmap.Height);
                                bitmap.Save(outFile, inImage.RawFormat);
                            }
                        }
                    }
                }
            }
        }

        public static void GetImageSize(string inFile, out int width, out int height)
        {
            using (Stream stream = new FileStream(inFile, FileMode.Open))
            {
                using (Image src_image = Image.FromStream(stream))
                {
                    width = src_image.Width;
                    height = src_image.Height;
                }
            }
        }
    }
}
RickNZ
  • 18,448
  • 3
  • 51
  • 66
  • Thanks RickNZ, I might use it, but at the moment looking for out of the box solution, hope to find something – Burjua Dec 02 '11 at 15:54
  • 1
    Also, this solution causes a white or black border artifact around the image. You need to pass DrawImage an ImageAttributes instance with ImageModeXY set. – Lilith River Jan 09 '12 at 22:02
  • I am unable to repro that bug; the code works fine for me as-is, with no white or black borders, for both GIF and JPG images. What was your test case? – RickNZ Jan 26 '12 at 10:12
  • Take 2 images, 1 fully white, 1 fully black, and resize them. Depending upon the OS version, one or the other will have 1px gray border. This is because the HighQualityBicubic setting (which is still the best) uses a pre-resizing blur to increase the quality, and the edges are blurred against the default color. The solution is to create an ImageAttributes instance in a using(){} clause, and set TileFlipMode to TileFlipXY, then pass it in as the last parameter to DrawImage. – Lilith River Mar 26 '12 at 17:17
  • I believe in the code posted above, in the non JPG case, there needs to be this line for it to work, otherwise, you get a – Peter Kellner May 27 '12 at 00:48
  • One thing to be careful about is that any parallel calls to GDI+ (and graphics.DrawImage does just that) will be blocked at a process level (not thread!) so you will encounter some performance issues of you are running this code on a high load system with parallel image resizing calls. Check http://stackoverflow.com/questions/8766147/poor-performance-of-concurrent-graphics-rotatetransform-operations – VladT Aug 25 '14 at 09:05