using System; using System.Data; using System.Configuration; using System.Collections; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; using Atalasoft.Imaging.ImageProcessing; using Atalasoft.Imaging; using Atalasoft.Imaging.ImageProcessing.Effects; using System.IO; using System.Drawing; using Atalasoft.Imaging.Codec; using Atalasoft.Imaging.Drawing; public partial class Slider_Tests : System.Web.UI.Page { private Size defaultImageSize = new Size(320, 240); protected void Page_Load(object sender, EventArgs e) { } protected override void OnInit(EventArgs e) { base.OnInit(e); string imgUrl = this.Request.QueryString["img"]; string gammaStr = this.Request.QueryString["gamma"]; // If these queries do not exist, assume this is the initial rendering of the page if (!string.IsNullOrEmpty(imgUrl) && !string.IsNullOrEmpty(gammaStr)) { // They do exist, stream the new image StreamImage(imgUrl, gammaStr); } } /// /// Streams the new image to the browser /// /// Relative url to the image /// string representing the gamma value, (integer from 0 to 200), in percent of the original image private void StreamImage(string imgUrl, string gammaStr) { AtalaImage imageToStream = null; try { // check that the file exists where we think it should be if (File.Exists(Page.MapPath(imgUrl))) { int gamma = int.Parse(gammaStr); imageToStream = ApplyGammaToImage(Page.MapPath(imgUrl), gamma); } // if the image is null, something went wrong if (imageToStream == null) { imageToStream = new AtalaImage(defaultImageSize.Width, defaultImageSize.Height, PixelFormat.Pixel32bppBgra, Color.AntiqueWhite); Canvas imgCanvas = new Canvas(imageToStream); // Show the user that something went wrong imgCanvas.DrawText("Unable to load the image at path:\n" + imgUrl, new Rectangle(Point.Empty, defaultImageSize), new Font("Arial", 14), new SolidFill(Color.Red)); } // Clear the response stream, and stream down a Jpeg this.Response.Clear(); this.Response.Cache.SetCacheability(HttpCacheability.Public); this.Response.ContentType = "image/jpeg"; imageToStream.Save(this.Context.Response.OutputStream, new JpegEncoder(65, 0, true), null); // End the stream early, so we don't process the whole page again this.Response.End(); } finally { if (imageToStream != null) imageToStream.Dispose(); } } /// /// /// /// /// /// private AtalaImage ApplyGammaToImage(string origPath, int gamma) { AtalaImage img = null; try { // Command used to apply the gamma GammaCommand gammaCmd = new GammaCommand(); // This command doesn't accept 0, so we get close gammaCmd.GammaLevel = (gamma > 0) ? (double)gamma / 100 : .02; // We use a thumbnail here because it's too costly to use the whole image Thumbnail thumb = new Thumbnail(defaultImageSize); img = thumb.Create(origPath, 0); // We need to make sure that this image is a color depth that allows this command if (img.PixelFormat != PixelFormat.Pixel24bppBgr) img = Apply(new ChangePixelFormatCommand(PixelFormat.Pixel24bppBgr), img, false); // Apply the command, and return the new image, we need to make a copy because we're disposing the original return Apply(gammaCmd, img, true); } finally { if (img != null) img.Dispose(); } } /// /// Applies a command to a given image and makes a new copy when specified /// /// ImageCommand to apply to the given image /// AtalaImage to apply the given command /// Forces a copy of the AtalaImage to be created if the command can be applied in place /// private AtalaImage Apply(ImageCommand cmd, AtalaImage img, bool copy) { if (cmd.InPlaceProcessing) { if (copy) img = (AtalaImage)img.Clone(); } ImageResults results = cmd.Apply(img); if (!results.IsImageSourceImage) { img.Dispose(); img = results.Image; } return img; } }