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

■ 임의의 선으로 채워진 텍스트 그리기

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


TestProject.zip


MainForm.cs

 

 

using System;

using System.Drawing;

using System.Drawing.Drawing2D;

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();

 

            ResizeRedraw = true;

 

            #region 이벤트를 설정한다.

 

            Paint += Form_Paint;

 

            #endregion

        }

 

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

        {

            string text  = "TEST";

            int    count = 150;

 

            e.Graphics.TextRenderingHint = TextRenderingHint.AntiAliasGridFit;

 

            e.Graphics.Clear(this.BackColor);

 

            using(Font font = new Font("Times New Roman", 150, FontStyle.Bold, GraphicsUnit.Pixel))

            {

                using(GraphicsPath graphicsPath = new GraphicsPath())

                {

                    using(StringFormat stringFormat = new StringFormat())

                    {

                        stringFormat.Alignment     = StringAlignment.Center;

                        stringFormat.LineAlignment = StringAlignment.Center;

 

                        int width  = ClientSize.Width  / 2;

                        int height = ClientSize.Height / 2;

 

                        graphicsPath.AddString

                        (

                            text,

                            font.FontFamily,

                            (int)font.Style,

                            font.Size,

                            new Point(width, height),

                            stringFormat

                        );

                    }

 

                    using(Region region = new Region(graphicsPath))

                    {

                        e.Graphics.Clip = region;

 

                        Random rand = new Random();

 

                        int x0;

                        int y0;

                        int x1;

                        int y1;

 

                        x0 = 0;

                        x1 = ClientSize.Width;

 

                        for(int i = 1; i < count; i++)

                        {

                            y0 = rand.Next(0, ClientSize.Height);

                            y1 = rand.Next(0, ClientSize.Height);

 

                            e.Graphics.DrawLine(Pens.Black, x0, y0, x1, y1);

                        }

 

                        y0 = 0;

                        y1 = ClientSize.Height;

 

                        for(int i = 1; i < count; i++)

                        {

                            x0 = rand.Next(0, ClientSize.Width);

                            x1 = rand.Next(0, ClientSize.Width);

 

                            e.Graphics.DrawLine(Pens.Black, x0, y0, x1, y1);

                        }

 

                        e.Graphics.ResetClip();

                    }

                }

            }

        }

 

        #endregion

    }

}

 

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

Posted by 사용자 icodebroker

댓글을 달아 주세요