Page 1 of 1

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

Posted: 2019-07-25T10:43:29-07:00
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

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

Posted: 2019-07-25T10:55:05-07:00
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

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

Posted: 2019-08-06T21:44:00-07:00
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

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

Posted: 2019-08-06T23:02:09-07:00
by fmw42
You need to specify a point size before annotate, not a width.

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

Posted: 2019-08-07T10:37:20-07:00
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;
        }

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

Posted: 2019-08-07T11:10:19-07:00
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.