첨부 실행 코드는 나눔고딕코딩 폰트를 사용합니다.
유용한 소스 코드가 있으면 icodebroker@naver.com으로 보내주시면 감사합니다.
블로그 자료는 자유롭게 사용하세요.

728x90
반응형

■ Graphics 클래스 : 그래픽 변환 사용하기

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


TestProject.zip



MainForm.cs

 

 

using System;

using System.Drawing;

using System.Drawing.Drawing2D;

using System.Windows.Forms;

 

namespace TestProject

{

    /// <summary>

    /// 메인 폼

    /// </summary>

    public partial class MainForm : Form

    {

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

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

 

        #region 생성자 - MainForm()

 

        /// <summary>

        /// 생성자

        /// </summary>

        public MainForm()

        {

            InitializeComponent();

 

            SizeChanged                         += Form_SizeChanged;

            this.originalPictureBox.Paint       += originalPictureBox_Paint;

            this.translatePictureBox.Paint      += translatePictureBox_Paint;

            this.translateXTextBox.TextChanged  += translateTextBox_TextChanged;

            this.translateYTextBox.TextChanged  += translateTextBox_TextChanged;

            this.rotatePicutreBox.Paint         += rotatePicutreBox_Paint;

            this.rotateAngleTextBox.TextChanged += rotateAngleTextBox_TextChanged;

            this.scalePictureBox.Paint          += scalePictureBox_Paint;

            this.scaleXTextBox.TextChanged      += scaleTextBox_TextChanged;

            this.scaleYTextBox.TextChanged      += scaleTextBox_TextChanged;

            this.skewPictureBox.Paint           += skewPictureBox_Paint;

            this.skewXTextBox.TextChanged       += skewTextBox_TextChanged;

            this.skewYTextBox.TextChanged       += skewTextBox_TextChanged;

            this.m11TextBox.TextChanged         += matrixTextBox_TextChanged;

            this.m12TextBox.TextChanged         += matrixTextBox_TextChanged;

            this.m21TextBox.TextChanged         += matrixTextBox_TextChanged;

            this.m22TextBox.TextChanged         += matrixTextBox_TextChanged;

            this.m31TextBox.TextChanged         += matrixTextBox_TextChanged;

            this.m32TextBox.TextChanged         += matrixTextBox_TextChanged;

            this.matrixPictureBox.Paint         += matrixPictureBox_Paint;

        }

 

        #endregion

 

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

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

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

 

        #region 폼 크기 변경시 처리하기 - Form_SizeChanged(sender, e)

 

        /// <summary>

        /// 폼 크기 변경시 처리하기

        /// </summary>

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

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

        private void Form_SizeChanged(object sender, EventArgs e)

        {

            this.originalPictureBox.Refresh();

            this.translatePictureBox.Refresh();

            this.rotatePicutreBox.Refresh();

            this.scalePictureBox.Refresh();

            this.skewPictureBox.Refresh();

            this.matrixPictureBox.Refresh();

        }

 

        #endregion

        #region 원본 픽처 박스 페인트시 처리하기 - originalPictureBox_Paint(sender, e)

 

        /// <summary>

        /// 원본 픽처 박스 페인트시 처리하기

        /// </summary>

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

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

        private void originalPictureBox_Paint(object sender, PaintEventArgs e)

        {

            DrawPicture(sender as PictureBox, e.Graphics);

        }

 

        #endregion

        #region 이동 텍스트 박스 텍스트 변경시 처리하기 - translateTextBox_TextChanged(sender, e)

 

        /// <summary>

        /// 이동 텍스트 박스 텍스트 변경시 처리하기

        /// </summary>

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

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

        private void translateTextBox_TextChanged(object sender, EventArgs e)

        {

            this.translatePictureBox.Refresh();

        }

 

        #endregion

        #region 이동 픽처 박스 페인트시 처리하기 - translatePictureBox_Paint(sender, e)

 

        /// <summary>

        /// 이동 픽처 박스 페인트시 처리하기

        /// </summary>

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

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

        private void translatePictureBox_Paint(object sender, PaintEventArgs e)

        {

            float translateX;

            float translateY;

 

            float.TryParse(this.translateXTextBox.Text, out translateX);

            float.TryParse(this.translateYTextBox.Text, out translateY);

 

            e.Graphics.TranslateTransform(translateX, translateY);

 

            DrawPicture(sender as PictureBox, e.Graphics);

        }

 

        #endregion

        #region 회전 각도 텍스트 박스 텍스트 변경시 처리하기 - rotateAngleTextBox_TextChanged(sender, e)

 

        /// <summary>

        /// 회전 각도 텍스트 박스 텍스트 변경시 처리하기

        /// </summary>

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

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

        private void rotateAngleTextBox_TextChanged(object sender, EventArgs e)

        {

            this.rotatePicutreBox.Refresh();

        }

 

        #endregion

        #region 회전 픽처 박스 페인트시 처리하기 - rotatePicutreBox_Paint(sender, e)

 

        /// <summary>

        /// 회전 픽처 박스 페인트시 처리하기

        /// </summary>

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

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

        private void rotatePicutreBox_Paint(object sender, PaintEventArgs e)

        {

            float angle;

 

            float.TryParse(this.rotateAngleTextBox.Text, out angle);

 

            e.Graphics.RotateTransform(angle);

 

            DrawPicture(sender as PictureBox, e.Graphics);

        }

 

        #endregion

        #region 확대/축소 텍스트 박스 텍스트 변경시 처리하기 - scaleTextBox_TextChanged(sender, e)

 

        /// <summary>

        /// 확대/축소 텍스트 박스 텍스트 변경시 처리하기

        /// </summary>

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

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

        private void scaleTextBox_TextChanged(object sender, EventArgs e)

        {

            this.scalePictureBox.Refresh();

        }

 

        #endregion

        #region 확대/축소 픽처 박스 페인트시 처리하기 - scalePictureBox_Paint(sender, e)

 

        /// <summary>

        /// 확대/축소 픽처 박스 페인트시 처리하기

        /// </summary>

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

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

        private void scalePictureBox_Paint(object sender, PaintEventArgs e)

        {

            float scaleX;

            float scaleY;

 

            float.TryParse(this.scaleXTextBox.Text, out scaleX);

            float.TryParse(this.scaleYTextBox.Text, out scaleY);

 

            try

            {

                e.Graphics.ScaleTransform(scaleX, scaleY);

 

                DrawPicture(sender as PictureBox, e.Graphics);

            }

            catch

            {

            }

        }

 

        #endregion

        #region 기울이기 텍스트 박스 텍스트 변경시 처리하기 - skewTextBox_TextChanged(sender, e)

 

        /// <summary>

        /// 기울이기 텍스트 박스 텍스트 변경시 처리하기

        /// </summary>

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

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

        private void skewTextBox_TextChanged(object sender, EventArgs e)

        {

            this.skewPictureBox.Refresh();

        }

 

        #endregion

        #region 기울이기 픽처 박스 페인트시 처리하기 - skewPictureBox_Paint(sender, e)

 

        /// <summary>

        /// 기울이기 픽처 박스 페인트시 처리하기

        /// </summary>

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

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

        private void skewPictureBox_Paint(object sender, PaintEventArgs e)

        {

            float skewX;

            float skewY;

 

            float.TryParse(this.skewXTextBox.Text, out skewX);

            float.TryParse(this.skewYTextBox.Text, out skewY);

 

            Matrix matrix = new Matrix();

 

            matrix.Shear(skewX, skewY);

 

            e.Graphics.Transform = matrix;

 

            DrawPicture(sender as PictureBox, e.Graphics);

        }

 

        #endregion

        #region 매트릭스 텍스트 박스 텍스트 변경시 처리하기 - matrixTextBox_TextChanged(sender, e)

 

        /// <summary>

        /// 매트릭스 텍스트 박스 텍스트 변경시 처리하기

        /// </summary>

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

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

        private void matrixTextBox_TextChanged(object sender, EventArgs e)

        {

            this.matrixPictureBox.Refresh();

        }

 

        #endregion

        #region 매트릭스 픽처 박스 페인트시 처리하기 - matrixPictureBox_Paint(sender, e)

 

        /// <summary>

        /// 매트릭스 픽처 박스 페인트시 처리하기

        /// </summary>

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

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

        private void matrixPictureBox_Paint(object sender, PaintEventArgs e)

        {

            float translateX = 0;

            float translateY = 0;

            float scaleX     = 1;

            float scaleY     = 1;

            float skewY      = 0;

            float skewX      = 0;

 

            float.TryParse(this.m11TextBox.Text, out scaleX    );

            float.TryParse(this.m12TextBox.Text, out skewY     );

            float.TryParse(this.m21TextBox.Text, out skewX     );

            float.TryParse(this.m22TextBox.Text, out scaleY    );

            float.TryParse(this.m31TextBox.Text, out translateX);

            float.TryParse(this.m32TextBox.Text, out translateY);

 

            try

            {

                Matrix matrix = new Matrix(scaleX, skewY, skewX, scaleY, translateX, translateY);

 

                e.Graphics.Transform = matrix;

 

                DrawPicture(sender as PictureBox, e.Graphics);

            }

            catch

            {

            }

        }

 

        #endregion

 

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

 

        #region 웃는 얼굴 그리기 - DrawSmiley(graphics, boundRectangle)

 

        /// <summary>

        /// 웃는 얼굴 그리기

        /// </summary>

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

        /// <param name="boundRectangle">테두리 사각형</param>

        private void DrawSmiley(Graphics graphics, RectangleF boundRectangle)

        {

            graphics.DrawRectangle(Pens.Green, boundRectangle);

 

            float thickness = boundRectangle.Width / 30;

 

            boundRectangle.Inflate(-thickness, -thickness);

 

            using(Pen pen = new Pen(Color.Red, thickness))

            {

                graphics.FillEllipse(Brushes.Yellow, boundRectangle);

 

                graphics.DrawEllipse(pen, boundRectangle);

 

                pen.Color = Color.Black;

 

                RectangleF rectangle = boundRectangle;

 

                rectangle.Inflate

                (

                    -boundRectangle.Width / 6,

                    -boundRectangle.Width / 6

                );

 

                graphics.DrawArc(pen, rectangle, 0, 180);

 

                pen.Width /= 2f;

 

                float centerX = boundRectangle.X + boundRectangle.Width  * 0.5f;

                float centerY = boundRectangle.Y + boundRectangle.Height * 0.55f;

 

                float noseWidth  = boundRectangle.Width * 0.2f;

                float noseHeight = boundRectangle.Width * 0.35f;

 

                RectangleF noseRectangle = new RectangleF

                (

                    centerX - noseWidth  / 2f,

                    centerY - noseHeight / 2f,

                    noseWidth,

                    noseHeight

                );

 

                graphics.FillEllipse(Brushes.LightBlue, noseRectangle);

 

                pen.Color = Color.Blue;

 

                graphics.DrawEllipse(pen, noseRectangle);

 

                centerX = boundRectangle.X + boundRectangle.Width  * 0.3f;

                centerY = boundRectangle.Y + boundRectangle.Height * 0.3f;

 

                float eyeWidth  = noseWidth  * 0.85f;

                float eyeHeight = noseHeight * 0.75f;

 

                RectangleF eyeRectangle = new RectangleF

                (

                    centerX - eyeWidth  / 2f,

                    centerY - eyeHeight / 2f,

                    eyeWidth,

                    eyeHeight

                );

 

                graphics.FillEllipse(Brushes.White, eyeRectangle);

 

                pen.Color = Color.Black;

 

                graphics.DrawEllipse(pen, eyeRectangle);

 

                float pupilX = centerX + noseWidth / 4f;

                float pupilY = centerY;

 

                float pupilWidth  = noseWidth  / 2f;

                float pupilHeight = noseHeight / 2f;

 

                RectangleF pupilRectangle = new RectangleF

                (

                    pupilX - pupilWidth  / 2f,

                    pupilY - pupilHeight / 2f,

                    pupilWidth,

                    pupilHeight

                );

 

                graphics.FillEllipse(Brushes.Black, pupilRectangle);

 

                centerX = boundRectangle.X + boundRectangle.Width  * 0.7f;

                centerY = boundRectangle.Y + boundRectangle.Height * 0.3f;

 

                eyeWidth  = noseWidth * 0.85f;

                eyeHeight = noseHeight * 0.75f;

 

                eyeRectangle = new RectangleF

                (

                    centerX - eyeWidth  / 2f,

                    centerY - eyeHeight / 2f,

                    eyeWidth,

                    eyeHeight

                );

 

                graphics.FillEllipse(Brushes.White, eyeRectangle);

 

                pen.Color = Color.Black;

 

                graphics.DrawEllipse(pen, eyeRectangle);

 

                pupilX = centerX + noseWidth / 4f;

                pupilY = centerY;

 

                pupilWidth  = noseWidth  / 2f;

                pupilHeight = noseHeight / 2f;

 

                pupilRectangle = new RectangleF

                (

                    pupilX - pupilWidth  / 2f,

                    pupilY - pupilHeight / 2f,

                    pupilWidth,

                    pupilHeight

                );

 

                graphics.FillEllipse(Brushes.Black, pupilRectangle);

            }

        }

 

        #endregion

        #region 그림 그리기 - DrawPicture(pictureBox, graphics)

 

        /// <summary>

        /// 그림 그리기

        /// </summary>

        /// <param name="pictureBox">픽처 박스</param>

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

        private void DrawPicture(PictureBox pictureBox, Graphics graphics)

        {

            graphics.Clear(pictureBox.BackColor);

 

            graphics.SmoothingMode = SmoothingMode.AntiAlias;

 

            DrawSmiley(graphics, pictureBox.ClientRectangle);

        }

 

        #endregion

    }

}

 

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

728x90
반응형
Posted by 사용자 icodebroker

댓글을 달아 주세요