1

I am using ImageMagick to add text to an image and I need that text to be centered horizontally but I also want to be able to set the vertical position. My code that works but places the text 50 pixels from the left of the canvas edge is:

        'This adds the address text block
        image.Settings.FillColor = MagickColors.Transparent
        image.Settings.StrokeColor = MagickColors.Transparent
        image.Settings.FontPointsize = 195
        image.Annotate("508 West Earl Street", New MagickGeometry(50, 300, 150, 75), Gravity.Undefined, 0)

I need the code to center it horizontally and make it 300px from the top vertically; something like below but I can't figure out what to put in place of the X coordinate to center it.

        'This adds the address text block and should center it.
        image.Settings.FillColor = MagickColors.Transparent
        image.Settings.StrokeColor = MagickColors.Transparent
        image.Settings.FontPointsize = 195
        image.Annotate("508 West Earl Street", New MagickGeometry('This Value Needs to Center it', 300, 150, 75), Gravity.Undefined, 0)
7
  • Use -gravity north and then use the equivalent of -annotate "some text" +0+300. That will start the text horizontally centered, but move it down 300 pixels from the top (north). Commented Oct 23, 2019 at 16:22
  • I guess the problem is going to be to figure out what the equivalent is in C#/VB. Below is what I have tried and the "Gravity.North has no effect; the text is always on the left side of the canvas. image.Annotate("Test", New MagickGeometry(0, 300, 150, 75), Gravity.North, 0) I have also tried image.Annotate("Test", Gravity.North, New MagickGeometry(0, 300, 150, 75), 0) with the same result. Commented Oct 23, 2019 at 20:46
  • I do not understand why you need 4 values, just to put the image down 300 in Y, if you use gravity north. Try setting them to 0, so you have (0,300,0,0). Commented Oct 23, 2019 at 22:39
  • image.Annotate("Test", New MagickGeometry(0, 365, 0, 0), Gravity.North, 0) doesn't work either. It places the text with most of it off of the left hand side of the canvas. Commented Oct 23, 2019 at 23:21
  • What do the 4 numbers mean in the arguments? Are they offsets or sizes or rotation angles? Can you point me to the documentation? Otherwise, I suggest you post your question to dlemstra, the developer of Magick.NET at either github.com/dlemstra/Magick.NET or imagemagick.org/discourse-server/viewforum.php?f=27 Commented Oct 23, 2019 at 23:52

1 Answer 1

0

If you have the width of your image, divide that in half. You can also set the gravity property to center to center align your text. Your annotation code should look something like the following:

image.Annotate("508 West Earl Street", New MagickGeometry(image.width/2, 300, 150, 75), Gravity.Center, 0)

Source

Sign up to request clarification or add additional context in comments.

2 Comments

One needs to set gravity to north as I said in my comment and then use a +300 Y offset so that the text is horizontally centered and down 300 px from the top.
Your solution works with one alteration. I could not access image.width from within MagikGeometry so I placed it into a variable and then used the variable in MagikGeometry like this: Dim width As Integer = image.Width Dim widthhalf As Integer = widthe / 2 image.Annotate("508 West Earl Street", New MagickGeometry(widthhalf, 300, 150, 75), Gravity.Center, 0) Thank you!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.