동적으로 폰트 파일 읽어서 컨트롤에 적용하기

폰트파일 로드
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];
}

답글 남기기

이메일 주소는 공개되지 않습니다. 필수 항목은 *(으)로 표시합니다