■ Graphics 클래스 : TextRenderingHint 속성을 사용해 폰트 안티-알리아싱 설정하기

------------------------------------------------------------------------------------------------------------------------


TestProject.zip


MainForm.cs

 

 

using System;

using System.Collections.Generic;

using System.Drawing;

using System.Drawing.Text;

using System.Windows.Forms;

 

namespace TestProject

{

    /// <summary>

    /// 메인 폼

    /// </summary>

    public partial class MainForm : Form

    {

        //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor

        ////////////////////////////////////////////////////////////////////////////////////////// Public

 

        #region 생성자 - MainForm()

 

        /// <summary>

        /// 생성자

        /// </summary>

        public MainForm()

        {

            InitializeComponent();

 

            #region 이벤트를 설정한다.

 

            Load                                   += Form_Load;

            Paint                                  += Form_Paint;

            this.fontComboBox.SelectedIndexChanged += fontComboBox_SelectedIndexChanged;

 

            #endregion

        }

 

        #endregion

 

        //////////////////////////////////////////////////////////////////////////////////////////////////// Method

        ////////////////////////////////////////////////////////////////////////////////////////// Private

        //////////////////////////////////////////////////////////////////////////////// Event

 

        #region 폼 로드시 처리하기 - Form_Load(sender, e)

 

        /// <summary>

        /// 폼 로드시 처리하기

        /// </summary>

        /// <param name="sender"></param>

        /// <param name="e"></param>

        private void Form_Load(object sender, EventArgs e)

        {

            InstalledFontCollection collection = new InstalledFontCollection();

 

            List<string> list = new List<string>();

 

            foreach(FontFamily fontFamily in collection.Families)

            {

                list.Add(fontFamily.Name);

            }

 

            this.fontComboBox.DataSource = list;

        }

 

        #endregion

        #region 폼 페인트시 처리하기 - Form_Paint(sender, e)

 

        /// <summary>

        /// 폼 페인트시 처리하기

        /// </summary>

        /// <param name="sender">이벤트 발생자</param>

        /// <param name="e">이벤트 인자</param>

        private void Form_Paint(object sender, PaintEventArgs e)

        {

            e.Graphics.Clear(this.BackColor);

 

            int x = 20;

            int y = this.fontComboBox.Bottom + 20;

 

            DrawSampleStringList(e.Graphics, TextRenderingHint.AntiAlias, x, ref y);

 

            y += 10;

 

            DrawSampleStringList(e.Graphics, TextRenderingHint.AntiAliasGridFit, x, ref y);

 

            y += 10;

 

            DrawSampleStringList(e.Graphics, TextRenderingHint.ClearTypeGridFit, x, ref y);

 

            x += 300;

 

            y = this.fontComboBox.Bottom + 20;

 

            DrawSampleStringList(e.Graphics, TextRenderingHint.SingleBitPerPixel, x, ref y);

 

            y += 10;

 

            DrawSampleStringList(e.Graphics, TextRenderingHint.SingleBitPerPixelGridFit, x, ref y);

 

            y += 10;

 

            DrawSampleStringList(e.Graphics, TextRenderingHint.SystemDefault, x, ref y);

        }

 

        #endregion

        #region 폰트 콤보 박스 선택 인덱스 변경시 처리하기 - fontComboBox_SelectedIndexChanged(sender, e)

 

        /// <summary>

        /// 폰트 콤보 박스 선택 인덱스 변경시 처리하기

        /// </summary>

        /// <param name="sender">이벤트 발생자</param>

        /// <param name="e">이벤트 인자</param>

        private void fontComboBox_SelectedIndexChanged(object sender, EventArgs e)

        {

            Refresh();

        }

 

        #endregion

 

        //////////////////////////////////////////////////////////////////////////////// Function

 

        #region 샘플 문자열 그리기 - DrawSampleString(graphics, fontSize, x, y)

 

        /// <summary>

        /// 샘플 문자열 그리기

        /// </summary>

        /// <param name="graphics">그래픽스</param>

        /// <param name="fontSize">폰트 크기</param>

        /// <param name="x">X 좌표</param>

        /// <param name="y">Y 좌표</param>

        private void DrawSampleString(Graphics graphics, float fontSize, int x, ref int y)

        {

            try

            {

                using(Font font = new Font(this.fontComboBox.Text, fontSize))

                {

                    string text = this.fontComboBox.Text + ", " + fontSize.ToString();

 

                    graphics.DrawString(text, font, Brushes.Black, x, y);

     

                    y = (int)(y + fontSize) + 10;

                }

            }

            catch

            {

            }

        }

 

        #endregion

        #region 샘플 문자열 목록 그리기 - DrawSampleStringList(graphics, textRenderingHint, x, y)

 

        /// <summary>

        /// 샘플 문자열 목록 그리기

        /// </summary>

        /// <param name="graphics">그래픽스</param>

        /// <param name="textRenderingHint">텍스트 렌더링 힌트</param>

        /// <param name="x">X 좌표</param>

        /// <param name="y">Y 좌표</param>

        private void DrawSampleStringList(Graphics graphics, TextRenderingHint textRenderingHint, int x, ref int y)

        {

            graphics.TextRenderingHint = TextRenderingHint.AntiAlias;

 

            graphics.DrawString(textRenderingHint.ToString(), Font, Brushes.Blue, x - 10, y);

 

            y += 20;

 

            graphics.TextRenderingHint = textRenderingHint;

 

            for(int fontSize = 6; fontSize <= 16; fontSize += 2)

            {

                DrawSampleString(graphics, fontSize, x, ref y);

            }

        }

 

        #endregion

    }

}

 

------------------------------------------------------------------------------------------------------------------------

  • 14 포인트 이상의 폰트의 경우 AntiAliasGridFit을 사용한다.
  • 약 8~14 포인트 사이의 폰트의 경우 AntiAlias를 사용한다.
  • 8 포인트 보다 작은 폰트의 경우 ClearTypeGridFit을 사용한다.

 

Posted by 사용자 icodebroker

댓글을 달아 주세요