Resizing and displaying images from a server using ASP.NET

Got a question. I have images hosted on my server. I already know the method when the image is loaded to resize and save, but I have a different thought.

  • I was wondering if there is a way to resize when the image is requested from the user. Not when it was uploaded by the user.

So, for example, the user uploads an image, and I DO NOT DELETE it and save another copy of the changed image. Instead, when an image is requested by a user through an ASP.NET img control / tag, he will resize the image on the fly to display it and display it using the img tag / control.

Why should I do this?

Saving to disk. Most servers have a disk space limit, but not a server processing limit. Therefore, I would like to save to disk and use the processing space instead.

EDIT: As a boot website, it is now better to save the disk than to save processing time. At this moment, I have little money for a lot of space. I hope it changes when you start the site.

Any ideas? Thank you guys and girls.

+4
source share
5 answers

I assume that you can "manage" the URLs for the modified images, so for example a full-size image might be referred to as <img src="uploads/myphoto.jpg"/> thumbnail could be in ASPX or ASHX, for example <img src="uploads/myphoto.jpg.ashx"/> ?

This article in an article on CodeProject - Dynamic Image Resizing seems to have exactly the source code you are looking for (and although it is in VB, it should not be difficult to port if you are a C # person). Hope this helps.

Finally, I would advise you to consider various forms of caching (both using Http-Headers, and to ensure that images are cached on the client or proxy whenever possible, and using ASP.NET built-in functions to avoid unnecessary processing from the same over-and-over images).

Although you will keep disk quotas, you effectively slow down every other page / request ... just a thought.

+4
source

Dynamic image resizing has many advantages, the least of which is reduced disk space usage. However, it must be combined with a form of persistent caching, such as Amazon CloudFront or disk cache.

Dynamic image resizing gives you more flexibility on your website, while pre-generated image variations block you, preventing possible changes that you have to make. When combined with caching between them, there is no difference in performance at runtime.

The ImageResizer library offers disk caching, CloudFront caching, and proper memory and cache management . It is constantly being improved and maintained since 2007 and is bulletproof enough. He runs several social networking sites, and some have over a million images.

This is a time-tested, time-tested and module-checked library. It is also extremely easy to use - you just add ?width=x&height=y to the query string. Functionality can be added through 20+ plugins, so you will not be burdened with unused code and functions.

The article mentioned by CraigD is inherently limited in its performance by using HttpHandler instead of HttpModule, and HttpHandler cannot pass the request back to IIS source code for execution after the modified image is written to disk. It also incorrectly adjusts jpeg encoding or works well with ASP.NET cache or URL authorization system. Although, I must admit - compared to most of the sample code I saw, it breaks a much smaller number of resizing errors that I compiled .

I highly recommend using the ImageResizer library . This is good code, I wrote it :) If you end up using a sample code or writing your own, please avoid these pitfalls !

+2
source

You can create an IHttpHandler implementation to respond to image requests, in this handler you can have code that loads the image from disk and converts it to the required size. You need to return the correct mime type with the answer and use the WriteBytes method (or something like this, I forgot the name). In addition, you can see the headers with exhausted content so that the image does not need to be loaded every time by the same client, but instead was cached.

0
source

You claim unlimited processing, but limited disk space. In most cases, even if they don’t apply a processing limit, since you have more customers, getting to your website, processing will be a worse bottleneck than storage space, and it will cost more to add more processing. Besides,

  • If you have large images, the size and compressed versions will occupy% 10 the place of the originals, even if you save the version of the display and thumbnails.
  • Else, just serve them and display them in size with your browser, it will be faster.
0
source

In fact, this is not the actual size of the image, it is quite resized when displaying the image, but I just used it successfully

  <img src="myimage" height="height you want to give" width="width you want to give" alt="" /> 

It works every time.

-1
source

All Articles