C# System.Drawing.Text.AddMemoryFont 内存字体
PrivateFontCollection pfc = new PrivateFontCollection();
string NameSpc = System.Reflection.Assembly.GetExecutingAssembly().GetName().Name.ToString();
Stream stmFont = System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream(NameSpc + ".AN.TTF");
if (null != stmFont)
{
byte[] rgbyt = new Byte[stmFont.Length];
stmFont.Read(rgbyt, 0, rgbyt.Length);
IntPtr pbyt = Marshal.AllocCoTaskMem(rgbyt.Length);
if (null != pbyt)
{
Marshal.Copy(rgbyt, 0, pbyt, rgbyt.Length);
pfc.AddMemoryFont(pbyt, rgbyt.Length);
Marshal.FreeCoTaskMem(pbyt);
}
}
Font f = new Font(pfc.Families[0], 15, FontStyle.Bold);
//设置字体
foreach (Control labb in this.Controls)
{
if (labb is Label)
{
labb.Font = f;
((Label)labb).UseCompatibleTextRendering = true;
}
}
字样就实现内存字体显示
不过就有一些异常问题,就是说:
尝试读取或写入受保护的内存。这通常指示其他内存已损坏。
这样字眼的问题,经多次调试发现原来是UseCompatibleTextRendering = true;
的原因。。。。。
请问各位高人,还有其它方法帮我实现“字体”从内存里读取吗?或者有更好的方法来处理UseCompatibleTextRendering = true; 所出显的异常。。。。。