3

Below is the action method that returns images from the database as they are, I need some lite library or write the code myself if short that will resize and compress these images according to my requirements "Make thumbnails" before they are passed to the HTTP response.

EDIT: Actually come to think of it, perhaps it would be best to save thumbnails in additional column, so now I need a way to compress and resize the images before they are saved to the database a long with saving a copy that is untouched. Saving images initially by passing them in HttpPostedFileBase and now need some tool that will resize and compress before saving to database.

public FileContentResult GetImage(int LineID)
{
    var PMedia = repository.ProductMedias.FirstOrDefault(x => x.LineID == LineID);

    if (PMedia != null)
    {
        return File(PMedia.ImageData, PMedia.ImageMimeType, PMedia.FileName);
    }
    else
    {
        return null;
    }
} 
Yuval Itzchakov
  • 146,575
  • 32
  • 257
  • 321
LaserBeak
  • 3,257
  • 10
  • 43
  • 73

4 Answers4

2

Is your thumbnail size different everytime? Otherwise it is probably optimal to have another Column/Storage with Resized photos for your Thumbnails than processing them every time.

Erik Philips
  • 53,428
  • 11
  • 128
  • 150
2

System.Drawing can be used to create thumbnails easily. However, its use is not supported for ASP.NET for a number of good reasons.

However however, if you took Erik Philips' suggestion and pre-generated all the thumbnails and stored them in the database alongside the originals, you would conceivably have a process of some sort (like a Windows service) that would periodically generate thumbs for rows that needed them. Because this process would generate the thumbs serially, you would not have the concerns about using System.Drawing as you would in an ASP.NET application (where you could easily have multiple threads gobbling up the relatively scarce system resources that System.Drawing wraps).

Edit: I just noticed the MVC tags. I don't know if System.Drawing is usable with MVC, or whether it's been superseded by something else. Generally, .NET has always had built-in useful graphics libraries that can do most simple things easily (I won't say it does simple things simply, as evidenced by the 30 overloads of the Graphics.DrawImage(...) method), so I expect you can still do this in MVC.

Community
  • 1
  • 1
MusiGenesis
  • 74,184
  • 40
  • 190
  • 334
  • Yeah, I'll do that. But I still need something that will work with ASP.NET to resize/compress the images into thumbnails before I can save them in the additional column. – LaserBeak Oct 28 '11 at 00:16
  • Do end-users upload the original images through your site? If so, you wouldn't want to use System.Drawing to generate the thumbnails inside a user-initiated call. If the images are only saved to the db through an internal process you control, System.Drawing would probably work fine for you. – MusiGenesis Oct 28 '11 at 00:20
2

Don't reinvent a wheel ! There is a very nice, free (open-source in fact), pure .NET library, usable with API or handler :

http://imageresizing.net/

It supports very high quality resizing, converting to bunch of formats, auto-cropping, rotating, watermarking...

And if you think "nah, i can do it myself!", here is 20 reasons why you shouldnt :

http://nathanaeljones.com/163/20-image-resizing-pitfalls/

rouen
  • 5,003
  • 2
  • 25
  • 48
1

Here is routine that I use for making thumbnails :

    public void MakeThumbnail(string imagePath)
    {
        // Image exists?
        if (string.IsNullOrEmpty(imagePath)) throw new FileNotFoundException("Image does not exist at " + imagePath);

        // Default values
        string Filename = imagePath.ToLower().Replace(".jpg", "_thumb.jpg");
        int Width = 100; // 180;
        int Height = 75; // 135;
        bool lSaved = false;

        // Load image
        Bitmap bitmap = new Bitmap(imagePath);

        // If image is smaller than just save
        try
        {
            if (bitmap.Width <= Width && bitmap.Height <= Height)
            {
                bitmap.Save(Filename, ImageFormat.Jpeg);
                lSaved = true;
            }
        }
        catch (Exception e)
        {
            throw new Exception(e.Message);
        }
        finally
        {
            bitmap.Dispose();
        }

        if (!lSaved)
        {
            Bitmap FinalBitmap = null;
            // Making Thumb
            try
            {
                bitmap = new Bitmap(imagePath);

                int BitmapNewWidth;
                decimal Ratio;
                int BitmapNewHeight;

                // Change size of image
                Ratio = (decimal)Width / Height;
                BitmapNewWidth = Width;
                BitmapNewHeight = Height;

                // Image processing
                FinalBitmap = new Bitmap(BitmapNewWidth, BitmapNewHeight);
                Graphics graphics = Graphics.FromImage(FinalBitmap);
                graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
                graphics.FillRectangle(Brushes.White, 0, 0, BitmapNewWidth, BitmapNewHeight);
                graphics.DrawImage(bitmap, 0, 0, BitmapNewWidth, BitmapNewHeight);

                // Save modified image
                FinalBitmap.Save(Filename, ImageFormat.Jpeg);

            }
            catch (Exception e)
            {
                throw new Exception(e.Message);
            }
            finally
            {
                if (FinalBitmap != null) FinalBitmap.Dispose();
            }
        }
    }
Michael Kopljan
  • 177
  • 1
  • 3
  • 12