■ TextureBrush 클래스 : 여러 색상을 갖는 곡선 그리기

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


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

    {

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

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

 

        #region Field

 

        /// <summary>

        /// 포인트 리스트

        /// </summary>

        private Point[] pointList = null;

 

        #endregion

 

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

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

 

        #region 생성자 - MainForm()

 

        /// <summary>

        /// 생성자

        /// </summary>

        public MainForm()

        {

            InitializeComponent();

 

            #region 이벤트를 설정한다.

 

            Load                              += Form_Load;

            this.curveCheckBox.CheckedChanged += curveCheckBox_CheckedChanged;

            this.canvasPictureBox.Paint       += canvasPictureBox_Paint;

 

            #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)

        {

            Random random = new Random();

 

            this.pointList = new Point[20];

 

            for(int i = 0; i < this.pointList.Length; i++)

            {

                this.pointList[i] = new Point

                (

                    i * 5,

                    random.Next(5, 95)

                );

            }

        }

 

        #endregion

        #region 곡선 체크 박스 체크 변경시 처리하기 - curveCheckBox_CheckedChanged(sender, e)

 

        /// <summary>

        /// 곡선 체크 박스 체크 변경시 처리하기

        /// </summary>

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

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

        private void curveCheckBox_CheckedChanged(object sender, EventArgs e)

        {

            this.canvasPictureBox.Refresh();

        }

 

        #endregion

        #region 캔버스 픽처 박스 페인트시 처리하기 - canvasPictureBox_Paint(sender, e)

 

        /// <summary>

        /// 캔버스 픽처 박스 페인트시 처리하기

        /// </summary>

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

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

        private void canvasPictureBox_Paint(object sender, PaintEventArgs e)

        {

            e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;

 

            RectangleF worldRectangle = new RectangleF(0, 0, 100, 100);

 

            RectangleF deviceRectangle = new RectangleF

            (

                5,

                5,

                this.canvasPictureBox.ClientSize.Width  - 10,

                this.canvasPictureBox.ClientSize.Height - 10

            );

 

            SetTransformation(e.Graphics, worldRectangle, deviceRectangle, false, true);

 

            using(Pen pen = new Pen(Color.Black, 0))

            {

                for(int y = 10; y < 100; y += 10)

                {

                    e.Graphics.DrawLine(pen, -2, y, 2, y);

                }

 

                e.Graphics.DrawLine(pen, 0, 0, 0, 100);

 

                for(int x = 10; x < 100; x += 10)

                {

                    e.Graphics.DrawLine(pen, x, -2, x, 2);

                }

 

                e.Graphics.DrawLine(pen, 0, 0, 100, 0);

            }

 

            using(Bitmap bitmap = new Bitmap(100, 100))

            {

                using(Graphics graphics = Graphics.FromImage(bitmap))

                {

                    graphics.Clear(Color.White);

 

                    graphics.FillRectangle(Brushes.Red   , 0, 80, 100, 20);

                    graphics.FillRectangle(Brushes.Orange, 0, 60, 100, 20);

                    graphics.FillRectangle(Brushes.Yellow, 0, 40, 100, 20);

                    graphics.FillRectangle(Brushes.Green , 0, 20, 100, 20);

                    graphics.FillRectangle(Brushes.Blue  , 0, 0 , 100, 20);

                }

 

                using(TextureBrush brush = new TextureBrush(bitmap))

                {

                    using(Pen pen = new Pen(brush, 3))

                    {

                        pen.LineJoin = LineJoin.Bevel;

 

                        if(this.curveCheckBox.Checked)

                        {

                            e.Graphics.DrawCurve(pen, this.pointList);

                        }

                        else

                        {

                            e.Graphics.DrawLines(pen, this.pointList);

                        }

                    }

                }

            }

        }

 

        #endregion

 

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

 

        #region 변환 설정하기 - SetTransformation(graphics, worldRectangle, deviceRectangle, invertX, invertY)

 

        /// <summary>

        /// 변환 설정하기

        /// </summary>

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

        /// <param name="worldRectangle">세계 사각형</param>

        /// <param name="deviceRectangle">장치 사각형</param>

        /// <param name="invertX">X축 반전 여부</param>

        /// <param name="invertY">Y축 반전 여부</param>

        private void SetTransformation(Graphics graphics, RectangleF worldRectangle, RectangleF deviceRectangle, bool invertX, bool invertY)

        {

            PointF[] devicePointArray =

            {

                new PointF(deviceRectangle.Left , deviceRectangle.Top   ),

                new PointF(deviceRectangle.Right, deviceRectangle.Top   ),

                new PointF(deviceRectangle.Left , deviceRectangle.Bottom),

            };

 

            if(invertX)

            {

                devicePointArray[0].X = deviceRectangle.Right;

                devicePointArray[1].X = deviceRectangle.Left;

                devicePointArray[2].X = deviceRectangle.Right;

            }

 

            if(invertY)

            {

                devicePointArray[0].Y = deviceRectangle.Bottom;

                devicePointArray[1].Y = deviceRectangle.Bottom;

                devicePointArray[2].Y = deviceRectangle.Top;

            }

 

            graphics.Transform = new Matrix(worldRectangle, devicePointArray);

        }

 

        #endregion

    }

}

 

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

Posted by 사용자 icodebroker

댓글을 달아 주세요