■ 커스텀 안티-알리아싱 만들기

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


TestProject.zip


MainForm.cs

 

 

using System;

using System.Drawing;

using System.Drawing.Drawing2D;

using System.Drawing.Text;

using System.Globalization;

using System.Windows.Forms;

 

namespace TestProject

{

    /// <summary>

    /// 메인 폼

    /// </summary>

    public partial class MainForm : Form

    {

        //////////////////////////////////////////////////////////////////////////////////////////////////// Field

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

 

        #region Field

 

        /// <summary>

        /// 아쿠아 버튼

        /// </summary>

        private AquaButtonDrawingObject aquaButton = new AquaButtonDrawingObject();

 

        /// <summary>

        /// 고양이

        /// </summary>

        private CatDrawingObject cat = new CatDrawingObject();

 

        /// <summary>

        /// 그리기 객체

        /// </summary>

        private IDrawingObject drawingObject = null;

 

        #endregion

 

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

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

 

        #region 생성자 - MainForm()

 

        /// <summary>

        /// 생성자

        /// </summary>

        public MainForm()

        {

            InitializeComponent();

 

            SetStyle(ControlStyles.DoubleBuffer, true);

            SetStyle(ControlStyles.AllPaintingInWmPaint, true);

            SetStyle(ControlStyles.ResizeRedraw, true);

 

            this.drawingObject = this.cat;

 

            this.catMenuItem.Checked = true;

        }

 

        #endregion

 

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

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

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

 

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

 

        /// <summary>

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

        /// </summary>

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

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

        private void Form_Paint(object sender, PaintEventArgs e)

        {

            StringFormat format = new StringFormat();

 

            format.Alignment     = StringAlignment.Center;

            format.LineAlignment = StringAlignment.Center;

 

            Font font = new Font("Arial", 13);

 

            SolidBrush whiteBrush = new SolidBrush(Color.White);

 

            PerformanceHelper performanceHelper = new PerformanceHelper();

 

            SolidBrush blueBrush       = new SolidBrush(Color.RoyalBlue);

            SolidBrush cornflowerBrush = new SolidBrush(Color.CornflowerBlue);

 

            double rectangleWidth = ClientRectangle.Width / 3d;

            double startTime;

            double originalExecutionTime;

            double standardExecutionTime;

            double customExecutionTime;

 

            Rectangle originalRectangle = new Rectangle(ClientRectangle.Left, ClientRectangle.Top, (int)rectangleWidth,

                ClientRectangle.Height);


            Rectangle standardRectangle = new Rectangle((int)(ClientRectangle.Left + rectangleWidth), ClientRectangle.Top,

                (int)rectangleWidth, ClientRectangle.Height);


            Rectangle customRectangle = new Rectangle((int)(ClientRectangle.Right - rectangleWidth), ClientRectangle.Top,

                (int)rectangleWidth, ClientRectangle.Height);

 

            Rectangle originalDrawingRectangle = new Rectangle(originalRectangle.Left, originalRectangle.Bottom - 60,

                originalRectangle.Width, 60);


            Rectangle standardDrawingRectangle = new Rectangle(standardRectangle.Left, standardRectangle.Bottom - 60,

                standardRectangle.Width, 60);


            Rectangle customDrawingRectangle = new Rectangle(customRectangle.Left, customRectangle.Bottom - 60, customRectangle.Width, 60);

 

            Rectangle topLabelRectangle = new Rectangle(ClientRectangle.Left, ClientRectangle.Top, ClientRectangle.Width, 60);

 

            e.Graphics.FillRectangle(blueBrush      , ClientRectangle  );

            e.Graphics.FillRectangle(cornflowerBrush, standardRectangle);

 

            originalRectangle.Inflate(new Size(-originalRectangle.Height / 15, -originalRectangle.Height / 15));

            standardRectangle.Inflate(new Size(-standardRectangle.Height / 15, -standardRectangle.Height / 15));

            customRectangle.Inflate  (new Size(-customRectangle.Height   / 15, -customRectangle.Height   / 15));

 

            e.Graphics.TextRenderingHint = TextRenderingHint.AntiAlias;

            e.Graphics.SmoothingMode     = SmoothingMode.None;

 

            startTime = performanceHelper.ElapsedTime;

 

            drawingObject.Paint(e.Graphics, originalRectangle, 1.0f);

 

            originalExecutionTime = performanceHelper.ElapsedTime - startTime;

 

            e.Graphics.SmoothingMode = SmoothingMode.None;

 

            startTime = performanceHelper.ElapsedTime;

 

            e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;

 

            drawingObject.Paint(e.Graphics, standardRectangle);

 

            standardExecutionTime = performanceHelper.ElapsedTime - startTime;

        

            e.Graphics.SmoothingMode = SmoothingMode.None;

 

            startTime = performanceHelper.ElapsedTime;

 

            drawingObject.Paint(e.Graphics, customRectangle);

 

            customExecutionTime = performanceHelper.ElapsedTime - startTime;

 

            NumberFormatInfo numberFormatInfo = new CultureInfo("en-US", false).NumberFormat;

 

            numberFormatInfo.NumberDecimalDigits = 3;

 

            e.Graphics.DrawString("GDI+ No AntiAlias\n(" + originalExecutionTime.ToString("N", numberFormatInfo) + "s)", font, whiteBrush,

                originalDrawingRectangle, format);

            e.Graphics.DrawString("GDI+ AntiAlias\n(" + standardExecutionTime.ToString("N", numberFormatInfo) + "s)", font, whiteBrush,

                standardDrawingRectangle, format);

            e.Graphics.DrawString("Custom AntiAlias\n(" + customExecutionTime.ToString("N", numberFormatInfo) + "s)", font, whiteBrush, 

                customDrawingRectangle  , format);

 

            RectangleF boundRectangle = drawingObject.GetBoundingRectangle(drawingObject.GetStandardTransform(standardRectangle));

 

            e.Graphics.DrawString(((int)(boundRectangle.Width + 0.5)) + "×" + ((int)(boundRectangle.Height + 0.5)), font, whiteBrush, 

                topLabelRectangle, format);

        }

 

        #endregion

 

        #region 아쿠아 버튼 메뉴 항목 클릭시 처리하기 - acuaButtonMenuItem_Click(sender, e)

 

        /// <summary>

        /// 아쿠아 버튼 메뉴 항목 클릭시 처리하기

        /// </summary>

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

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

        private void acuaButtonMenuItem_Click(object sender, EventArgs e)

        {

            this.aquaButtonMenuItem.Checked = true;

            this.catMenuItem.Checked        = false;

 

            this.drawingObject = this.aquaButton;

 

            Invalidate();

        }

 

        #endregion

        #region 고양이 메뉴 항목 클릭시 처리하기 - catMenuItem_Click(sender, e)

 

        /// <summary>

        /// 고야이 메뉴 항목 클릭시 처리하기

        /// </summary>

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

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

        private void catMenuItem_Click(object sender, EventArgs e)

        {

            this.aquaButtonMenuItem.Checked = false;

            this.catMenuItem.Checked        = true;

 

            this.drawingObject = this.cat;

 

            Invalidate();

        }

 

        #endregion

    }

}

 

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

Posted by 사용자 icodebroker

댓글을 달아 주세요