views:

61

answers:

2

I have downloaded a font, [Betsy Flanagan][1], that I'd like to use in my program that shows on-screen keyboard shortcuts and their meaning in various programs.

However, when selecting the font in Visual Studio 2010 for a label, I get an error message that says "Only TrueType fonts are supported. This is not a TrueType font."

Is there any way for me to display text with this font in a .NET program? Since this is a specialized toast-like form, with just one label that needs to have this particular font, I don't really care if it is a hack to do it (like P/Invoke or similar.)

Note: This is a .NET 4.0 Winforms application.

+1  A: 

Have a look at this code that loads an embedded font as a resource and used in controls where applicable, the sample shows the usage of embedding an OCR font

    private PrivateFontCollection pfc = new PrivateFontCollection();
    private Font _fntOCRFont = null;
    private enum FontEnum{
       OCR = 0
    };

    private FontSize _fntSizeDefault = FontSize.Small;
    private float _fFontSize = 0.0F;

    private void InitOCRFont(){
        try{
            System.IO.Stream streamFont = this.GetType().Assembly.GetManifestResourceStream("ocraext.ttf");
            if (streamFont != null){
                byte[] fontData = new byte[streamFont.Length];
                streamFont.Read(fontData, 0, (int)streamFont.Length);
                streamFont.Close();
                unsafe{
                    fixed(byte *pFontData = fontData){
                    this.pfc.AddMemoryFont((System.IntPtr)pFontData, fontData.Length);
                }
            }
        }else{
            throw new Exception("Error! Could not read built-in Font.");
        }
    }catch(Exception eX){
        throw new Exception("Exception was: " + eX.Message);
    }
}

private void ConvertFontEnumToFloat(){
    switch(this._fntSizeDefault){
        case FontSize.Small :
            this._fFontSize = 8.0F;
            break;
        case FontSize.Medium :
            this._fFontSize = 10.0F;
            break;
        case FontSize.Large :
            this._fFontSize = 12.0F;
            break;
    }
}

Typical invocation of the code would be something like this:

this.ConvertFontEnumToFloat();
this._fntOCRFont = new Font(this.pfc.Families[(int)FontEnum.OCR], this._fFontSize, System.Drawing.FontStyle.Bold);
if (this._fntOCRFont != null){
   // Do something here... perhaps assign it to a control
}

The function InitOCRFont uses unsafe which means having the unsafe compiler option switched on, reads from the embedded resource and loads into a PrivateFontCollection. The function ConvertFontEnumToFloat uses a hardcoded float value to indicate the size based on the enum of the font. When finished with the code, be sure to dispose of the PrivateFontCollection instance in the Dispose method of designated class.

tommieb75
+2  A: 

If this is the font your're trying to use...

http://desktoppub.about.com/library/fonts/dd/uc_betsyflanagan.htm

then, maybe your local instance is corrupt?

VS2010 was well behaved when I tried to set labels and other winforms controls to Betsy. Download the one I linked and see if that works. My take is that if you've got a valid TTF installed, VS isn't going to take exception.

Works on my machine!

Grant