question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

DrawText with horizontal alignment doesn't appear to work properly

See original GitHub issue

Prerequisites

  • I have written a descriptive issue title
  • I have verified that I am running the latest version of ImageSharp
  • I have verified if the problem exist in both DEBUG and RELEASE mode
  • I have searched open and closed issues to ensure it has not already been reported

ImageSharp version

3.0.1

Other ImageSharp packages and versions

SixLabors.ImageSharp.Drawing 1.0.0-beta15

Environment (Operating system, version and so on)

Windows 10 x64

.NET Framework version

.Net 7.0

Description

I’m trying to right and center align text, but it appears to align in the opposite direction, or the boundary box in which it’s doing it is incorrect. I can’t spot anything that I might be doing wrong myself, basing it off of the sample code.

Am I doing something wrong here, or are there more steps to doing this I didn’t see in the docs?

Steps to Reproduce

image.Mutate<Rgba32>(c => c.Draw(Pens.DashDotDot(Color.White, 1), new RectangleF(0, 0, image.Width - 1, image.Height - 1)));
image.Mutate<Rgba32>(c => c.Draw(Pens.DashDotDot(Color.Red, 1), new RectangleF(x, y, width - 1, height - 1)));
var text = "Test simulation of a printed part";
var drawingOptions = new DrawingOptions();
var font = new Font(fontFamily, fontSize);
var textOptions = new TextOptions(font)
{
    Origin = new PointF(0, y),
    TabWidth = 4,
    WordBreaking = WordBreaking.BreakAll,
    Dpi = Dpi,
    KerningMode = KerningMode.None,
    LayoutMode = LayoutMode.HorizontalTopBottom,
    TextDirection = TextDirection.LeftToRight,
    HorizontalAlignment = HorizontalAlignment.Left,
    WrappingLength = width
};
image.Mutate<Rgba32>(c => c.DrawText(drawingOptions, textOptions, text, Brushes.Solid(fontColor), Pens.Solid(fontColor, 1)));

Left Aligned: beta (1)

Centered: beta (2)

Right Aligned: beta (3)

Images

No response

Issue Analytics

  • State:closed
  • Created 4 months ago
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
JimBobSquarePantscommented, May 9, 2023

Looks like you’re confusing (Horizontal/Vertical)Alignment with TextAlignment.

The former determines the position around the point at which draw the text, the latter aligns text within a container determined by the wrapping length.

Note to self. Improve the XML docs for these properties.

0reactions
JimBobSquarePantscommented, Jul 8, 2023

Upstream fix is now deployed to MyGet and Feedz

https://github.com/SixLabors/ImageSharp.Drawing/pull/278

Read more comments on GitHub >

github_iconTop Results From Across the Web

QPainter with Qt::AlignCenter does not center text correctly
My current workaround is to draw the texts separately like this: painter->drawText(QRectF(1, 0, m_iSize, m_iSize), Qt::AlignCenter, ...
Read more >
[solved] Draw: Paragraph Alignment does not work!
Hello, for some reasons, the horizontal text alignment doesn't work in Libre Office Draw. If I do: INSERT, TEXT BOX, and I over...
Read more >
How to: Align Drawn Text - Windows Forms .NET ...
Use the TextFormatFlags enumeration for wrapping as well as vertically and horizontally centering text with the appropriate DrawText method.
Read more >
Centering Single-Line Text in a Canvas
The problem is that while you can easily set a horizontal alignment for your TextPaint (via Paint.Align), the vertical alignment is tricky.
Read more >
Centering text · Issue #10 · Hopding/pdf-lib
Create a new content stream and use the drawText operator to draw text in the ... modifies it by adding right aligned and...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found