ASP.NET MVC ImageHandler for thumbnails

by Tom 26. January 2009 10:33
As some of you might know, I am a really big fan of community-driven technologies. I also like to contribute myself in order to exchange new ideas and meet new people with common interests.Since I am quite new to ASP.NET MVC, I was thinking I would submit my first contribution : handling all your images through a custom actionresult/controller.
This allows me to do several things :
  • uniform access to images
  • possibility for custom types (this example ALWAYs renders as PNG)
  • generation of thumbnails.

Let's start with the code for the ActionResult (initially inspired by/copy-pasted from this , but since the new RC it is my own code):

using System.IO;
using System.Web.Mvc;
using System.Drawing;
using System.Drawing.Imaging;

namespace Be.Corebvba.MVC
{
 
public class ImageResult : FileStreamResult
  {

    public ImageResult(Image input):this(input,input.Width,input.Height) { }

    public ImageResult(Image input, int width,int height) :
      
base(
         GetMemoryStream(input,width,height,
ImageFormat.Png),
        
"image/png") 
   { }

   
static MemoryStream GetMemoryStream(Image input,int width,int height,ImageFormat fmt)
    {
       // maintain aspect ratio
      
if (input.Width > input.Height)
          height = input.Height * width / input.Width; 
       else
         
width = input.Width * height / input.Height;
      
      
var bmp = new Bitmap(input, width, height);
       var ms = new MemoryStream();
       bmp.Save(ms,
ImageFormat.Png);
       ms.Position = 0;
      
return ms;
     }
   }
}

The code for the controller is pretty straighforward as well :

public ActionResult ThumbImage(int id,int width,int height)
{
  
var d = _rDocument.GetById(id);
  
Image i=null;
  
try
  
{
      i =
Image.FromFile(d.PhysicalFilename);
     
return new Be.Corebvba.MVC.ImageResult(i, width, height);
   }
  
catch (Exception ex)
   {
      i =
new Bitmap(1, 1);
     
return new Be.Corebvba.MVC.ImageResult(i,1,1);
   }
  
finally
  
{
     
if (i != null) i.Dispose();
   }
}

This does need some better errorhandling, but I suppose you get the idea... (The _rDocument is a pointer to a Document repository which stores physical files. If you access your images from a database, you could simply use Image.FromStream instead of an Image.FromFile .In my document i reference the image as follows, et voila, automatic thumbnails :

<% var imgurl = Url.Action("ThumbImage", "Documents", new { id = d.ID,width=100,height=100 }); %>
<img src="<%=imgurl %>" style="float:left"/> 
Bookmark and Share

Comments

5/3/2009 2:30:01 AM #

trackback

Trackback from Steve Moseley

MVC and Thumbnails

Steve Moseley |

9/23/2011 8:59:15 AM #

pingback

Pingback from freerob.com

HTML | Free Software | Free Templates | Freerob.com

freerob.com |

About Tom

Tom Janssens op LinkedIn

Tom Janssens op twitter

Core bvba RSS

 

Tom Janssens is an independent freelance ICT consultant that has been "into computers" ever since the age of 7.

Typing source code from a book evolved into exploring the limits of coding in procedural, assembly and object-oriented languages.
As he matured in software coding, he started focussing on the problems surrounding software development, and learned that software development is usually about people and interactions first, and about technology second.

Due to his diverse track record he gained insights in a lot of aspects of the software development process. Currently his main focus is on strategic ICT advice, lean product/project development and improving the software development process and architecture.

He avoids ivory-tower-approaches by applying and verifying the applicability of the latest tech buzz in software experiments.

He is also the founder of the following LinkedIn groups:

CQRS Professional
BDD Professional
Asp.Net MVC professional

More info about Tom and his company...


Advertisement

Forget all your SCRUM -, Kanban - and other Agile and Lean certificates

Here is the only true AGILE and LEAN certificate you will ever need:

The Creative Recursive Analysis Process Certificate
(CRAP Certificate for short)

More info can be found at the official CRAP certificate website:
http://bit.ly/CRAPCertificate