Skip to content

Instantly share code, notes, and snippets.

@antonfirsov
Created March 11, 2021 15:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save antonfirsov/077b7c364b422c1665dbe4b7cb2933fb to your computer and use it in GitHub Desktop.
Save antonfirsov/077b7c364b422c1665dbe4b7cb2933fb to your computer and use it in GitHub Desktop.
using SixLabors.Fonts;
using SixLabors.ImageSharp;
using SixLabors.ImageSharp.Processing;
using SixLabors.ImageSharp.Drawing.Processing;
using System;
using SixLabors.ImageSharp.PixelFormats;
namespace ColonTest
{
class Program
{
static void Main(string[] args)
{
using Image<Rgba32> image = new Image<Rgba32>(500, 500);
image.Mutate(c => AddDateStamp(c, "a : b : c", Color.White, @"C:\Windows\Fonts\arial.ttf"));
image.SaveAsPng(@".\Test.png");
}
private static IImageProcessingContext AddDateStamp(IImageProcessingContext processingContext, string dateTime, Color color, string fontPath)
{
var imgSize = processingContext.GetCurrentSize();
float defaultFontSize = 18;
float defaultResolution = 645;
float defaultPadding = 10;
float fontSize = imgSize.Width * defaultFontSize / defaultResolution;
float padding = imgSize.Width * defaultPadding / defaultResolution;
FontCollection collection = new FontCollection();
FontFamily family = collection.Install(fontPath);
Font font = family.CreateFont(fontSize, FontStyle.Regular);
var fontRectangle = TextMeasurer.Measure(dateTime, new RendererOptions(font));
var location = new PointF(imgSize.Width - fontRectangle.Width - padding, imgSize.Height - fontRectangle.Height - padding);
return processingContext.DrawText(dateTime, font, color, location);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment