Generating image thumbnails in ASP.NET?
Asked Answered
C

7

17

What is the fastest and more reliable way of generating thumbnails in .NET? I need to get any image, compress it in JPEG and resize it.

I've seen several examples with GDI+, some non-free components and I remember WPF has some good stuff about imaging. GDI+ is pretty old and the WPF stuff maybe has no benefits on a server environment though.

This has to work in a ASP.NET MVC application that runs on full trust, and if possible, synchronously.

What would you recommend?

UPDATE:

Based on Mantorok's answer I have worked out this example, but it's still GDI+, and it crashes if I try with a large image:

public void GenerateThumbnail(String filename, Int32? desiredWidth, 
    Int32? desiredHeight, Int64 quality, Stream s)
{
    using (Image image = Image.FromFile(filename))
    {
        Int32 width=0, height=0;

        if ((!desiredHeight.HasValue && !desiredWidth.HasValue) ||
            (desiredHeight.HasValue && desiredWidth.HasValue))
            throw new ArgumentException(
                "You have to specify a desired width OR a desired height");

        if (desiredHeight.HasValue)
        {
            width = (desiredHeight.Value * image.Width) / image.Height;
            height = desiredHeight.Value;
        }
        else
        {
            height = (desiredWidth.Value * image.Height) / image.Width;
            width = desiredWidth.Value;
        }

        using (var newImage = new Bitmap(width, height))
        using (var graphics = Graphics.FromImage(newImage))
        using (EncoderParameter qualityParam = 
            new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, 
                quality))
        using (EncoderParameters encoderParams = new EncoderParameters(1))
        {
            graphics.DrawImage(image, 0, 0, width, height);
            ImageCodecInfo jpegCodec = ImageCodecInfo.GetImageEncoders().
                Single(e => e.MimeType.Equals("image/jpeg", 
                    StringComparison.Ordinal));
            encoderParams.Param[0] = qualityParam;
            newImage.Save(s, jpegCodec, encoderParams);
        }
    }
}
Celestina answered 4/5, 2011 at 10:55 Comment(5)
What is the exception being thrown?Incantation
OutOfMemoryException in Image.FromFile(filename)Celestina
Blimey! How big is the file!?Incantation
8.75 Mb : greatmarta.deviantart.com/art/KovuKiaraCubs-114524760Celestina
A quick search threw this up: #1109107Incantation
A
5

For intensive server-side code, I suggest you use other techniques than GDI+ that has not been designed to handle images chunk by chunk (in a streaming manner).

You can use Windows Imaging Component or WPF for this task. There is a very good example on how to do this in a fast and - more important - scalable manner here:

The fastest way to resize images from ASP.NET. And it’s (more) supported-ish.

Arciniega answered 4/5, 2011 at 19:38 Comment(0)
N
7

This has done me fine for years:

public static void CreateThumbnail(string filename, int desiredWidth, int desiredHeight, string outFilename)
{
    using (System.Drawing.Image img = System.Drawing.Image.FromFile(filename))
    {
        float widthRatio = (float)img.Width / (float)desiredWidth;
        float heightRatio = (float)img.Height / (float)desiredHeight;
        // Resize to the greatest ratio
        float ratio = heightRatio > widthRatio ? heightRatio : widthRatio;
        int newWidth = Convert.ToInt32(Math.Floor((float)img.Width / ratio));
        int newHeight = Convert.ToInt32(Math.Floor((float)img.Height / ratio));
        using (System.Drawing.Image thumb = img.GetThumbnailImage(newWidth, newHeight, new System.Drawing.Image.GetThumbnailImageAbort(ThumbnailImageAbortCallback), IntPtr.Zero))
        {
            thumb.Save(outFilename, System.Drawing.Imaging.ImageFormat.Jpeg);
        }
    }
}

public static bool ThumbnailImageAbortCallback()
{
    return true;
}
Nunn answered 4/5, 2011 at 19:14 Comment(2)
simple and elegant; I am stealing this for sure :)Ghastly
Try that with a large image, though. 1920 * 60000, for example. Isn't robust enough.Sobriquet
A
5

For intensive server-side code, I suggest you use other techniques than GDI+ that has not been designed to handle images chunk by chunk (in a streaming manner).

You can use Windows Imaging Component or WPF for this task. There is a very good example on how to do this in a fast and - more important - scalable manner here:

The fastest way to resize images from ASP.NET. And it’s (more) supported-ish.

Arciniega answered 4/5, 2011 at 19:38 Comment(0)
B
3

I use ImageMagick for photo processing

UPDATED

Model:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO;
using ImageMagickObject;

namespace photostorage.Models
{
    public class PhotoProcessing
    {
        public MagickImage ResizeImg(string filepath, string filename)
        {
            Object[] rotate = new Object[] { filepath + "/" + filename, 
                "-auto-orient", filepath + "/" + filename };
            Object[] big = new Object[] { filepath + "/" + filename, 
                "-resize", "800", filepath + "/" + "big_" + filename };
            Object[] middle = new Object[] { filepath + "/big_" + filename, 
                "-resize", "400", filepath + "/" + "mid_" + filename };
            Object[] small = new Object[] { filepath + "/mid_" + filename, 
                "-resize", "200", filepath + "/" + "small_" + filename };
            Object[] crop = new Object[] { filepath + "/small_" + filename, 
                "-resize", "50", filepath + "/" + "crop_" + filename };
            ImageMagickObject.MagickImage img = 
                new ImageMagickObject.MagickImage();
            img.Convert(rotate);
            img.Convert(big);
            img.Convert(middle);
            img.Convert(small);
            img.Convert(crop);
            return img;
        }
    }
}

Controller:

PhotoProcessing resizeImg = new PhotoProcessing();
[HttpPost]
public string Index(params,params,params...)
{
    var GetResize = resizeImg.ResizeImg(
        destinationFolder + "/" + curFolder, fullFileName);
}
Botulin answered 4/5, 2011 at 13:33 Comment(5)
How do you use it? I've found a .net wrapper, but it is undocumented and that classes you mention don't exists on it.Celestina
i dont use ImageMagick.NET - i use the ImageMagickObject dll (cause, like you wrote, the .Net vertion is fully undocumented). Wait a minute, i will put full solution hereBotulin
Is ImageMagickObject a COM+ object?Celestina
yes. downloaded and setuped to computer, then attached to projectBotulin
sorry, I forgot to mention that I'm using Azure, and apparently COM+ objects don't work :( . Good example anyway, thanks a million. I'll use in other projects.Celestina
S
3

See my answer here Create thumbnail image

There is a function on Image which returns a thumbnail like this:

Image image = Image.FromFile(fileName);
Image thumb = image.GetThumbnailImage(120, 120, ()=>false, IntPtr.Zero);
thumb.Save(Path.ChangeExtension(fileName, "thumb"));
Sandglass answered 4/5, 2011 at 19:2 Comment(0)
I
2

I can't say whether this is the most efficient manner, but here is a snippet of code I wrote to produce 3 smaller images from a large image:

  private void GenerateImages(byte[] data, string extension, string filename)
  {
     // Assuming data is the original filename.
     var ms = new MemoryStream(data);
     var image = Image.FromStream(ms);
     image.Save(filename);
     ResizeImage(image, 800, 600, "large.jpg");
     ResizeImage(image, 480, 320, "medium.jpg");
     ResizeImage(image, 192, 144, "small.jpg");
  }

  private void ResizeImage(Image image, int width, int height, string filename)
  {
     using (var newImage = new Bitmap(width, height))
     {
        var graphics = Graphics.FromImage(newImage);
        graphics.DrawImage(image, 0, 0, width, height);
        newImage.Save(filename, ImageFormat.Jpeg);
     }
  }
Incantation answered 4/5, 2011 at 11:27 Comment(0)
N
0

http://www.codeproject.com/KB/graphics/image_croppingVBNet.aspx

this is for center croping image i hope i will help you more..

http://www.codeproject.com/KB/GDI-plus/imageresize.aspx

Nelsen answered 4/5, 2011 at 13:10 Comment(0)
A
0

I am using Aurigma Image Uploader tool. it's pretty nice control. but it is paid you can check aurigma

Alphabetize answered 4/5, 2011 at 13:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.