// GET: api/Values public HttpResponseMessage Get(string p) { HttpResponseMessage result = null; if (_disposed) { throw new ObjectDisposedException("ThumbHandlerState"); } NameValueCollection routeValues = HttpContext.Current.Request.QueryString; if (!String.IsNullOrEmpty(p)) { var serializer = new JavaScriptSerializer(); ImageProperties imageProperties = serializer.Deserialize <ImageProperties>(p); string path = "~" + imageProperties.Path + "/" + imageProperties.Name + imageProperties.Type; using (Thumbnails thumbnails = new Thumbnails(path)) { thumbnails.maximum = new Dimension <int> { Width = imageProperties.Dimensions.Width, Height = imageProperties.Dimensions.Height }; thumbnails.Create(out bitMap); MemoryStream ms = new MemoryStream(); bitMap.Save(ms, imageProperties.ContentType()); ms.Position = 0; result = new HttpResponseMessage(HttpStatusCode.OK); result.Content = new StreamContent(ms); result.Content.Headers.ContentLength = ms.Length; result.Content.Headers.ContentType = new MediaTypeHeaderValue("image/" + imageProperties.ContentType().ToString().ToLower()); } } return(result); }
public IHttpHandler GetHttpHandler(RequestContext requestContext) { if (_disposed) { throw new ObjectDisposedException("ThumbHandlerState"); } NameValueCollection routeValues = requestContext.HttpContext.Request.QueryString; if (!String.IsNullOrEmpty(routeValues.Get("p"))) { var serializer = new JavaScriptSerializer(); ImageProperties imageProperties = serializer.Deserialize <ImageProperties>(routeValues.Get("p")); string path = "~" + imageProperties.Path + "/" + imageProperties.Name + imageProperties.Type; using (Thumbnails thumbnails = new Thumbnails(path)) { thumbnails.maximum = new Dimension <int> { Width = imageProperties.Dimensions.Width, Height = imageProperties.Dimensions.Height }; thumbnails.Create(out bitMap); requestContext.HttpContext.Response.Clear(); requestContext.HttpContext.Response.BufferOutput = true; bitMap.Save(requestContext.HttpContext.Response.OutputStream, imageProperties.ContentType()); requestContext.HttpContext.Response.Flush(); requestContext.HttpContext.Response.Close(); } } return(null); }