동적으로 폰트 파일 읽어서 컨트롤에 적용하기 DarkKaiser, 2010년 3월 6일2023년 9월 6일 PrivateFontCollection fonts; FontFamily family = LoadFontFamily(@"F:\azuki.ttf", out fonts); theFont = new Font(family, 20.0f); // when done: theFont.Dispose(); family.Dispose(); family.Dispose(); ///////////////////////////////////////////////////////////////////////////////////////// public static FontFamily LoadFontFamily(Stream stream, out PrivateFontCollection fontCollection) { var buffer = new byte[stream.Length]; stream.Read(buffer, 0, buffer.Length); return LoadFontFamily(buffer, out fontCollection); } //public static unsafe FontFamily LoadFontFamilyUnsafe(byte[] buffer, out PrivateFontCollection fontCollection) //{ // fixed (byte* ptr = buffer) // { // fontCollection = new PrivateFontCollection(); // fontCollection.AddMemoryFont(new IntPtr(ptr), buffer.Length); // return fontCollection.Families[0]; // } //} public static FontFamily LoadFontFamily(byte[] buffer, out PrivateFontCollection fontCollection) { // pin array so we can get its address var handle = GCHandle.Alloc(buffer, GCHandleType.Pinned); try { var ptr = Marshal.UnsafeAddrOfPinnedArrayElement(buffer, 0); fontCollection = new PrivateFontCollection(); fontCollection.AddMemoryFont(ptr, buffer.Length); return fontCollection.Families[0]; } finally { // don't forget to unpin the array! handle.Free(); } } public static FontFamily LoadFontFamily(string fileName, out PrivateFontCollection fontCollection) { fontCollection = new PrivateFontCollection(); fontCollection.AddFontFile(fileName); return fontCollection.Families[0]; } C# 폰트