첨부 실행 코드는 나눔고딕코딩 폰트를 사용합니다.
본 블로그는 광고를 포함하고 있습니다.
광고 클릭에서 발생하는 수익금은 모두 블로그 콘텐츠 향상을 위해 쓰여집니다.

728x90
반응형
728x170

TestProject.zip
다운로드

▶ MainForm.cs

using System;
using System.Collections.Generic;
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();

            this.pictureBox.Resize += pictureBox_Resize;
            this.pictureBox.Paint  += pictureBox_Paint;
        }

        #endregion

        //////////////////////////////////////////////////////////////////////////////////////////////////// Method
        ////////////////////////////////////////////////////////////////////////////////////////// Private
        //////////////////////////////////////////////////////////////////////////////// Event

        #region 픽처 박스 크기 변경시 처리하기 - pictureBox_Resize(sender, e)

        /// <summary>
        /// 픽처 박스 크기 변경시 처리하기
        /// </summary>
        /// <param name="sender">이벤트 발생자</param>
        /// <param name="e">이벤트 인자</param>
        private void pictureBox_Resize(object sender, EventArgs e)
        {
            this.pictureBox.Refresh();
        }

        #endregion
        #region 픽처 박스 페인트시 처리하기 - pictureBox_Paint(sender, e)

        /// <summary>
        /// 픽처 박스 페인트시 처리하기
        /// </summary>
        /// <param name="sender">이벤트 발생자</param>
        /// <param name="e">이벤트 인자</param>
        private void pictureBox_Paint(object sender, PaintEventArgs e)
        {
            using(Font font = new Font("나눔고딕코딩", 40, FontStyle.Bold))
            {
                Color topBackgroundColor    = Color.RoyalBlue;
                Color topForegroundColor    = Color.White;
                Color bottomBackgroundColor = Color.White;
                Color bottomForegroundColor = Color.RoyalBlue;

                SolidBrush topBackgroundBrush    = new SolidBrush(topBackgroundColor   );
                SolidBrush topForegroundBrush    = new SolidBrush(topForegroundColor   );
                SolidBrush bottomBackgroundBrush = new SolidBrush(bottomBackgroundColor);
                SolidBrush bottomForegroundBrush = new SolidBrush(bottomForegroundColor);
 
                DrawSineSplitText
                (
                    e.Graphics,
                    "싸인파 반전 텍스트",
                    font,
                    this.pictureBox.ClientRectangle,
                    topBackgroundBrush,
                    topForegroundBrush,
                    bottomBackgroundBrush,
                    bottomForegroundBrush,
                    2.5f,
                    0.25f
                );

                topBackgroundBrush.Dispose();
                topForegroundBrush.Dispose();
                bottomBackgroundBrush.Dispose();
                bottomForegroundBrush.Dispose();
            }
        }

        #endregion

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

        #region 싸인파 분리 텍스트 그리기 - DrawSineSplitText(graphics, text, font, rectangle,
            topBackgroundBrush, topForegroundBrush, bottomBackgroundBrush, bottomForegroundBrush, waveCount, scaleY)

        /// <summary>
        /// 싸인파 분리 텍스트 그리기
        /// </summary>
        /// <param name="graphics">그래픽스</param>
        /// <param name="text">텍스트</param>
        /// <param name="font">폰트</param>
        /// <param name="rectangle">사각형</param>
        /// <param name="topBackgroundBrush">상단 배경 브러시</param>
        /// <param name="topForegroundBrush">상단 전경 브러시</param>
        /// <param name="bottomBackgroundBrush">하단 배경 브러시</param>
        /// <param name="bottomForegroundBrush">하단 전경 브러시</param>
        /// <param name="waveCount">웨이브 수</param>
        /// <param name="scaleY">Y 축적</param>
        private void DrawSineSplitText
        (
            Graphics  graphics,
            string    text,
            Font      font,
            Rectangle rectangle,
            Brush     topBackgroundBrush,
            Brush     topForegroundBrush,
            Brush     bottomBackgroundBrush,
            Brush     bottomForegroundBrush,
            float     waveCount,
            float     scaleY
        )
        {
            Bitmap topBitmap    = new Bitmap(rectangle.Width, rectangle.Height);
            Bitmap bottomBitmap = new Bitmap(rectangle.Width, rectangle.Height);

            using(StringFormat stringFormat = new StringFormat())
            {
                stringFormat.Alignment     = StringAlignment.Center;
                stringFormat.LineAlignment = StringAlignment.Center;

                using(Graphics topGraphics = Graphics.FromImage(topBitmap))
                {
                    topGraphics.TextRenderingHint = TextRenderingHint.AntiAliasGridFit;

                    topGraphics.FillRectangle(topBackgroundBrush, rectangle);

                    topGraphics.DrawString(text, font, topForegroundBrush, rectangle, stringFormat);
                }

                using(Graphics bottomGraphics = Graphics.FromImage(bottomBitmap))
                {
                    bottomGraphics.TextRenderingHint = TextRenderingHint.AntiAliasGridFit;

                    bottomGraphics.FillRectangle(bottomBackgroundBrush, rectangle);

                    bottomGraphics.DrawString(text, font, bottomForegroundBrush, rectangle, stringFormat);
                }
            }

            using(TextureBrush brush = new TextureBrush(topBitmap))
            {
                graphics.FillRectangle(brush, rectangle);
            }

            List<PointF> pointList = new List<PointF>();

            float magnitude = (font.Size * 96f / 72f) / 2 * scaleY;
            float offsetY   = rectangle.Height / 2f;

            pointList.Add(new PointF(0, rectangle.Height));

            float scaleX = (float)(waveCount * 2 * Math.PI / rectangle.Width);

            for(int x = 0; x < rectangle.Width; x++)
            {
                float y = (float)(offsetY + magnitude * Math.Sin(x * scaleX));

                pointList.Add(new PointF(x, y));            
            }

            pointList.Add(new PointF(rectangle.Width - 1, rectangle.Height));

            using(TextureBrush brush = new TextureBrush(bottomBitmap))
            {
                graphics.SmoothingMode = SmoothingMode.AntiAlias;

                graphics.FillPolygon(brush, pointList.ToArray());
            }

            topBitmap.Dispose();
            bottomBitmap.Dispose();
        }

        #endregion
    }
}
728x90
반응형
그리드형(광고전용)
Posted by icodebroker

댓글을 달아 주세요