Font Dialog not returning exact font size

Sudip Bhatt 2,271 Reputation points
2020-10-29T17:05:18.433+00:00

i am working with font dialog from my winform project.

 if (fontDlg.ShowDialog() == DialogResult.OK)
                {
                    FontName = fontDlg.Font.Name;
                    FontStyle = fontDlg.Font.Style.ToString();
                    FontSize = fontDlg.Font.Size.ToString();
                    //if (FontSize.Contains("."))
                    //    FontSize = FontSize.Split('.')[0];

                    UnderLine = fontDlg.Font.Underline;
                    IsFontChanged = true;
                }

when i am select font size 14 then getting 14.25 and when selecting 16 then getting 15.75
i want whole number what i am selecting. how it will be possible?

if i select 14 then i should get 14. if i select 16 then i should get 16. how it will be possible ?
please advise. thanks

Windows Forms
Windows Forms
A set of .NET Framework managed libraries for developing graphical user interfaces.
1,835 questions
{count} votes

Accepted answer
  1. Viorel 112.5K Reputation points
    2020-10-30T07:07:01.097+00:00

    This dialog is bases on system Font dialog and it seems that the returned font size is recalculated.

    As a workaround, consider calling the system dialog directly. This needs more code. You can use these fragments in your research:

    private void button1_Click( object sender, EventArgs e )
    {
     CHOOSEFONT cf = new CHOOSEFONT( ) { lStructSize = Marshal.SizeOf<CHOOSEFONT>( ) };
     cf.Flags = CHOOSEFONTFLAGS.CF_EFFECTS | CHOOSEFONTFLAGS.CF_FORCEFONTEXIST |
     CHOOSEFONTFLAGS.CF_NOSCRIPTSEL | CHOOSEFONTFLAGS.CF_NOVERTFONTS | CHOOSEFONTFLAGS.CF_SCALABLEONLY;
     IntPtr hlf = Marshal.AllocHGlobal( Marshal.SizeOf<LOGFONT>( ) );
     cf.lpLogFont = hlf;
    
     if( ChooseFont( ref cf ) )
     {
     int size = cf.iPointSize / 10;
    
     LOGFONT lf = (LOGFONT)Marshal.PtrToStructure( hlf, typeof( LOGFONT ) );
    
     MessageBox.Show( $"Font: '{lf.lfFaceName}', Size: {size}, Italic: {lf.lfItalic != 0}, Bold: {lf.lfWeight > 500}, Underline: {lf.lfUnderline != 0}" );
     }
    
     Marshal.FreeHGlobal( hlf );
    }
    

    Also add these declarations to your form:

    [DllImport( "comdlg32", SetLastError = true )]
    public extern static bool ChooseFont( ref CHOOSEFONT cf );
    
    public struct CHOOSEFONT
    {
     public int lStructSize;
     public IntPtr hwndOwner;
     public IntPtr hDC;
     public IntPtr lpLogFont;
     public int iPointSize;
     public CHOOSEFONTFLAGS Flags;
     public int rgbColors;
     public IntPtr lCustData;
     public IntPtr lpfnHook;
     public string lpTemplateName;
     public IntPtr hInstance;
     public string lpszStyle;
     public short nFontType;
     private short __MISSING_ALIGNMENT__;
     public int nSizeMin;
     public int nSizeMax;
    }
    
    [Flags]
    public enum CHOOSEFONTFLAGS:int
    {
     CF_SCREENFONTS = 0x00000001,
     CF_PRINTERFONTS = 0x00000002,
     CF_BOTH = ( CF_SCREENFONTS | CF_PRINTERFONTS ),
     CF_SHOWHELP = 0x00000004,
     CF_ENABLEHOOK = 0x00000008,
     CF_ENABLETEMPLATE = 0x00000010,
     CF_ENABLETEMPLATEHANDLE = 0x00000020,
     CF_INITTOLOGFONTSTRUCT = 0x00000040,
     CF_USESTYLE = 0x00000080,
     CF_EFFECTS = 0x00000100,
     CF_APPLY = 0x00000200,
     CF_ANSIONLY = 0x00000400,
     CF_SCRIPTSONLY = CF_ANSIONLY,
     CF_NOVECTORFONTS = 0x00000800,
     CF_NOOEMFONTS = CF_NOVECTORFONTS,
     CF_NOSIMULATIONS = 0x00001000,
     CF_LIMITSIZE = 0x00002000,
     CF_FIXEDPITCHONLY = 0x00004000,
     CF_WYSIWYG = 0x00008000,
     CF_FORCEFONTEXIST = 0x00010000,
     CF_SCALABLEONLY = 0x00020000,
     CF_TTONLY = 0x00040000,
     CF_NOFACESEL = 0x00080000,
     CF_NOSTYLESEL = 0x00100000,
     CF_NOSIZESEL = 0x00200000,
     CF_SELECTSCRIPT = 0x00400000,
     CF_NOSCRIPTSEL = 0x00800000,
     CF_NOVERTFONTS = 0x01000000,
     CF_INACTIVEFONTS = 0x02000000
    }
    
    public struct LOGFONT
    {
     public int lfHeight;
     public int lfWidth;
     public int lfEscapement;
     public int lfOrientation;
     public int lfWeight;
     public byte lfItalic;
     public byte lfUnderline;
     public byte lfStrikeOut;
     public byte lfCharSet;
     public byte lfOutPrecision;
     public byte lfClipPrecision;
     public byte lfQuality;
     public byte lfPitchAndFamily;
     [MarshalAs( UnmanagedType.ByValTStr, SizeConst = 32 )]
     public string lfFaceName;
    }
    
    1 person found this answer helpful.

1 additional answer

Sort by: Most helpful
  1. Daniel Zhang-MSFT 9,616 Reputation points
    2020-10-30T03:23:16.27+00:00

    Hi Sudip Bhatt,
    Font size is changing in steps of 0.75. It is related to DPI and font size units.
    More discussion in these thread you can refer to,
    Font size discrepancy in .NET GDI+?
    Strange FontSize change
    And you can change the font size via following code:

    fontDialog1.Font = new Font("Arial Black", 14, FontStyle.Italic);  
    string size = fontDialog1.Font.Size.ToString();  
    

    Best Regards,
    Daniel Zhang


    If the response is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments