Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Globalization;
- using System.IO;
- using System.Windows;
- using System.Windows.Media;
- using System.Windows.Media.Imaging;
- namespace Memegen
- {
- class Program
- {
- static void Main(string[] args)
- {
- var imagePath = @"D:input.jpg";
- var text = "Это норма!";
- var resultPath = @"D:output.jpg";
- var image = BitmapFrame.Create(new Uri("file://" + imagePath));
- var imageWidth = (double)image.PixelWidth;
- var imageHeight = (double)image.PixelHeight;
- var background = Brushes.Black;
- var drawingVisual = new DrawingVisual();
- var textColor = Brushes.White;
- var font =
- new Typeface(
- new FontFamily("Segoe UI"), FontStyles.Normal,
- FontWeights.Bold, FontStretches.SemiExpanded);
- var fontSize = 70;
- var dpi = 96;
- var formattedText =
- new FormattedText(
- text, CultureInfo.CurrentCulture, FlowDirection.LeftToRight,
- font, fontSize, textColor, dpi)
- {
- MaxTextWidth = imageWidth,
- TextAlignment = TextAlignment.Center
- };
- var textWidth = formattedText.Width;
- var textHeight = formattedText.Height;
- var gap = 20;
- var totalWidth = (int)Math.Ceiling(imageWidth + 2 * gap);
- var totalHeight = (int)Math.Ceiling(imageHeight + 3 * gap + textHeight);
- using (DrawingContext drawingContext = drawingVisual.RenderOpen())
- {
- drawingContext.DrawRectangle(
- background, null,
- new Rect(0, 0, totalWidth, totalHeight));
- drawingContext.DrawImage(
- image,
- new Rect(gap, gap, imageWidth, imageHeight));
- drawingContext.DrawText(
- formattedText,
- new Point(gap, imageHeight + 2 * gap));
- }
- var bmp =
- new RenderTargetBitmap(
- totalWidth, totalHeight, dpi, dpi,
- PixelFormats.Pbgra32);
- bmp.Render(drawingVisual);
- var encoder = new JpegBitmapEncoder();
- encoder.Frames.Add(BitmapFrame.Create(bmp));
- using (var stream = File.Create(resultPath))
- encoder.Save(stream);
- }
- }
- }
Add Comment
Please, Sign In to add comment