I have code from this post that works well for me. What I came across, after some of my own actions, is this:
public System.Drawing.SizeF MeasureString(string Message, DXFonts.DXFont Font, float Width, ContentAlignment Align) { SharpDX.DirectWrite.TextFormat textFormat = Font.GetFormat(Align); SharpDX.DirectWrite.TextLayout layout = new SharpDX.DirectWrite.TextLayout(DXManager.WriteFactory, Message, textFormat, Width, textFormat.FontSize); return new System.Drawing.SizeF(layout.Metrics.Width, layout.Metrics.Height); }
If you insert text, font, suggested width and alignment, it exports the size of the rectangle to hold the text. Of course, you are looking for height, but this includes width, since text rarely fills the entire space.
Note. As suggested by the commentator, there should actually be the following code for Dispose () resources:
public System.Drawing.SizeF MeasureString(string Message, DXFonts.DXFont Font, float Width, ContentAlignment Align) { SharpDX.DirectWrite.TextFormat textFormat = Font.GetFormat(Align); SharpDX.DirectWrite.TextLayout layout = new SharpDX.DirectWrite.TextLayout(DXManager.WriteFactory, Message, textFormat, Width, textFormat.FontSize); textFormat.Dispose();
source share