Esempio n. 1
0
        private void DrawWatermark(RenderParameters parameters, Graphics graphics, Image image)
        {
            using (var watermark = Image.FromFile(parameters.FullWatermarkImageFilePath))
            {
                var padding              = 2;
                var width                = parameters.WatermarkImageWidth;
                var height               = parameters.WatermarkImageHeight;
                var sourceRectangle      = new Rectangle(0, 0, watermark.Width, watermark.Height);
                var destinationRectangle = new Rectangle(image.Width - width - padding, image.Height - height - padding, width, height);

                graphics.DrawImage(watermark, destinationRectangle, sourceRectangle, GraphicsUnit.Pixel);

                var font = new Font(parameters.WatermarkFont, parameters.WatermarkFontSize);

                var textSize = graphics.MeasureString(parameters.WatermarkText, font);

                var bounds = new Rectangle(image.Width - width - (int)Math.Ceiling(textSize.Width), image.Height - (int)Math.Ceiling(textSize.Height), image.Width, (int)Math.Ceiling(textSize.Height));

                graphics.CompositingMode = CompositingMode.SourceOver;

                var stroke = new SolidBrush(Color.FromArgb(150, parameters.WatermarkStroke));
                var fill   = new SolidBrush(Color.FromArgb(150, parameters.WatermarkFill));

                DrawText(graphics, parameters.WatermarkText, font, parameters.WatermarkFontStyle, parameters.WatermarkFontSize, stroke, parameters.WatermarkStrokeWidth, fill, StringFormat.GenericTypographic, bounds);

                graphics.CompositingMode = CompositingMode.SourceCopy;
            }
        }
Esempio n. 2
0
        private void DrawText(RenderParameters parameters, Graphics graphics, Image image, LineParameters line, Rectangle bounds, StringFormat stringFormat)
        {
            var done       = false;
            var fontSize   = line.FontSize;
            var fontFamily = FindFont(parameters, line.Font);

            while (!done)
            {
                var font = new Font(fontFamily, fontSize, FontStyle.Regular);

                var size = graphics.MeasureString(line.Text, font, bounds.Width);

                if (size.Height > bounds.Size.Height && fontSize > 10)
                {
                    fontSize -= 2;
                    continue;
                }

                var stroke = new SolidBrush(line.Stroke);
                var fill   = new SolidBrush(line.Fill);

                DrawText(graphics, line.Text, font, line.FontStyle, fontSize, stroke, line.StrokeWidth, fill, stringFormat, bounds);

                if (parameters.DebugMode)
                {
                    DrawBoxes(graphics, image.Width, image.Height, bounds);
                }

                done = true;
            }
        }
Esempio n. 3
0
        private void DrawText(RenderParameters parameters, Graphics graphics, Image image, LineParameters line, Rectangle bounds, StringFormat stringFormat)
        {
            var done = false;
            var fontSize = line.FontSize;
            var fontFamily = FindFont(parameters, line.Font);

            while (!done)
            {
                var font = new Font(fontFamily, fontSize, FontStyle.Regular);

                var size = graphics.MeasureString(line.Text, font, bounds.Width);
                
                if (size.Height > bounds.Size.Height && fontSize > 10)
                {
                    fontSize -= 2;
                    continue;
                }

                var stroke = new SolidBrush(line.Stroke);
                var fill = new SolidBrush(line.Fill);

                DrawText(graphics, line.Text, font, line.FontStyle, fontSize, stroke, line.StrokeWidth, fill, stringFormat, bounds);

                if (parameters.DebugMode)
                {
                    DrawBoxes(graphics, image.Width, image.Height, bounds);
                }

                done = true;
            }
        }
Esempio n. 4
0
        public byte[] Render(RenderParameters parameters)
        {
            using (var image = Image.FromFile(parameters.FullImagePath))
                using (var graphics = Graphics.FromImage(image))
                {
                    DrawWatermark(parameters, graphics, image);

                    foreach (var line in parameters.Lines)
                    {
                        var maxHeightPercent = line.HeightPercent;
                        var maxHeight        = (int)Math.Ceiling(image.Height * (maxHeightPercent / 100));
                        var bounds           = line.Bounds ?? new Rectangle(0, 0, image.Width, maxHeight);
                        var stringFormat     = new StringFormat(StringFormat.GenericTypographic);

                        stringFormat.Alignment     = line.TextAlignment;
                        stringFormat.LineAlignment = StringAlignment.Near;

                        if (line.HugBottom)
                        {
                            stringFormat.LineAlignment = StringAlignment.Far;
                            bounds.Y = image.Height - bounds.Height - 1;
                        }

                        DrawText(parameters, graphics, image, line, bounds, stringFormat);
                    }

                    using (var memoryStream = new MemoryStream())
                    {
                        image.Save(memoryStream, image.RawFormat);

                        return(memoryStream.ToArray());
                    }
                }
        }
Esempio n. 5
0
        private void DrawWatermark(RenderParameters parameters, Graphics graphics, Image image)
        {
            using (var watermark = Image.FromFile(parameters.FullWatermarkImageFilePath))
            {
                var padding = 2;
                var width = parameters.WatermarkImageWidth;
                var height = parameters.WatermarkImageHeight;
                var sourceRectangle = new Rectangle(0, 0, watermark.Width, watermark.Height);
                var destinationRectangle = new Rectangle(image.Width - width - padding, image.Height - height - padding, width, height);

                graphics.DrawImage(watermark, destinationRectangle, sourceRectangle, GraphicsUnit.Pixel);

                var font = new Font(parameters.WatermarkFont, parameters.WatermarkFontSize);

                var textSize = graphics.MeasureString(parameters.WatermarkText, font);

                var bounds = new Rectangle(image.Width - width - (int)Math.Ceiling(textSize.Width), image.Height - (int)Math.Ceiling(textSize.Height), image.Width, (int)Math.Ceiling(textSize.Height));

                graphics.CompositingMode = CompositingMode.SourceOver;

                var stroke = new SolidBrush(Color.FromArgb(150, parameters.WatermarkStroke));
                var fill = new SolidBrush(Color.FromArgb(150, parameters.WatermarkFill));
                
                DrawText(graphics, parameters.WatermarkText, font, parameters.WatermarkFontStyle, parameters.WatermarkFontSize, stroke, parameters.WatermarkStrokeWidth, fill, StringFormat.GenericTypographic, bounds);

                graphics.CompositingMode = CompositingMode.SourceCopy;
            }
        }
Esempio n. 6
0
        public byte[] Render(RenderParameters parameters)
        {
            using (var image = Image.FromFile(parameters.FullImagePath))
            using (var graphics = Graphics.FromImage(image))
            {
                DrawWatermark(parameters, graphics, image);

                foreach (var line in parameters.Lines)
                {
                    var maxHeightPercent = line.HeightPercent;
                    var maxHeight = (int)Math.Ceiling(image.Height * (maxHeightPercent / 100));
                    var bounds = line.Bounds ?? new Rectangle(0, 0, image.Width, maxHeight);
                    var stringFormat = new StringFormat(StringFormat.GenericTypographic);

                    stringFormat.Alignment = line.TextAlignment;
                    stringFormat.LineAlignment = StringAlignment.Near;

                    if (line.HugBottom)
                    {
                        stringFormat.LineAlignment = StringAlignment.Far;
                        bounds.Y = image.Height - bounds.Height - 1;
                    }
                    
                    DrawText(parameters, graphics, image, line, bounds, stringFormat);
                }
                  
                using (var memoryStream = new MemoryStream())
                {
                    image.Save(memoryStream, image.RawFormat);

                    return memoryStream.ToArray();
                }
            }
        }
Esempio n. 7
0
        private static FontFamily FindFont(RenderParameters parameters, string font)
        {
            var fontFamily = parameters.PrivateFonts.Families.FirstOrDefault(f => f.Name == font)
                             ?? FontFamily.Families.FirstOrDefault(f => f.Name == font);

            if (fontFamily == null)
            {
                throw new ArgumentException(string.Format("Font {0} could not be found", font));
            }

            return(fontFamily);
        }
Esempio n. 8
0
        private static FontFamily FindFont(RenderParameters parameters, string font)
        {
            var fontFamily = parameters.PrivateFonts.Families.FirstOrDefault(f => f.Name == font)
                             ?? FontFamily.Families.FirstOrDefault(f => f.Name == font);

            if (fontFamily == null)
            {
                throw new ArgumentException(string.Format("Font {0} could not be found", font));
            }

            return fontFamily;
        }
Esempio n. 9
0
        public ActionResult Make()
        {
            var url = Request.ServerVariables["HTTP_URL"];
            var memeRequest = MemeRequest.FromUrl(url, Server);
            var meme = MemeUtilities.FindMeme(GlobalMemeConfiguration.Memes, memeRequest.Name);
            if (meme == null)
            {
                // TODO: update this flow to return a proper HTTP 404 code, too
                meme = GlobalMemeConfiguration.NotFoundMeme;
                memeRequest.Lines = new List<string> { "404", "Y U NO USE VALID MEME NAME?" };
            }

            // if we want to do this earlier in the process consider
            // if we still need to worry about png vs. jpg. The browser
            // probably doesn't care, but it might be weird if the extension
            // doesn't match the mimetype
            var hasExtension = UrlExtension.IsMatch(url);
            if (!hasExtension)
            {
                return Redirect(url + Path.GetExtension(meme.ImageFileName));
            }

            var renderParameters = new RenderParameters
            {
                FullImagePath = HttpContext.Server.MapPath(meme.ImagePath),
                DebugMode = memeRequest.IsDebugMode,
                FullWatermarkImageFilePath = HttpContext.Server.MapPath("~/Content/UpBoatWatermark.png"),
                WatermarkImageHeight = 25,
                WatermarkImageWidth = 25,
                WatermarkText = "upboat.me",
                WatermarkFont = "Arial",
                WatermarkFontStyle = FontStyle.Regular,
                WatermarkFontSize = 9,
                WatermarkStroke = Color.Black,
                WatermarkFill = Color.White,
                WatermarkStrokeWidth = 1,
                PrivateFonts = MemeConfig.PrivateFontCollection,
                Lines = meme.Lines.Select(l => new LineParameters
                {
                    Bounds = l.Bounds,
                    DoForceTextToAllCaps = l.DoForceTextToAllCaps,
                    Fill = l.Fill,
                    Font = l.Font,
                    FontSize = l.FontSize,
                    FontStyle = l.FontStyle,
                    HeightPercent = l.HeightPercent,
                    Stroke = l.Stroke,
                    StrokeWidth = l.StrokeWidth,
                    TextAlignment = l.TextAlignment,
                    HugBottom = l.HugBottom
                }).ToList()
            };

            for(var x = 0; x < renderParameters.Lines.Count; x++)
            {
                if (x < memeRequest.Lines.Count)
                {
                    renderParameters.Lines[x].Text = memeRequest.Lines[x].SanitizeMemeText(renderParameters.Lines[x].DoForceTextToAllCaps);
                }
            }

            var renderer = new Renderer();

            var bytes = renderer.Render(renderParameters);

            Analytics.TrackMeme(HttpContext, memeRequest.Name);

            return new FileContentResult(bytes, meme.ImageType);
        }