<%@ WebHandler Language="C#" Class="UploadHandler" %> using System; using System.Web; using System.IO; using System.Drawing; using System.Drawing.Drawing2D; public class UploadHandler : IHttpHandler { public void ProcessRequest (HttpContext context) { //string path = context.Request["path"]; //FileInfo fn = new FileInfo(path); //fn.CopyTo(context.Server.MapPath("Image/"+fn.Name)); //context.Response.Write(fn.Name+":"+fn.Extension); if (context.Request.Files.Count > 0) { HttpFileCollection files = context.Request.Files; for (int i = 0; i < files.Count; i++) { HttpPostedFile file = files[i]; string fname,path; if (HttpContext.Current.Request.Browser.Browser.ToUpper() == "IE" || HttpContext.Current.Request.Browser.Browser.ToUpper() == "INTERNETEXPLORER") { string[] testfiles = file.FileName.Split(new char[] { '\\' }); fname = testfiles[testfiles.Length - 1]; } else { Random rnd = new Random(); fname = rnd.Next(99, 99999) + file.FileName.Replace(' ', '-'); } //fname = Path.Combine(context.Server.MapPath("AgentImage/"), fname); path = Path.Combine(context.Server.MapPath("AgentImage/"), fname); //----------Resize Function----------// System.Drawing.Image imageToBeResized = System.Drawing.Image.FromStream(file.InputStream); int imageHeight = imageToBeResized.Height; int imageWidth = imageToBeResized.Width; int maxHeight = 300; int maxWidth = 300; if (imageWidth > maxWidth || imageWidth < maxWidth) { imageWidth = 300; } if (imageHeight > maxHeight || imageHeight < maxHeight) { imageHeight = 300; } var thumbnailImg = new Bitmap(imageWidth, imageHeight); //Bitmap bitmap = new Bitmap(imageToBeResized, imageWidth, imageHeight); var thumbGraph = Graphics.FromImage(thumbnailImg); thumbGraph.CompositingQuality = CompositingQuality.HighQuality; thumbGraph.SmoothingMode = SmoothingMode.HighQuality; thumbGraph.InterpolationMode = InterpolationMode.HighQualityBicubic; var imageRectangle = new Rectangle(0, 0, imageWidth, imageHeight); thumbGraph.DrawImage(imageToBeResized, imageRectangle); thumbnailImg.Save(path, imageToBeResized.RawFormat); //file.SaveAs(fname); context.Response.Write("AgentImage/"+fname); } } //context.Response.ContentType = "text/plain"; } public bool IsReusable { get { return false; } } }