private void DrawText(IZenDrawable drawable) { var FONT_SIZE = 56; var X_TRANSLATE = 40; var Y_TRANSLATE = 40; var MAX_WIDTH = 940; var MAX_HEIGHT = 492 - 30; // 30 is for Author name var quote = $" {drawable.Message}"; var textOptions = new TextGraphicsOptions() { WrapTextWidth = MAX_WIDTH, VerticalAlignment = VerticalAlignment.Center }; var font = new Font(_font, FONT_SIZE, FontStyle.Bold); var currentHeight = 0f; var currentWidth = 0f; while (true) { var renderOptions = new RendererOptions(font, 72) { WrappingWidth = textOptions.WrapTextWidth }; var textMeasure = TextMeasurer.Measure(quote, renderOptions); currentHeight = textMeasure.Height; currentWidth = textMeasure.Width; if (currentHeight >= MAX_HEIGHT) { FONT_SIZE -= 8; font = new Font(_font, FONT_SIZE, FontStyle.Bold); } else { break; } } var yDraw = (MAX_HEIGHT - currentHeight) > Y_TRANSLATE * 2 ? IMAGE_HEIGHT / 2 - Y_TRANSLATE * 2 : IMAGE_HEIGHT / 2; //_image.DrawText(quote, font, Brushes.Solid(Rgba32.White), Pens.Solid<Rgba32>(new SolidBrush<Rgba32>(Rgba32.Black), 1), new Vector2(X_TRANSLATE, yDraw), textOptions); _image.DrawText(quote, font, Brushes.Solid(Rgba32.White), Pens.Solid(Rgba32.Black, 1), new Vector2(X_TRANSLATE, yDraw), textOptions); // Drawing Author name textOptions = new TextGraphicsOptions() { HorizontalAlignment = HorizontalAlignment.Right, VerticalAlignment = VerticalAlignment.Top }; _image.DrawText(drawable.Author, _fontAuthor, Rgba32.White, new Vector2(currentWidth + X_TRANSLATE, yDraw + currentHeight / 2 + 10), textOptions); }
public ZenQuoteImage(IZenDrawable drawable, IHostingEnvironment _hostingEnvironment) { Random rdn = new Random(); var quoteBackground = rdn.Next(1, BACK_NUM + 1); _image = Image.Load($"{_hostingEnvironment.ContentRootPath}\\wwwroot\\img\\back{quoteBackground}.jpg"); _fonts = new FontCollection(); //_font = _fonts.Install($"{_hostingEnvironment.ContentRootPath}\\wwwroot\\fonts\\Cousine-Italic.ttf"); _font = _fonts.Install($"{_hostingEnvironment.ContentRootPath}\\wwwroot\\fonts\\Flamante-Roma-Medium.ttf"); _fontAuthor = new Font(_fonts.Install($"{_hostingEnvironment.ContentRootPath}\\wwwroot\\fonts\\Cousine.ttf"), 30); DrawText(drawable); }