Được rồi, tôi đã tìm ra nhờ vào các mạng nội bộ và Google.
Để tham khảo sau này, nếu có sự cố này, khắc phục là: sau khi nhận phông chữ được nhúng dưới dạng luồng và trước khi gọi AddMemoryFont, , bạn phải gọi AddFontMemResourceEx! (Không có sẵn trong C# vì vậy bạn phải nhập nó:
[DllImport("gdi32.dll")]
private static extern IntPtr AddFontMemResourceEx(IntPtr pbFont, uint cbFont, IntPtr pdv, [In] ref uint pcFonts);
và sau đó:.
//create an unsafe memory block for the data
System.IntPtr data = Marshal.AllocCoTaskMem((int)fontStream.Length);
//create a buffer to read in to
Byte[] fontData = new Byte[fontStream.Length];
//fetch the font program from the resource
fontStream.Read(fontData, 0, (int)fontStream.Length);
//copy the bytes to the unsafe memory block
Marshal.Copy(fontData, 0, data, (int)fontStream.Length);
// We HAVE to do this to register the font to the system (Weird .NET bug !)
uint cFonts = 0;
AddFontMemResourceEx(data, (uint)fontData.Length, IntPtr.Zero, ref cFonts);
//pass the font to the font collection
mFontCollection.AddMemoryFont(data, (int)fontStream.Length);
//close the resource stream
fontStream.Close();
//free the unsafe memory
Marshal.FreeCoTaskMem(data);
Và mau, bạn sẽ có thể sử dụng phông chữ Nếu không có sự AddFontMemResourceEx nó wont work
+1 hữu ích khi biết rằng, cảm ơn, Led – BillW
Cua cua tôi đã đập đầu vào tường trong nhiều giờ nhờ !! – Mike
"FontStream" ở đâu từ đây? –