Best C# Code To Overlay Text on a .JPG Image?

Magick.NET is an object-oriented C# interface to ImageMagick. Use this forum to discuss, make suggestions about, or report bugs concerning Magick.NET
Post Reply
GlennIM
Posts: 23
Joined: 2018-07-22T22:22:12-07:00
Authentication code: 1152

Best C# Code To Overlay Text on a .JPG Image?

Post by GlennIM »

Does anyone have the best C# code to overlay text in different font's and colors on an .JPG image? I'm trying to put "motivational" messages in the middle of images in .JPG format.

Thanks, Glenn
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Best C# Code To Overlay Text on a .JPG Image?

Post by fmw42 »

Please define "best"! Who knows what is the best code. Can you show what you have so far? Each change of font or color would have to be written with separate commands or you can use PANGO: markup. See https://imagemagick.org/Usage/text/#pango

Sorry, I do not know Magick.Net
GlennIM
Posts: 23
Joined: 2018-07-22T22:22:12-07:00
Authentication code: 1152

Re: Best C# Code To Overlay Text on a .JPG Image?

Post by GlennIM »

Code: Select all

var tempimage = new MagickImage(@"D:\\beautiful-girl-woman-young-127405_WEB.jpg");
            int textWidth = tempimage.Width - 10;
            tempimage.Dispose();

            MagickReadSettings settings = new MagickReadSettings()
            {
                FillColor = MagickColors.White, // -fill black
                StrokeColor = MagickColors.Black,
                Font = "Arial", // -font Arial
                Width = textWidth
            };

            using (var image = new MagickImage(@"D:\\beautiful-girl-woman-young-127405_WEB.jpg", settings))
            {
                image.Annotate("This is a Test", Gravity.Center);
                // write the image to the appropriate directory
                image.Write(@"D:\testimage.jpg");
            }

Below is testimage.jpg result. Why is the text on the image so small and how can I make it wider?
Image
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Best C# Code To Overlay Text on a .JPG Image?

Post by fmw42 »

You need to specify a point size before annotate, not a width.
GlennIM
Posts: 23
Joined: 2018-07-22T22:22:12-07:00
Authentication code: 1152

Re: Best C# Code To Overlay Text on a .JPG Image?

Post by GlennIM »

What value do you think I should I use for the Font Point Size? I wrote a function GetProperFontSize(MagickImage img) below to get the suggested font size from the image width. Then I call the following to get the FontPointsize and set it for the image read settings.

Code: Select all

            var tempimage = new MagickImage(@"D:\beautiful-girl-woman-young-127405_WEB.jpg");
            int textWidth = tempimage.Width - 10;
            int widthOfImage = GetProperFontSize(tempimage);

            MagickReadSettings settings = new MagickReadSettings()
            {
                FillColor = IdealTextColor(randomColor),
                StrokeColor = MagickColors.LightBlue,
                FontFamily = aFontFamily,
                FontPointsize = widthOfImage - 10
            };

Code: Select all

private int GetProperFontSize(MagickImage img)
        {
            var width = img.Width;

            if (width > 480 && width <= 680)
            {
                return 40;
                // return 20;
            }

            if (width > 680 && width <= 800)
            {
                return 44;
                // return 24;
            }

            if (width > 800 && width <= 1024)
            {
                return 52;
                // return 32;
            }

            if (width > 1024 && width <= 1600)
            {
                return 64;
                // return 44;
            }

            if (width > 1600 && width <= 2048)
            {
                return 70;
                // return 50;
            }

            if (width > 2048 && width <= 2560)
            {
                return 86;
                // return 66;
            }

            if (width > 2560 && width <= 6000)
            {
                return 100;
                // return 80;
            }

            return 26;
            // return 16;
        }
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Best C# Code To Overlay Text on a .JPG Image?

Post by snibgo »

Pointsize is the size in points (of course). One point is 1/72 of an inch. So the relationship between pointsize and pixels isn't direct. To calculate pointsize from a pixel size you also need to know the density, pixels per inch.
snibgo's IM pages: im.snibgo.com
Post Reply