<%@ WebHandler Language="C#" Class="ViewContent" %> using System; using System.Web; using System.Configuration; using System.Drawing; using System.Drawing.Imaging; using System.Drawing.Drawing2D; using System.IO; using Memories; using Memories.Entities; public class SizeExtended { private float _Width = 0; public float Width { get { return _Width; } set { _Width = value; } } private float _Height = 0; public float Height { get { return _Height; } set { _Height = value; } } private float _XOffset = 0; public float XOffset { get { return _XOffset; } set { _XOffset = value; } } private float _YOffset = 0; public float YOffset { get { return _YOffset; } set { _YOffset = value; } } private float _Ratio = 0; public float Ratio { get { return _Ratio; } set { _Ratio = value; } } public override string ToString() { return _Width.ToString() + "x" + _Height.ToString(); } } public class ViewContent : IHttpHandler { private string _Path = ""; private SizeF _MaxSize; private bool _Crop = false; public void ProcessRequest(HttpContext context) { Settings _Settings = new Settings(); _Path = context.Request.QueryString["path"]; //_Settings.TempPath = @"d:\kunder\zilch\www\systema-ptw\App_Data\GalleryCache"; //_Settings.TempPath = @"d:\kunder\zilch\db\gallerycache"; Memories.Entities.File file = MemoriesDB.GetFile(_Settings, _Path); if (context.Request.Params["ImageWidth"] != null && context.Request.Params["ImageWidth"] != "") { _MaxSize.Width = Single.Parse(context.Request.Params["ImageWidth"]); } if(context.Request.Params["ImageHeight"] != null && context.Request.Params["ImageHeight"] != "") { _MaxSize.Height = Single.Parse(context.Request.Params["ImageHeight"]); } if(context.Request.Params["Crop"] != null && context.Request.Params["Crop"] != "") { _Crop = Boolean.Parse(context.Request.Params["Crop"]); } if(file != null) { HttpResponse response = HttpContext.Current.Response; response.Cache.SetCacheability(HttpCacheability.Public); response.Cache.SetExpires(DateTime.Now.AddDays(1)); response.ContentType = "image/jpeg"; if(_MaxSize.Width < 1 && _MaxSize.Height < 1) { response.WriteFile(file.FullName); } else { string filePath = getSavePath(_Settings, _Path, _MaxSize); if(!System.IO.File.Exists(filePath) || System.IO.File.GetLastWriteTime(filePath) < file.LastWriteTime) { saveResizedImage(file.FullName, filePath, _MaxSize); } response.WriteFile(filePath); } response.End(); } } public bool IsReusable { get { return true; } } string getSavePath(Settings pSettings, string pPath, SizeF pSize) { return pSettings.TempPath.TrimEnd(System.IO.Path.DirectorySeparatorChar) + System.IO.Path.DirectorySeparatorChar + pPath.Replace('/', '_') + pSize.Width + "x" + pSize.Height; } private CropSize CalculateFitWidth(SizeF pSize, SizeF pMaxSize) { CropSize ret = new CropSize(); ret.Ratio = pSize.Width / pMaxSize.Width; ret.Width = pMaxSize.Width; ret.Height = pSize.Height / ret.Ratio; return ret; } private CropSize CalculateFitHeight(SizeF pSize, SizeF pMaxSize) { CropSize ret = new CropSize(); ret.Ratio = pSize.Height / pMaxSize.Height; ret.Width = pSize.Width / ret.Ratio; ret.Height = pMaxSize.Height; return ret; } private CropSize CalculatePercent(SizeF pSize, int pPercent) { CropSize ret = new CropSize(); ret.Width = (pSize.Width * pPercent) / 100; ret.Height = (pSize.Height * pPercent) / 100; return ret; } private CropSize CalculateSize(SizeF pSize, SizeF pMaxSize) { CropSize ret = CalculateFitWidth(pSize, pMaxSize); if(ret.Height > pMaxSize.Height) { ret = CalculateFitHeight(pSize, pMaxSize); } return ret; } private CropSize CalculateCrop(SizeF pSize, SizeF pMaxSize) { CropSize ret = CalculateFitWidth(pSize, pMaxSize); if(ret.Height < pMaxSize.Height) { ret = CalculateFitHeight(pSize, pMaxSize); } ret.XOffset = (pSize.Width - (pMaxSize.Width * ret.Ratio)) / 2; ret.YOffset = (pSize.Height - (pMaxSize.Height * ret.Ratio)) / 2; return ret; } void saveResizedImage(string pImagePath, string pSavePath, SizeF pMaxSize) { using(System.Drawing.Bitmap imgIn = new System.Drawing.Bitmap(pImagePath)) { SizeF _ImageSize = new SizeF(imgIn.Width, imgIn.Height); CropSize _NewSize; if(_ImageSize.Width > pMaxSize.Width || _ImageSize.Height > pMaxSize.Height) { if(_Crop) { _NewSize = CalculateCrop(_ImageSize, _MaxSize); _NewSize.Width = _MaxSize.Width; _NewSize.Height = _MaxSize.Height; } else { _NewSize = CalculateSize(_ImageSize, _MaxSize); } using(Bitmap imgOut = new Bitmap((int)_NewSize.Width, (int)_NewSize.Height, PixelFormat.Format24bppRgb)) { imgOut.SetResolution(imgIn.HorizontalResolution, imgIn.VerticalResolution); using(Graphics g = Graphics.FromImage(imgOut)) { g.InterpolationMode = InterpolationMode.HighQualityBicubic; g.DrawImage(imgIn, new RectangleF(0, 0, _NewSize.Width, _NewSize.Height), new RectangleF(_NewSize.XOffset, _NewSize.YOffset, _ImageSize.Width - (_NewSize.XOffset * 2), _ImageSize.Height - (_NewSize.YOffset * 2)), GraphicsUnit.Pixel); g.Dispose(); } imgOut.Save(pSavePath, imgIn.RawFormat); } } else { System.IO.File.Copy(pImagePath, pSavePath, true); } } } }