Example #1
0
        // Helper methods
        private static string CompileUrl(ThumbnailSettings settings)
        {
            var nvc        = settings.GetUrlParameters();
            var parameters = string.Join("&", nvc.AllKeys.Select(a => a + "=" + HttpUtility.UrlEncode(nvc[a])));

            return($"/{Constants.UrlFragment}?{parameters}");
        }
Example #2
0
        public virtual Image LoadThumbnailImage(ThumbnailSettings settings)
        {
            string fileName = settings.GetFileName(".png");

            if (!CachedImageExists(fileName))
            {
                var stream = GenerateImage(settings);

                string savePath = GetFileFullPath(fileName);
                using (var fileStream = File.Create(savePath))
                {
                    stream.Seek(0, SeekOrigin.Begin);
                    stream.CopyTo(fileStream);
                    stream.Dispose();
                    stream.Close();
                }
            }

            Image img;

            using (var bmpTemp = new Bitmap(GetFileFullPath(fileName)))
            {
                img = new Bitmap(bmpTemp);
            }

            return(img);
        }
Example #3
0
        public static ThumbnailSettings GetSettings(string backgroundColor, string foregroundColor, int fontSize)
        {
            var settings = new ThumbnailSettings();

            if (!string.IsNullOrEmpty(backgroundColor))
            {
                settings.BackgroundColor = backgroundColor;
            }
            else
            {
                var cfg = ConfigurationManager.AppSettings[Constants.AppSettings.BackgroundColor] ?? string.Empty;
                settings.BackgroundColor = string.IsNullOrEmpty(cfg) ? Constants.DefaultBackgroundColor : cfg;
            }

            if (!string.IsNullOrEmpty(foregroundColor))
            {
                settings.ForegroundColor = foregroundColor;
            }
            else
            {
                var cfg = ConfigurationManager.AppSettings[Constants.AppSettings.ForegroundColor] ?? string.Empty;
                settings.ForegroundColor = string.IsNullOrEmpty(cfg) ? Constants.DefaultForegroundColor : cfg;
            }

            if (fontSize < 0)
            {
                int.TryParse(ConfigurationManager.AppSettings[Constants.AppSettings.FontSize], out fontSize);
            }

            settings.FontSize = fontSize > 0 ? fontSize : Constants.DefaultFontSize;
            settings.Width    = Constants.DefaultWidth;
            settings.Height   = Constants.DefaultHeight;

            return(settings);
        }
Example #4
0
        public virtual Image LoadThumbnailImage(ThumbnailSettings settings)
        {
            var fileName  = settings.GetFileName(".png");
            var cachePath = GetFileFullPath(fileName);

            if (File.Exists(cachePath))
            {
                using (var bmpTemp = new Bitmap(cachePath))
                {
                    return(new Bitmap(bmpTemp));
                }
            }
            else
            {
                using (var fileStream = File.Create(cachePath))
                    using (var stream = GenerateImage(settings))
                        using (var bmpTemp = new Bitmap(stream))
                        {
                            stream.Seek(0, SeekOrigin.Begin);
                            stream.CopyTo(fileStream);
                            stream.Dispose();
                            stream.Close();

                            return(new Bitmap(bmpTemp));
                        }
            }
        }
Example #5
0
        private static Settings CreateSettings()
        {
            SettingConfigurationSection section = (SettingConfigurationSection)ConfigurationManager.GetSection(SettingConfigurationSection.SectionName);

            ApiSettings api = new ApiSettings(section.Api.Allowed, section.Api.DailyLimit);

            ThumbnailSettings thumbnail = new ThumbnailSettings(section.Thumbnail.ApiKey, section.Thumbnail.Endpoint);

            GoogleSafeBrowsingSettings google = new GoogleSafeBrowsingSettings(section.Google.ApiKey, section.Google.Endpoint, HttpContext.Current.Server.MapPath(section.Google.PhishingFile), HttpContext.Current.Server.MapPath(section.Google.MalwareFile));

            TwitterSettings twitter = null;

            if (section.Twitter != null)
            {
                twitter = new TwitterSettings(section.Twitter.UserName, section.Twitter.Password, section.Twitter.Endpoint, section.Twitter.MessageTemplate, section.Twitter.MaximumMessageLength, section.Twitter.Recipients.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries));
            }

            IList <User> defaultUsers = new List <User>();

            foreach (DefaultUserConfigurationElement configuration in section.DefaultUsers)
            {
                defaultUsers.Add(new User {
                    Name = configuration.Name, Email = configuration.Email, Role = configuration.Role
                });
            }

            Settings settings = new Settings(section.RedirectPermanently, section.UrlPerPage, section.BaseType, api, thumbnail, google, twitter, defaultUsers);

            return(settings);
        }
Example #6
0
 public ThumbnailSettings GetThumbnailSettings()
 {
     if (_thumbnailSettings == null)
     {
         _thumbnailSettings = this.GetSettings <ThumbnailSettings>("ThumbnailSettings") ?? ThumbnailSettings.Default;
     }
     return(_thumbnailSettings);
 }
Example #7
0
        public ActionResult GenerateThumbnail(ThumbnailSettings settings)
        {
            if (!CheckValidFormatHtmlColor(settings.BackgroundColor) || !CheckValidFormatHtmlColor(settings.ForegroundColor))
            {
                throw (new Exception("Unknown foreground or background color"));
            }

            var image = _thumbnailService.LoadThumbnailImage(settings);

            return(new ImageResult()
            {
                Image = image, ImageFormat = ImageFormat.Png
            });
        }
Example #8
0
        protected virtual MemoryStream GenerateImage(ThumbnailSettings settings)
        {
            PrivateFontCollection fonts;
            FontFamily            family;

            if (settings.UseEmbeddedFont)
            {
                family = LoadFontFamilyFromEmbeddedResource(settings.EmbeddedFont, out fonts);
            }
            else
            {
                family = LoadFontFamilyFromDisk(settings.CustomFontName, out fonts);
            }


            var cc = new ColorConverter();
            var bg = (Color)cc.ConvertFrom(settings.BackgroundColor);
            var fg = (Color)cc.ConvertFrom(settings.ForegroundColor);

            var stream = new MemoryStream();

            using (var font = new Font(family, settings.FontSize))
                using (var bitmap = new Bitmap(settings.Width, settings.Height, PixelFormat.Format24bppRgb))
                    using (var g = Graphics.FromImage(bitmap))
                    {
                        g.TextRenderingHint = TextRenderingHint.AntiAliasGridFit;
                        g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBilinear;
                        g.PixelOffsetMode   = System.Drawing.Drawing2D.PixelOffsetMode.HighQuality;
                        g.SmoothingMode     = System.Drawing.Drawing2D.SmoothingMode.HighQuality;

                        g.Clear(bg);

                        StringFormat format1 = new StringFormat(StringFormatFlags.NoClip);

                        format1.LineAlignment = StringAlignment.Center;
                        format1.Alignment     = StringAlignment.Center;

                        Rectangle displayRectangle = new Rectangle(new Point(0, 0), new Size(settings.Width, settings.Height));
                        string    chr = char.ConvertFromUtf32(settings.Character);
                        g.DrawString(chr, font, new SolidBrush(fg), displayRectangle, format1);
                        bitmap.Save(stream, ImageFormat.Png);

                        family.Dispose();
                        fonts.Dispose();
                    }

            return(stream);
        }
Example #9
0
        public ThumbnailIconControllerFixture()
        {
            var partialDirectory = $"[appDataPath]\\thumb_cache\\{Guid.NewGuid()}\\";

            ConfigurationManager.AppSettings["FontThumbnail.CachePath"] = partialDirectory;
            _temporaryDirectory = VirtualPathUtilityEx.RebasePhysicalPath(partialDirectory);

            Directory.CreateDirectory(_temporaryDirectory);

            var service = new FontThumbnailService();

            Controller = new ThumbnailIconController(service);
            Settings   = new ThumbnailSettings
            {
                FontSize        = Constants.DefaultFontSize,
                BackgroundColor = Constants.DefaultBackgroundColor,
                ForegroundColor = Constants.DefaultForegroundColor,
                Height          = Constants.DefaultHeight,
                Width           = Constants.DefaultWidth
            };
        }
Example #10
0
        internal virtual MemoryStream GenerateImage(ThumbnailSettings settings)
        {
            FontFamily family;

            if (settings.UseEmbeddedFont)
            {
                family = LoadFontFamilyFromEmbeddedResource(settings.EmbeddedFont);
            }
            else
            {
                family = LoadFontFamilyFromDisk(settings.CustomFontName);
            }

            var cc = new ColorConverter();
            var bg = (Color)cc.ConvertFrom(settings.BackgroundColor);
            var fg = (Color)cc.ConvertFrom(settings.ForegroundColor);

            var stream = new MemoryStream();

            using (var font = new Font(family, settings.FontSize))
                using (var bitmap = new Bitmap(settings.Width, settings.Height, PixelFormat.Format24bppRgb))
                    using (var g = Graphics.FromImage(bitmap))
                    {
                        g.TextRenderingHint = TextRenderingHint.AntiAliasGridFit;
                        g.InterpolationMode = InterpolationMode.HighQualityBilinear;
                        g.PixelOffsetMode   = PixelOffsetMode.HighQuality;
                        g.SmoothingMode     = SmoothingMode.HighQuality;

                        g.Clear(bg);

                        switch (settings.Rotate)
                        {
                        case Rotations.Rotate90:
                        case Rotations.Rotate180:
                        case Rotations.Rotate270:
                            g.TranslateTransform(settings.Width / 2, settings.Height / 2);
                            g.RotateTransform((int)settings.Rotate);
                            g.TranslateTransform(-(settings.Width / 2), -(settings.Height / 2));
                            break;
                        }

                        var format1 = new StringFormat(StringFormatFlags.NoClip);
                        using (var format = new StringFormat(StringFormatFlags.NoClip))
                        {
                            format.LineAlignment = StringAlignment.Center;
                            format.Alignment     = StringAlignment.Center;
                            var displayRectangle = new Rectangle(new Point(0, 0), new Size(settings.Width, settings.Height));
                            var chr = char.ConvertFromUtf32(settings.Character);
                            using (var brush = new SolidBrush(fg))
                            {
                                g.DrawString(chr, font, brush, displayRectangle, format);
                            }
                        }

                        switch (settings.Rotate)
                        {
                        case Rotations.FlipHorizontal:
                            bitmap.RotateFlip(RotateFlipType.RotateNoneFlipX);
                            break;

                        case Rotations.FlipVertical:
                            bitmap.RotateFlip(RotateFlipType.RotateNoneFlipY);
                            break;
                        }
                        bitmap.Save(stream, ImageFormat.Png);
                    }

            family.Dispose();
            return(stream);
        }