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.