From f380d7a092ef474754400647debbc3d90e8451a0 Mon Sep 17 00:00:00 2001 From: Luke Pulverenti Date: Sat, 21 Sep 2013 11:06:00 -0400 Subject: added percent played overlay --- .../Drawing/ImageProcessor.cs | 64 ++++++++++++++++++---- .../Drawing/PercentPlayedDrawer.cs | 36 ++++++++++++ .../MediaBrowser.Server.Implementations.csproj | 1 + 3 files changed, 91 insertions(+), 10 deletions(-) create mode 100644 MediaBrowser.Server.Implementations/Drawing/PercentPlayedDrawer.cs (limited to 'MediaBrowser.Server.Implementations') diff --git a/MediaBrowser.Server.Implementations/Drawing/ImageProcessor.cs b/MediaBrowser.Server.Implementations/Drawing/ImageProcessor.cs index 6458435ba..1f7361d2f 100644 --- a/MediaBrowser.Server.Implementations/Drawing/ImageProcessor.cs +++ b/MediaBrowser.Server.Implementations/Drawing/ImageProcessor.cs @@ -109,7 +109,7 @@ namespace MediaBrowser.Server.Implementations.Drawing var quality = options.Quality ?? 90; - var cacheFilePath = GetCacheFilePath(originalImagePath, newSize, quality, dateModified, options.OutputFormat, options.Indicator); + var cacheFilePath = GetCacheFilePath(originalImagePath, newSize, quality, dateModified, options.OutputFormat, options.Indicator, options.PercentPlayed, options.BackgroundColor); try { @@ -173,9 +173,11 @@ namespace MediaBrowser.Server.Implementations.Drawing thumbnailGraph.PixelOffsetMode = PixelOffsetMode.HighQuality; thumbnailGraph.CompositingMode = CompositingMode.SourceOver; + SetBackgroundColor(thumbnailGraph, options); + thumbnailGraph.DrawImage(originalImage, 0, 0, newWidth, newHeight); - DrawIndicator(thumbnailGraph, newWidth, newHeight, options.Indicator); + DrawIndicator(thumbnailGraph, newWidth, newHeight, options.Indicator, options.PercentPlayed); var outputFormat = GetOutputFormat(originalImage, options.OutputFormat); @@ -206,9 +208,41 @@ namespace MediaBrowser.Server.Implementations.Drawing } } - private WatchedIndicatorDrawer _watchedDrawer; + /// + /// Sets the color of the background. + /// + /// The graphics. + /// The options. + private void SetBackgroundColor(Graphics graphics, ImageProcessingOptions options) + { + var color = options.BackgroundColor; + + if (!string.IsNullOrEmpty(color)) + { + Color drawingColor; + + try + { + drawingColor = ColorTranslator.FromHtml(color); + } + catch + { + drawingColor = ColorTranslator.FromHtml("#" + color); + } - private void DrawIndicator(Graphics graphics, int imageWidth, int imageHeight, ImageOverlay? indicator) + graphics.Clear(drawingColor); + } + } + + /// + /// Draws the indicator. + /// + /// The graphics. + /// Width of the image. + /// Height of the image. + /// The indicator. + /// The percent played. + private void DrawIndicator(Graphics graphics, int imageWidth, int imageHeight, ImageOverlay? indicator, int percentPlayed) { if (!indicator.HasValue) { @@ -217,13 +251,17 @@ namespace MediaBrowser.Server.Implementations.Drawing try { - if (indicator.Value == ImageOverlay.Watched) + if (indicator.Value == ImageOverlay.Played) { - _watchedDrawer = _watchedDrawer ?? (_watchedDrawer = new WatchedIndicatorDrawer()); + var currentImageSize = new Size(imageWidth, imageHeight); + new WatchedIndicatorDrawer().Process(graphics, currentImageSize); + } + if (indicator.Value == ImageOverlay.PercentPlayed) + { var currentImageSize = new Size(imageWidth, imageHeight); - _watchedDrawer.Process(graphics, currentImageSize); + new PercentPlayedDrawer().Process(graphics, currentImageSize, percentPlayed); } } catch (Exception ex) @@ -350,7 +388,7 @@ namespace MediaBrowser.Server.Implementations.Drawing /// /// Gets the cache file path based on a set of parameters /// - private string GetCacheFilePath(string originalPath, ImageSize outputSize, int quality, DateTime dateModified, ImageOutputFormat format, ImageOverlay? overlay) + private string GetCacheFilePath(string originalPath, ImageSize outputSize, int quality, DateTime dateModified, ImageOutputFormat format, ImageOverlay? overlay, int percentPlayed, string backgroundColor) { var filename = originalPath; @@ -364,12 +402,18 @@ namespace MediaBrowser.Server.Implementations.Drawing if (format != ImageOutputFormat.Original) { - filename += "format=" + format; + filename += "f=" + format; } if (overlay.HasValue) { - filename += "overlay=" + overlay.Value; + filename += "o=" + overlay.Value; + filename += "p=" + percentPlayed; + } + + if (!string.IsNullOrEmpty(backgroundColor)) + { + filename += "b=" + backgroundColor; } return GetCachePath(_resizedImageCachePath, filename, Path.GetExtension(originalPath)); diff --git a/MediaBrowser.Server.Implementations/Drawing/PercentPlayedDrawer.cs b/MediaBrowser.Server.Implementations/Drawing/PercentPlayedDrawer.cs new file mode 100644 index 000000000..355824458 --- /dev/null +++ b/MediaBrowser.Server.Implementations/Drawing/PercentPlayedDrawer.cs @@ -0,0 +1,36 @@ +using System.Drawing; +using System.Globalization; + +namespace MediaBrowser.Server.Implementations.Drawing +{ + public class PercentPlayedDrawer + { + private const int IndicatorWidth = 80; + private const int IndicatorHeight = 50; + private const int FontSize = 30; + private readonly CultureInfo _usCulture = new CultureInfo("en-US"); + + public void Process(Graphics graphics, Size imageSize, int percent) + { + var x = imageSize.Width - IndicatorWidth; + + using (var backdroundBrush = new SolidBrush(Color.FromArgb(225, 102, 192, 16))) + { + graphics.FillRectangle(backdroundBrush, x, 0, IndicatorWidth, IndicatorHeight); + + var text = string.Format("{0}%", percent.ToString(_usCulture)); + + x = imageSize.Width - (percent < 10 ? 66 : 75); + + using (var font = new Font(FontFamily.GenericSansSerif, FontSize, FontStyle.Regular, GraphicsUnit.Pixel)) + { + using (var fontBrush = new SolidBrush(Color.White)) + { + graphics.DrawString(text, font, fontBrush, x, 6); + } + } + } + + } + } +} diff --git a/MediaBrowser.Server.Implementations/MediaBrowser.Server.Implementations.csproj b/MediaBrowser.Server.Implementations/MediaBrowser.Server.Implementations.csproj index 80fee7d97..f84c02c1f 100644 --- a/MediaBrowser.Server.Implementations/MediaBrowser.Server.Implementations.csproj +++ b/MediaBrowser.Server.Implementations/MediaBrowser.Server.Implementations.csproj @@ -114,6 +114,7 @@ + -- cgit v1.2.3