Published articles on other web sites*

Published articles on other web sites*

NuGet Package of Week #11 - ImageResizer enables clean, clear image resizing in ASP.NET

NuGet Package of Week #11 - ImageResizer enables clean, clear image resizing in ASP.NET:
The Backstory: I was thinking since the NuGet .NET package management site is starting to fill up that I should start looking for gems (no pun intended) in there. You know, really useful stuff that folks might otherwise not find. I'll look for mostly open source projects, ones I think are really useful. I'll look at how they built their NuGet packages, if there's anything interesting about the way the designed the out of the box experience (and anything they could do to make it better) as well as what the package itself does. Today, it's imageresizer.
Bertrand Le Roy has long been an advocate of doing image resizing correctly on .NET and particularly on ASP.NET. Last week he posted a great post on a new library to choose from; a library that is pure .NET and works in medium trust. It's "imageresizer." What a creative name! ;)
Seriously, though, it couldn't be easier. Here's a nice sample from Bertrand's blog showing how to do resizing of a JPEG as stream of bytes using the imageresizer library directly:
var settings = new ResizeSettings {
MaxWidth = thumbnailSize,
MaxHeight = thumbnailSize,
Format = "jpg"
};
settings.Add("quality", quality.ToString());
ImageBuilder.Current.Build(inStream, outStream, settings);
resized = outStream.ToArray();


There's a complete API with lots of flexibility. However, how quickly can I get from File | New Project to something cool?


ImageResizer




Well, make a new ASP.NET (MVC or WebForms) project and put an image in a folder.


Their default NuGet package is called ImageResizer, and their ASP.NET preconfigured web.config package is "ImageResizer.WebConfig" which includes a default intercepting module to get you the instant gratification you crave. I used NuGet to install-package imageresizer.webconfig.


I've got an image of my giant head that I can, of course, visit in any browser.


imageresizer


And now with the intercepting HttpModule installed with imageresizer.webconfig I can add ?width=100 to the end of the query string and I get a nice resized image that fits into the constraints of "100 wide." It's a trivial example, but it's a nice touch to have them do the "figure out how tall this should be" work for me.


imageresizer2


Of course, I'm sure you could DoS (Denial of Service) someone's system with resizing request, but for small sites I their intercepting module is a quick fix and a great example. DoS problems aren't unique to CPU intensive requests and a problem solved elsewhere.


For more sophisticated use they include a separate API dll where you can do even more like cropping, rotating, flipping, watermarking and even conversion. Bertrand has a chart that explores their speed issues, as they are slower than straight GDI and Windows Imaging Components, but as I said, they are pure managed code and work in Medium Trust which is a huge win. Their quality is also top notch.


ImageResizer also includes plugin support that you can buy. Genius, seriously, I tip my hat to these guys. The most popular and useful features are free, and crazy easy to use. If you want to do even more you buy plugins like DiskCache for huge performance wins, S3Reader or AzureReader for Amazon or Azure support, and lots of free plugins for 404 handling, DropShadows and more. So polished. Kudos to Nathanael Jones and team for a really nice use of ASP.NET, .NET, NuGet and a clever open source library with a plugin model for profit.

No comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...