I found few examples on how to add a Watermark on a ImageSharp Image, but I get this error:
SixLabors.ImageSharp.Drawing: Method not found: 'System.Span`1<SixLabors.ImageSharp.PixelFormats.Rgba32> SixLabors.ImageSharp.Memory.Buffer2D`1.GetRowSpan(Int32)'.
My code:
outputImage.Mutate(ctx => ctx.ApplyScalingWaterMark(SystemFonts.CreateFont("Arial", 10), "TEXT", Color.WhiteSmoke, 24));
...
private static IImageProcessingContext ApplyScalingWaterMark(
this IImageProcessingContext processingContext,
Font font,
string text,
Color color,
float padding
)
{
return processingContext.ApplyScalingWaterMarkSimple(font, text, color, padding);
}
private static IImageProcessingContext ApplyScalingWaterMarkSimple(this IImageProcessingContext processingContext,
Font font,
string text,
Color color,
float padding)
{
Size imgSize = processingContext.GetCurrentSize();
// measure the text size
FontRectangle size = TextMeasurer.Measure(text, new RendererOptions(font));
//find out how much we need to scale the text to fill the space (up or down)
float scalingFactor = Math.Min(imgSize.Width / size.Width, imgSize.Height / size.Height);
//create a new font
Font scaledFont = new Font(font, scalingFactor * font.Size);
var center = new PointF(imgSize.Width / 2, imgSize.Height / 2);
var textGraphicOptions = new DrawingOptions();
return processingContext.DrawText(textGraphicOptions, text, scaledFont, color, center);
}
I tried few more examples but with the same result. The official code is not up to date anymore.
PS: SixLabors.ImageSharp NuGet version 3.0.1
I made it work with these packages
<PackageReference Include="SixLabors.ImageSharp" Version="3.0.1" />
<PackageReference Include="SixLabors.ImageSharp.Drawing" Version="1.0.0-beta15" />
<PackageReference Include="SixLabors.Fonts" Version="1.0.0-beta19" />
Your code was giving a compiler error here:
FontRectangle size = TextMeasurer.Measure(text, new RendererOptions(font));
I updated it like this:
FontRectangle size = TextMeasurer.Measure(text, new TextOptions(font));
usings:
using SixLabors.Fonts;
using SixLabors.ImageSharp.Drawing.Processing;
Check it and let's see if it works for you as well.