반응형

실행파일에 폰트를 포함하는 방법

  • 프로젝트 내의 특정 폴더(ex: Resocuces)에 해당 폰트파일(들) 복사
    • (참고) 윈도우즈 폰트 파일 위치 : "C:\Windows\Fonts"
    • (예제) 한컴고딕("HANCOM GOTHIC REGULAR.TTF", 휴먼편지체 보통("HMFMPYUN.TTF")
  • Resources.resx > 리소스 추가 > 위의 폰트파일(들) 추가

 

 

FontManager.cs

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Text;
using System.Runtime.InteropServices;

namespace srctree {
    //1. 프로젝트 내의 Resources폴더(임의로 생성한 폴더)에 폰트파일(ttf) 복사
    //2. Resources.resx 에 복사한 ttf파일을 리소스 추가
    class FontManager {
        private static FontManager instance = new FontManager();
        public PrivateFontCollection privateFont = new PrivateFontCollection();
        public static FontFamily[] fontFamilys {
            get {
                return instance.privateFont.Families;
            }
        }

        public FontManager() {
            AddFontFromMemory();
        }

        private void AddFontFromMemory() {
            List<byte[]> fonts = new List<byte[]>();
            fonts.Add(Properties.Resources.HANCOM_GOTHIC_REGULAR);//한컴고딕 보통
            fonts.Add(Properties.Resources.HMFMPYUN);//휴먼편지체 보통

            foreach(byte[] font in fonts) {
                IntPtr fontBuffer = Marshal.AllocCoTaskMem(font.Length);
                Marshal.Copy(font, 0, fontBuffer, font.Length);
                privateFont.AddMemoryFont(fontBuffer, font.Length);

                Marshal.FreeHGlobal(fontBuffer);//메모리 해제
            }
        }
    }
}

사용

//한컴고딕 12크기 보통(뒷 부분 다 넣어줘야 한글이 정상적으로 동작)
Font font1 = new Font(FontManager.fontFamilys[0], 12, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(129)));

//휴먼편지체 14크기 볼드(뒷 부분 다 넣어줘야 한글이 정상적으로 동작)
Font font2 = new Font(FontManager.fontFamilys[1], 14, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(129)));
반응형
  • 네이버 블러그 공유하기
  • 네이버 밴드에 공유하기
  • 페이스북 공유하기
  • 카카오스토리 공유하기