I use some embedded fonts in my .Net 4.5 software. Everything is OK when I run software on Windows 10 but when I tried to test the software on Windows 7 virtual machine, it threw this exception:
Font 'B Koodak' does not support style 'Regular'.
Here is how I load embedded fonts from resources:
[System.Runtime.InteropServices.DllImport("gdi32.dll")]
private static extern IntPtr AddFontMemResourceEx(IntPtr pbFont, uint cbFont,
IntPtr pdv, [System.Runtime.InteropServices.In] ref uint pcFonts);
public readonly PrivateFontCollection privateFontCollection = new PrivateFontCollection();
public readonly FontFamily Samim;
private readonly FontFamily IranNastaliq;
private readonly FontFamily BKoodak;
private readonly FontFamily BNazanin;
public MainForm()
{
uint temp = 0;
byte[][] fontData = new byte[][] { Properties.Resources.Samim, Properties.Resources.IranNastaliq_Web,
Properties.Resources.BNazanin, Properties.Resources.BKoodak };
for (int i = 0; i < fontData.Length; i++)
{
IntPtr fontPtr = System.Runtime.InteropServices.Marshal.AllocCoTaskMem(fontData[i].Length);
System.Runtime.InteropServices.Marshal.Copy(fontData[i], 0, fontPtr, fontData[i].Length);
privateFontCollection.AddMemoryFont(fontPtr, fontData[i].Length);
AddFontMemResourceEx(fontPtr, (uint)fontData[i].Length, IntPtr.Zero, ref temp);
System.Runtime.InteropServices.Marshal.FreeCoTaskMem(fontPtr);
}
Samim = privateFontCollection.Families[3];
IranNastaliq = privateFontCollection.Families[2];
BNazanin = privateFontCollection.Families[1];
BKoodak = privateFontCollection.Families[0];
InitializeComponent();
Application.CurrentCulture = new CultureInfo("fa-IR");
// Other codes...
}
private void Draw(ref graphics)
{
Font font = new Font(BKoodak, 18);
graphics.DrawString("سلام", font, new SolidBrush(Color.Black), 0, 0, new StringFormat());
}
Why it is OK on Windows 10 but not Windows 7? How should I fix it?
EDIT:
I found this font only supports bold and outline mode. In Microsoft Word, there is no difference between bold and regular styles of this font.
So I changed Font font = new Font(BKoodak, 18);
to Font font = new Font(BKoodak, 18, FontStyle.Bold);
and now it works on Windows 7.
Now the question is: Why did regular style work OK in Windows 10 despite this font doesn't support regular style?