Magick.NET icon indicating copy to clipboard operation
Magick.NET copied to clipboard

Background colour of annotated text always black

Open Seany84 opened this issue 7 years ago • 2 comments

Prerequisites

  • [x] I have written a descriptive issue title
  • [x] I have verified that I am using the latest version of Magick.NET

System Configuration

Running from unit test project in VS2017

  • Magick.NET version : Magick.NET-Q16-AnyCPU v7.11.0
  • Environment (Operating system, version and so on): Windows 10 x64

Question

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 images = new MagickImageCollection())
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.

Seany84 avatar Mar 22 '19 09:03 Seany84

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.

dlemstra avatar Mar 23 '19 09:03 dlemstra

@dlemstra Thanks Dirk - this was very straightforward and worked perfectly.

FYI - If you would like to copy and paste the answer on SO: https://stackoverflow.com/questions/55300254/imagemagick-net-background-colour-of-annotated-text-label-always-black/55304537#55304537

Seany84 avatar Mar 23 '19 11:03 Seany84