Created
March 11, 2021 15:29
-
-
Save antonfirsov/077b7c364b422c1665dbe4b7cb2933fb to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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