locked
Font height and width RRS feed

  • Question

  • Can anybody provide an example of creating a font with a specific height and width? I used to do this with LOGFONT but in .NET I couldn't figure out a way to
    do this, either than P/Invoking CreateFontIndirect.

    Thanks,
    Kostas
    Monday, November 21, 2005 3:24 PM

Answers

  • You can use Font.Font(FontFamily, Single, GraphicsUnit) Constructor. Set GraphicsUnit.Pixel as GraphicsUnit. And AFAIK you cannot set the width of a char, only it's height.
    Monday, November 21, 2005 7:13 PM

All replies

  • Use Graphics.DrawString() Method with font object as argument.

    From MSDN:



    public void DrawStringPointF(PaintEventArgs e)
    {
    // Create string to draw.
    String drawString = "Sample Text";
    // Create font and brush.
    Font drawFont = new Font("Arial", 16);
    SolidBrush drawBrush = new SolidBrush(Color.Black);
    // Create point for upper-left corner of drawing.
    PointF drawPoint = new PointF(150.0F, 150.0F);
    // Draw string to screen.
    e.Graphics.DrawString(drawString, drawFont, drawBrush, drawPoint);
    }


     
    Monday, November 21, 2005 3:40 PM
  • I don't think you understood me. Suppose I want to create a "Courier new" Font whose character cell would be exactly 10 pixels high and exactly 30 pixels wide. (I know that width is exact when the font is fixed-size. Otherwise it's just an average). Which constructor should I call? Most of them expect an em-size parameter which I don't know what it is.
    Monday, November 21, 2005 3:55 PM
  • You can use Font.Font(FontFamily, Single, GraphicsUnit) Constructor. Set GraphicsUnit.Pixel as GraphicsUnit. And AFAIK you cannot set the width of a char, only it's height.
    Monday, November 21, 2005 7:13 PM