ASP.NET Image Resize Handler

Ever wish you could resize or change the format of an image on the fly? Maybe to get a gif or png into your Flash movie (pre-Flash Player 8, of course), or to save the hassle of creating thumbnails for every hi-res image that you put on your site?

Not that you couldn’t before, but now you have another tool to do the job: Enter the Image Resize And Other Good Stuff HttpHandler. Implemented as a .NET Http Handler and written in C#, this tool makes it a breeze to do all of the above.

Installation:

  1. Download the source below
  2. From the Deploy\Bin folder, grab the Com.JasonNussbaum.Web.HttpHandlers.dll file and upload it to your server’s Bin folder
  3. From the Deploy\Web.config file, copy the section into your Web.config (if you don’t have one, you can simply use the Web.config in it’s entirety)
  4. Assuming the default path for the handler (Image.ashx), you can now insert resized/re-encoded images as simply as folows:
    http://www.tempuri.com/path/to/image.gif/120/90/jpg/Image.ashx

The Url rewrite pattern is as follows: http://your.server.com/[path/to/image.ext]/[width]/[height]/[new format]/Image.ashx (where Image.ashx is whatever you have set as the path in your Web.config entry. When the handler is called, it first checks to see if an image has already been resized at the requested dimensions/format for the requested image, and creates it if necessary.

Full source is included in the zip, along with a batch file to compile. You will need to ensure that the path to the C# compiler is set in your PATH environment variable (generally, this will be: %SYSTEMROOT%\Microsoft.Net\Framework\v1.1.4322\. Obviously, to compile, you will need to have the .NET Framework installed (you do not need the SDK or Visual Studio, just the Framework Redist).

TODO List:

  • Allow default width/height to be set via web.config, or default to actual image size
  • Allow option to constrain proportions
  • Fix bug with attempting to encode images as anything other than Gif or Jpg (at the moment, Tiff, Png, etc all throw a GDI+ error on my machine)
  • Requests?

Samples:
/files/PJN_BirthdayCollage_2005.jpg:
gif, 47 x 32: (/files/PJN_BirthdayCollage_2005.jpg/47/32/gif/Image.ashx)

jpg, 94 x 64 (/files/PJN_BirthdayCollage_2005.jpg/96/64/jpg/Image.ashx):

jpg, 188 x 128 (/files/PJN_BirthdayCollage_2005.jpg/188/128/Image.ashx):

Download:
ASP.NET Image Resize Handler (with source) v1.0.1.0

Share me: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • del.icio.us
  • Reddit
  • StumbleUpon
  • Technorati

Comments (16) left to “ASP.NET Image Resize Handler”

  1. Svein Erik wrote:

    Hey, thank you for sharing your code!
    I have 2 questions:

    1. Is it possible to keep the aspect ratio of the picutres that get resized using Image.ashx?

    2. When i enter the url of the pic ………./Image.ashx, i get a file-download box, no matter if i click save or cancel, the file gets created. But how can i avoid that this box appears?
    I’m looking for a resizer on a publish-system for webpages, so that a user can upload a picture, then it gets resized to a specific size & keeps the aspect ratio. Do you have any suggestions on how i can use your handler to do this?

    Thank you,
    Svein Erik, Norway.

  2. AlexS wrote:

    Good stuff. I look forward to the future revision

    Alex

  3. shailendra wrote:

    hei,

    thank you for giving a easy method for resizing picture but i have same problem.it is possible to save file without clicking save as dialog box.

    shailendra
    india

  4. jason wrote:

    Hrm. Not sure about the save as dialog…what browser are you using? Do you have a url I can take a look at?

    I’ll see what I can do about it, though. I think it’s about time to update this sucka anyway. :)

  5. Fred wrote:

    Hi,

    I have managed to get this working on my test machine. It works great. However when uploading to my windows 2003 server it just simply dosent work. I tried setting write permission for everyone on the images directory, still no luck.

    Can you advise?

    Thanks
    Fred.

  6. jason wrote:

    Hrm. I’ve run this without a problem on Win2003 - in fact, that’s the primary platform I’ve deployed it on. The only things I can think to confirm are that you’re running v1.1 or greater of the .NET Framework, that you’re ASP.NET account has write access on folders, and that you have the correct settings in your Web.config. If none of those fix it, let me know.

    Also, check your error and IIS logs to see if you can pull a specific error message - that always speeds up the debugging process.

    Last but not least…I do have some fixes/updates that I hope to roll out in the next few weeks. Not the least of which might include an upgrade to .NET 2.0 (while still supporting .NET 1.1).

  7. Luis Mauricio wrote:

    Thank You for this!!

    It really sucks that images don’t resize like they do in windows.

    I’ve donne and utility to resize the images but it doesnt work in firefox.

    This is great!

    Thank you
    LM

  8. Tomas wrote:

    Hello,

    Is it possible to resize images with an bmp extension?

    Thanks and keep up the good work!
    T;

  9. Dylan wrote:

    I just implemented this on my test site. I’m not sure if this project is still live since there hasn’t been any comments in a year but I’ve been trying to find a simple resizer using the httphandler and this one worked like a charm (I’m not a developer) on my first try.

    In the list of features to be added I would go with a cache timer for the generated images.

    Thanks for this.

    -Dylan

  10. Asha wrote:

    Hi,
    How can I embed windows media player in my IE using ashx.
    I have done it using html tags, but would like to know if there is a way to embed using ashx file.

  11. Recent Faves Tagged With "coding" : MyNetFaves wrote:

    […] >> coding Sprinkle some Groovy into your App Part 1 First saved by jbond00747 | 1 days ago blog-j: Symbolizing, Coding, Objectifying, Processing " ASP.NET… First saved by JamesNintendoNerd | 2 days ago Pandora Prototype in Action First saved by […]

  12. Nathanael Jones wrote:

    Nice work! It’s good to finally see a free image resizer appear. Nothing of the kind was available when I developed mine, unfortunately.

    A few suggestions:
    * When you cache versions, you need some checks and balances to prevent denial of service attacks. I opted for placing all cached images in a single folder, and hashing the request URL to get the filename. A configurable limit is placed on the number of cached images, and the 10% least used are deleted when that limit is reached. I also put an upper bound on the target size, so that can’t be abused.

    * GetThumbnailImage is not a good method to use… It will use embedded thumbnail data from the image if available, producing inconsistent and blurry results… Use DrawImage and high-quality compositing settings instead for photoshop-level quality.

    * If users upload new versions of images, the thumbnails won’t be updated. You need to set the UTC modified date on the cached versions to match the originals, and compare them to see if an update is required.

    * When the image is *cached* to disk, you don’t have to read it back into memory to send it to the client - this will result in very bad performance under load. Just use WriteFile()…

    * I’d recommend using using(){} around all objects that implement IDisposable… Bitmap and Image references are *not* garbage collected by .NET, so you need to make sure they are disposed if even if an exception occurs.

    Hope this helps!

    You can find out more about my image resizer and download it at
    http://nathanaeljones.com/products/asp-net-image-resizer/

  13. jason wrote:

    Hey Nathanael,

    hehe, that’s exactly why I wrote mine - couldn’t find one that did what I wanted to. the original version (aka the version that’s still up) was a stop-gap to get something working. I revamped it for work later on, making it more robust, but didn’t take the code with me when I left (at least, I haven’t been able to find it, heh).

    Truth is, I’ve been pushing your version on a couple projects lately - thanks for the link!

    Cheers.

  14. Nathanael Jones wrote:

    Yeah… I’m glad there are so many more options now… I think ASP.NET was the last web framework to get good image resizing stuff…

    I’ve been build a *lot* of custom solutions for people lately, so the latest 1.2 build has evolved into a more extensible and flexible framework.. The disk caching component can be used separately, as well as the image manipulation code. And you can now set resizing defaults for folders from code, or make your own syntax.

    Best Regards,
    Nathanael

  15. Andy wrote:

    Code is working fine.

    Thanks for the code download support.

  16. Steve wrote:

    Hey,there

    If you are a Mac user, i would like to introduce a product called PhotoMagic, which can help you easily resize your images or photos for wallpapers etc.

Post a Comment

*Required
*Required (Never published)