internal static void OptimisedImageResponse(HttpContext context) { var size = GetRequiredSize(context); if (size.Width == 0 && size.Height == 0) { var match = WebProvider.GetMatch(context.Request); // Get the screen width and height. int value; if (match["ScreenPixelsWidth"] != null && match["ScreenPixelsWidth"].Count > 0 && int.TryParse(match["ScreenPixelsWidth"][0].ToString(), out value)) { size.Width = value; } if (match["ScreenPixelsHeight"] != null && match["ScreenPixelsHeight"].Count > 0 && int.TryParse(match["ScreenPixelsHeight"][0].ToString(), out value)) { size.Height = value; } // Use the larger of the two values as the width as there is no // way of knowing if the device is in landscape or portrait // orientation. size.Width = Math.Max(size.Width, size.Height); size.Height = 0; } // Ensure the size is not larger than the maximum parameters. ResolveSize(ref size); if (size.Width > 0 || size.Height > 0) { // Get the files and paths involved in the caching. var cachedFileVirtualPath = Image.Support.GetCachedResponseFile(context.Request, size); var cachedFilePhysicalPath = context.Server.MapPath(cachedFileVirtualPath); var cachedFile = new FileInfo(cachedFilePhysicalPath); var sourceFile = context.Server.MapPath(context.Request.AppRelativeCurrentExecutionFilePath); EventLog.Debug(String.Format( "Image processor is responding with image '{0}' of width '{1}' and height '{2}'", sourceFile, size.Width, size.Height)); // If the cached file doesn't exist or is out of date // then create a new cached file and serve this in response // to the request by rewriting the requested URL to the // static file. if (cachedFile.Exists == false || cachedFile.LastWriteTimeUtc < File.GetLastWriteTimeUtc(sourceFile)) { // Check the directory exists? if (cachedFile.Directory.Exists == false) { Directory.CreateDirectory(cachedFile.DirectoryName); } // Shrink the image to the cache file path. Use a bit depth of 32 pixels // as the image cache is not aware of the specific devices bit depth, only // the requested screen size. var processor = new Image.Processor(32); var source = new FileInfo(sourceFile); processor.Width = size.Width; processor.Height = size.Height; using (var cachedStream = new MemoryStream()) { try { // Make sure the source stream is closed when the shrinking // process has been completed. using (var sourceStream = source.OpenRead()) { processor.Shrink( sourceStream, cachedStream); } // Some times the GDI can produce larger files than the original. // Check for this to ensure the image is smaller. if (cachedStream.Length < source.Length) { // Use the cache stream for the new file. using (var fileStream = File.Create(cachedFilePhysicalPath)) { cachedStream.Position = 0; cachedStream.CopyTo(fileStream); } } else { // Copy the original file into the cache to avoid doing // this again in the future. source.CopyTo(cachedFilePhysicalPath); } } catch (Exception ex) { EventLog.Warn(String.Format( "Source file '{0}' generated exception '{1}' shrinking to '{2}' by '{3}'.", sourceFile, ex, processor.Width, processor.Height)); } } } if (File.Exists(cachedFilePhysicalPath)) { context.RewritePath(cachedFileVirtualPath, true); } else { context.RewritePath(context.Request.AppRelativeCurrentExecutionFilePath); } } }
internal static void OptimisedImageResponse(HttpContext context) { var size = GetRequiredSize(context); if (size.Width == 0 && size.Height == 0) { var match = WebProvider.GetMatch(context.Request); // Get the screen width and height. int value; if (match["ScreenPixelsWidth"] != null && match["ScreenPixelsWidth"].Count > 0 && int.TryParse(match["ScreenPixelsWidth"][0].ToString(), out value)) size.Width = value; if (match["ScreenPixelsHeight"] != null && match["ScreenPixelsHeight"].Count > 0 && int.TryParse(match["ScreenPixelsHeight"][0].ToString(), out value)) size.Height = value; // Use the larger of the two values as the width as there is no // way of knowing if the device is in landscape or portrait // orientation. size.Width = Math.Max(size.Width, size.Height); size.Height = 0; } // Ensure the size is not larger than the maximum parameters. ResolveSize(ref size); if (size.Width > 0 || size.Height > 0) { // Get the files and paths involved in the caching. var cachedFileVirtualPath = Image.Support.GetCachedResponseFile(context.Request, size); var cachedFilePhysicalPath = context.Server.MapPath(cachedFileVirtualPath); var cachedFile = new FileInfo(cachedFilePhysicalPath); var sourceFile = context.Server.MapPath(context.Request.AppRelativeCurrentExecutionFilePath); EventLog.Debug(String.Format( "Image processor is responding with image '{0}' of width '{1}' and height '{2}'", sourceFile, size.Width, size.Height)); // If the cached file doesn't exist or is out of date // then create a new cached file and serve this in response // to the request by rewriting the requested URL to the // static file. if (cachedFile.Exists == false || cachedFile.LastWriteTimeUtc < File.GetLastWriteTimeUtc(sourceFile)) { // Check the directory exists? if (cachedFile.Directory.Exists == false) Directory.CreateDirectory(cachedFile.DirectoryName); // Shrink the image to the cache file path. Use a bit depth of 32 pixels // as the image cache is not aware of the specific devices bit depth, only // the requested screen size. var processor = new Image.Processor(32); var source = new FileInfo(sourceFile); processor.Width = size.Width; processor.Height = size.Height; using (var cachedStream = new MemoryStream()) { try { // Make sure the source stream is closed when the shrinking // process has been completed. using (var sourceStream = source.OpenRead()) { processor.Shrink( sourceStream, cachedStream); } // Some times the GDI can produce larger files than the original. // Check for this to ensure the image is smaller. if (cachedStream.Length < source.Length) { // Use the cache stream for the new file. using (var fileStream = File.Create(cachedFilePhysicalPath)) { cachedStream.Position = 0; cachedStream.CopyTo(fileStream); } } else { // Copy the original file into the cache to avoid doing // this again in the future. source.CopyTo(cachedFilePhysicalPath); } } catch (Exception ex) { EventLog.Warn(String.Format( "Source file '{0}' generated exception '{1}' shrinking to '{2}' by '{3}'.", sourceFile, ex, processor.Width, processor.Height)); } } } if (File.Exists(cachedFilePhysicalPath)) context.RewritePath(cachedFileVirtualPath, true); else context.RewritePath(context.Request.AppRelativeCurrentExecutionFilePath); } }