Seany84
Seany84

Reputation: 5596

ImageMagick.net Background colour of annotated text/label always black

I am trying to add a label/annotation text (where the background colour of the text is transparent) to an existing image.

I have tried a couple of different approaches but I keep getting a black background colour.

If anyone could point me in the right direction, I would greatly appreciate it.

Attempt 1:

using (var images = new MagickImageCollection())
using (var img = new MagickImage(imgBytes))
{
    img.Resize(imageDto.Width, imageDto.Height);

    using (var imgText = new MagickImage(MagickColors.None, imageDto.Width, imageDto.Height))
    {
        var labelSettings = new MagickReadSettings()
        {
            BackgroundColor = MagickColors.None,
            Font = "Arial",
            FontPointsize = imageDto.FontSize,
            FillColor = MagickColors.Blue,
            BorderColor = MagickColors.None,
        };

        imgText.Read("label:" + imageDto.WatermarkText, labelSettings);
        img.Composite(imgText, Gravity.South);


        img.Write($"{Guid.NewGuid().ToString()}.png");
        return img.ToBase64();

    }
}

Attempt 2:

using (var img = new MagickImage(imgBytes))
{
    img.Resize(imageDto.Width, imageDto.Height);

    // Load the original image and add it to the collection.
    images.Add(img);

    // Text label watermark settings
    var labelSettings = new MagickReadSettings()
    {
        BackgroundColor = new MagickColor(MagickColors.Transparent),
        Font = "Arial",
        FontPointsize = imageDto.FontSize,
        FillColor = MagickColors.Blue
    };

    // Create the label image.
    var label = new MagickImage($"label:{imageDto.WatermarkText}", labelSettings);

    // Extent the width of the label to match the width of the original image.
    label.Extent(img.Width, 0, Gravity.Center);
    label.Transparent(MagickColors.Black);

    // Add the label to the collection.
    images.Add(label);

    // Append the images to create the output image.
    using (var result = images.AppendVertically())
    {
        result.Write($"{Guid.NewGuid().ToString()}.png");
        return result.ToBase64();
    }
}

Both attempts produce the same image with a black background (in the area where the text was added to the image)

Black BG on added text.

Upvotes: 0

Views: 2431

Answers (2)

dlemstra
dlemstra

Reputation: 8153

Your first approach is probably the easiest one. But you should use the following overload instead: img.Composite(imgText, Gravity.South, CompositeOperator.Over); The default is CompositeOperator.In and that is not what you should use to get the label as an overlay.

Upvotes: 1

fmw42
fmw42

Reputation: 53154

In ImageMagick, you cannot draw transparency for text or background on an opaque image. So you have to draw a colored (black) rectangle, then flood fill it with transparency, then draw your colored text onto the transparent image. For example with your image:

convert image.png \
-draw "translate 250,250 fill black rectangle -50,-50 50,50 \
fill none matte 0,0 floodfill" \
-fill "rgba(255,0,0,1)" -pointsize 20 \
-gravity center -annotate +0+0 "TESTING" \
result.png


enter image description here

ADDITION:

If you only want the text, then leave out the background color and just write the text.

convert image.png \
-fill "red" -pointsize 20 \
-gravity center -annotate +0+0 "TESTING" \
result.png


enter image description here

Upvotes: 1

Related Questions