Scaling and Serving Images With .NET 3.5
I am working on a web application where images has to be scaled dynamically and served to the clie开发者_Python百科nt as fast as possible (with low overhead). I need to create something that scales and compresses high-quality PNGs to medium quality JPEGs.
Now, there are multiple ways of doing this and I am slightly confused which method provides the best solution for the application to be as fast as possible - and I was hoping you guys could guide me in the right direction.
This application will run on the .NET 3.5-platform and I am looking for fresh ways of doing this. Googling has given me some clues but most of the articles I found were VERY old (2000-2005 or so).
The client app is written in ASP.NET MVC, while the backend app is written in Web Forms. The images are located in a directory in the Web Forms application, but I guess that dosen't say the image handler must be written in Web forms.
So, do you have any suggestions?
- Suitable technique to use (HTTP Handlers, MVC, Static files with FTP or something else?)
- Any good articles on the subject (spare me the old ones)
- How to cache the images?
Have you thought about a dedicated image server? e.g. Scene 7 or something similar?
You could build one yourself, but unless this is part of your core application functionality, it would probably be better to use something pre-built (whether paid or free).
1) I would personally use ASP.NET MVC itself instead of re-inventingthe wheel. Implementing any custom handler can only be slightly faster which I do not believe is worth it. I have not seen an ImageResult yet but it is easy to create one that can handle various file types.
2) Scaling images is a fairly easy task. I would personally use OpenCV to do that since it is very fast and flexible. It also has a managed wrapper (Emgu) but I have not used it.
3) Just use ASP.NET caching and I will use the image path and its scale to set the URL to the image. e.g. http://server/image1/0.5
IMO, it really does not matter what you use - handler(ashx), page(aspx) or MVC to resize and serve the image. Use routes if you want user-friendly URLs. But most important thing would be to use caching - you can set response cache headers and/or use ASP.NET output caching and/or cache your thumbnail images on local file system (IMO, you should cache at all levels for best performance).
See here one sample code for generating thumbnails: http://www.west-wind.com/weblog/posts/283.aspx (this one is actually pretty old but it illustrates use of output caching).
Refer this article for comparison (speed vs quality) of resizing images using GDI/WPF/WIC: http://weblogs.asp.net/bleroy/archive/2009/12/10/resizing-images-from-the-server-using-wpf-wic-instead-of-gdi.aspx
精彩评论