DrawText with horizontal alignment doesn't appear to work properly
See original GitHub issuePrerequisites
- 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
andRELEASE
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:
Centered:
Right Aligned:
Images
No response
Issue Analytics
- State:
- Created 4 months ago
- Comments:6 (4 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Looks like you’re confusing
(Horizontal/Vertical)Alignment
withTextAlignment
.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.
Upstream fix is now deployed to MyGet and Feedz
https://github.com/SixLabors/ImageSharp.Drawing/pull/278