4

we are developing windows 8 metro app using SharpDX. Now we have to declare set of string within a Rectangle. For that we trying to find out the Font width and height using SharpDX.DrawingSizeF. for example:

Windows.Graphics g;
Model.Font font;
DrawingSizeF size = g.MeasureString(quote, font.Font, new DrawingSizeF(font.Width, font.Height));

we are trying to find out MeasureString with out using Windows.Graphics. Is it possible? or Is there any other way to get MeasureString in SharpDX or using Direct2D?

none
  • 11,793
  • 9
  • 51
  • 87
Maniarasu
  • 362
  • 5
  • 15

3 Answers3

1

I got some code from this messageboard post that works nicely for me. What I wound up with, after some fiddling of my own, is the following:

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 plug in the text, the font, the proposed width, and the alignment, it exports the size of a rectangle to hold the text. What you would be looking for, of course, is the height, but this includes the width since the text seldom fills the entire space.

Note: As suggested by a commenter, the code should actually be the following to Dispose() of the 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(); // IMPORTANT! If you don't dispose your SharpDX resources, your program will crash after a while.

    return new System.Drawing.SizeF(layout.Metrics.Width, layout.Metrics.Height);
}
Sean Duggan
  • 1,105
  • 2
  • 18
  • 48
1

I modified the first answer to address the limitation of not having access to DXFont in VB.net:

Public Function MeasureString(Message As String, textFormat As SharpDX.DirectWrite.TextFormat, Width As Single, Align As ContentAlignment) As System.Drawing.SizeF

        Dim layout As SharpDX.DirectWrite.TextLayout =
        New SharpDX.DirectWrite.TextLayout(New DirectWrite.Factory, Message, textFormat, Width, textFormat.FontSize)

        Return New System.Drawing.SizeF(layout.Metrics.Width, layout.Metrics.Height)

    End Function
James Plotts
  • 63
  • 1
  • 6
0

This did it for me.

private int getTextWidth(string text)
{
  float textWidth = 0f;
            
  if(text.Length > 0)
  {
    TextFormat tf = new TextFormat(new SharpDX.DirectWrite.Factory(), sf.Family.ToString(), SharpDX.DirectWrite.FontWeight.Normal, SharpDX.DirectWrite.FontStyle.Normal, (float)sf.Size);
    TextLayout tl = new TextLayout(Core.Globals.DirectWriteFactory, text, tf, ChartPanel.W, ChartPanel.H);
                
    textWidth = tl.Metrics.Width;
                
    tf.Dispose();
    tl.Dispose();
  }
            
  return (int)textWidth;
}
Tyler2P
  • 2,324
  • 26
  • 22
  • 31
Mttzern
  • 1
  • 1