■ 팬시 텍스트 만들기

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


TestProject.zip


FontHelper.cs

 

 

using System;

using System.Drawing;

using System.Drawing.Text;

using System.Drawing.Drawing2D;

 

namespace TestProject

{

    /// <summary>

    /// 폰트 헬퍼

    /// </summary>

    public class FontHelper

    {

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

        ////////////////////////////////////////////////////////////////////////////////////////// Static

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

 

        #region Field

 

        /// <summary>

        /// BLUR 크기

        /// </summary>

        private static readonly int BLUR_AMOUNT = 6;

 

        #endregion

 

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

        ////////////////////////////////////////////////////////////////////////////////////////// Static

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

 

        #region 팬시 텍스트 이미지 구하기 - GetFancyTextImage(text, font, foregroundColor, backgroundColor)

 

        /// <summary>

        /// 팬시 텍스트 이미지 구하기

        /// </summary>

        /// <param name="text">텍스트</param>

        /// <param name="font">폰트</param>

        /// <param name="foregroundColor">전경 색상</param>

        /// <param name="backgroundColor">배경 색상</param>

        /// <returns>팬시 텍스트 이미지</returns>

        public static Bitmap GetFancyTextImage(string text, Font font, Color foregroundColor, Color backgroundColor)

        {

            Bitmap targetBitmap = null;

 

            using(Graphics desktopGraphics = Graphics.FromHwnd(IntPtr.Zero))

            {

                SizeF textSize = desktopGraphics.MeasureString(text, font);

 

                using(Bitmap textBitmap = new Bitmap((int)textSize.Width,(int)textSize.Height))

                {

                    using(Graphics textBitmapGraphics = Graphics.FromImage(textBitmap))

                    {

                        using(SolidBrush backgroundBrush = new SolidBrush(Color.FromArgb(16, backgroundColor.R, backgroundColor.G, 

                            backgroundColor.B)))

                        {

                            using(SolidBrush foregroundBrush = new SolidBrush(foregroundColor))

                            {

                                textBitmapGraphics.SmoothingMode     = SmoothingMode.HighQuality;

                                textBitmapGraphics.InterpolationMode = InterpolationMode.HighQualityBilinear;

                                textBitmapGraphics.TextRenderingHint = TextRenderingHint.AntiAliasGridFit;

 

                                textBitmapGraphics.DrawString(text, font, backgroundBrush, 0, 0);

    

                                targetBitmap = new Bitmap(textBitmap.Width + BLUR_AMOUNT, textBitmap.Height + BLUR_AMOUNT);

 

                                using(Graphics targetBitmapGraphics = Graphics.FromImage(targetBitmap))

                                {

                                    targetBitmapGraphics.SmoothingMode     = SmoothingMode.HighQuality;

                                    targetBitmapGraphics.InterpolationMode = InterpolationMode.HighQualityBilinear;                        

                                    targetBitmapGraphics.TextRenderingHint = TextRenderingHint.AntiAliasGridFit;

 

                                    for(int x = 0; x <= BLUR_AMOUNT; x++)

                                    {

                                        for(int y = 0; y <= BLUR_AMOUNT; y++)

                                        {

                                            targetBitmapGraphics.DrawImageUnscaled(textBitmap, x, y);

                                        }

                                    }

 

                                    targetBitmapGraphics.DrawString(text, font, foregroundBrush, BLUR_AMOUNT/2, BLUR_AMOUNT/2);

                                }

                            }

                        }

                    }

                }

            }

            

            return targetBitmap;

        }

 

        #endregion

    }

}

 

 

MainForm.cs

 

 

using System.Drawing;

using System.Windows.Forms;

 

namespace TestProject

{

    /// <summary>

    /// 메인 폼

    /// </summary>

    public partial class MainForm : Form

    {

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

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

 

        #region Field

 

        /// <summary>

        /// 텍스트 비트맵

        /// </summary>

        private Bitmap textBitmap;

 

        #endregion

 

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

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

 

        #region 생성자 - MainForm()

 

        /// <summary>

        /// 생성자

        /// </summary>

        public MainForm()

        {

            InitializeComponent();

 

            using(Font font = new Font("Arial", 30, FontStyle.Bold))

            {

                this.textBitmap = FontHelper.GetFancyTextImage("Hello World!", font, Color.Red, Color.Blue);

            }

        }

 

        #endregion

 

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

        ////////////////////////////////////////////////////////////////////////////////////////// Protected

 

        #region 페인트시 처리하기 - OnPaint(e)

 

        /// <summary>

        /// 페인트시 처리하기

        /// </summary>

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

        protected override void OnPaint(PaintEventArgs e)

        {

            base.OnPaint (e);

 

            e.Graphics.DrawImageUnscaled(textBitmap, 50, 50);

        }

 

        #endregion

    }

}

 

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

Posted by 사용자 icodebroker

댓글을 달아 주세요