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

728x90
반응형
728x170

TestProject.zip
다운로드

▶ OverlayTextHelper.cs

using System;
using System.Drawing;
using System.Drawing.Imaging;

namespace TestProject
{
    /// <summary>
    /// 오버레이 텍스트 비트맵 헬퍼
    /// </summary>
    public class OverlayTextBitmapHelper
    {
        //////////////////////////////////////////////////////////////////////////////////////////////////// Method
        ////////////////////////////////////////////////////////////////////////////////////////// Static
        //////////////////////////////////////////////////////////////////////////////// Public

        #region 비트맵 구하기 - GetBitmap(image, text, font, color, addAlpha, addShadow, textAlignment, fillRatio, applyZoom)

        /// <summary>
        /// 비트맵 구하기
        /// </summary>
        /// <param name="image">이미지</param>
        /// <param name="text">텍스트</param>
        /// <param name="font">폰트</param>
        /// <param name="color">색상</param>
        /// <param name="addAlpha">투명도 추가 여부</param>
        /// <param name="addShadow">그리잠 추가 여부</param>
        /// <param name="textAlignment">텍스트 정렬</param>
        /// <param name="fillRatio">채우기 비율</param>
        /// <param name="applyZoom">확대/축소 적용 여부</param>
        /// <returns>비트맵</returns>
        public static Bitmap GetBitmap(Image image, string text, Font font, Color color, bool addAlpha, bool addShadow, ContentAlignment textAlignment,
            float fillRatio, bool applyZoom)
        {
            if(string.IsNullOrWhiteSpace(text) || fillRatio == 0)
            {
                return new Bitmap(image);
            }

            Bitmap targetBitmap = new Bitmap(image.Width, image.Height, PixelFormat.Format24bppRgb);

            Graphics targetGraphics = Graphics.FromImage(targetBitmap);

            targetGraphics.DrawImage(image, 0, 0);

            int alpha = 255;

            if(addAlpha)
            {
                alpha = 90 + (text.Length * 2);

                if(alpha >= 255)
                {
                    alpha = 255;
                }
            }

            SolidBrush brush = new SolidBrush(Color.FromArgb(alpha, color));

            StringFormat stringFormat = StringFormat.GenericTypographic;

            SizeF textSize = targetGraphics.MeasureString(text, font, 100000, stringFormat);

            float zoom = applyZoom ? (float)(Math.Sqrt(((double)(image.Width * image.Height) * fillRatio) / (double)(textSize.Width * textSize.Height))) : 1f;

            FontStyle fontStyle = font.Style;

            Font actualFont = new Font(font.FontFamily, ((float)font.Size) * zoom, fontStyle);

            int   characterFit;
            int   lineFit;
            float sqrtFill = (float)(Math.Sqrt(fillRatio));

            stringFormat.FormatFlags = StringFormatFlags.NoClip;
            stringFormat.Trimming    = StringTrimming.Word;

            SizeF layoutSize = new SizeF(((float)image.Width) * sqrtFill, ((float)image.Height) * 1.5f);

            textSize = targetGraphics.MeasureString(text, actualFont, layoutSize, stringFormat, out characterFit, out lineFit);

            if((textSize.Height > (float)(image.Height) * sqrtFill) || (textSize.Width > layoutSize.Width))
            {
                do 
                {
                    zoom = Math.Max((textSize.Height / (((float)image.Height) * sqrtFill)), textSize.Width / layoutSize.Width);
                    zoom = actualFont.Size / zoom;

                    if(zoom > 16f)
                    {
                        zoom = (float)(Math.Floor(zoom));
                    }

                    if(zoom >= actualFont.Size)
                    {
                        zoom -= 1;
                    }

                    actualFont = new Font(font.FontFamily, zoom, fontStyle);

                    textSize = targetGraphics.MeasureString
                    (
                        text,
                        actualFont,
                        layoutSize,
                        stringFormat,
                        out characterFit,
                        out lineFit
                    );

                    if(zoom <= 1)
                    {
                        break;
                    }
                }
                while((textSize.Height > (float)(image.Height) * sqrtFill) || (textSize.Width > layoutSize.Width));
            }

            RectangleF rectangle;

            switch(textAlignment)
            {
                case ContentAlignment.TopLeft :

                    rectangle = new RectangleF
                    (
                        actualFont.Size * 0.15f,
                        (actualFont.Size * 0.1f),
                        layoutSize.Width,
                        ((float)image.Height) * sqrtFill
                    );

                    break;

                case ContentAlignment.TopCenter :

                    rectangle = new RectangleF
                    (
                        (targetBitmap.Width - textSize.Width) / 2f,
                        (actualFont.Size * 0.1f),
                        layoutSize.Width,
                        ((float)image.Height) * sqrtFill
                    );

                    break;

                case ContentAlignment.TopRight :

                    rectangle = new RectangleF
                    (
                        (targetBitmap.Width - textSize.Width) - (actualFont.Size * 0.1f),
                        (actualFont.Size * 0.1f),
                        layoutSize.Width,
                        ((float)image.Height) * sqrtFill
                    );

                    break;

                case ContentAlignment.MiddleLeft :

                    rectangle = new RectangleF
                    (
                        actualFont.Size * 0.15f,
                        (targetBitmap.Height - textSize.Height) / 2f,
                        layoutSize.Width,
                        ((float)image.Height) * sqrtFill
                    );

                    break;

                case ContentAlignment.MiddleCenter :

                    rectangle = new RectangleF
                    (
                        (targetBitmap.Width - textSize.Width) / 2f,
                        (targetBitmap.Height - textSize.Height) / 2f,
                        layoutSize.Width,
                        ((float)image.Height) * sqrtFill
                    );

                    break;

                case ContentAlignment.MiddleRight :

                    rectangle = new RectangleF
                    (
                        (targetBitmap.Width - textSize.Width) - (actualFont.Size * 0.1f),
                        (targetBitmap.Height - textSize.Height) / 2f,
                        layoutSize.Width,
                        ((float)image.Height) * sqrtFill
                    );

                    break;

                case ContentAlignment.BottomLeft :

                    rectangle = new RectangleF
                    (
                        actualFont.Size * 0.15f,
                        (targetBitmap.Height - textSize.Height) - (actualFont.Size * 0.1f),
                        layoutSize.Width,
                        ((float)image.Height) * sqrtFill
                    );

                    break;

                case ContentAlignment.BottomCenter :

                    rectangle = new RectangleF
                    (
                        (targetBitmap.Width - textSize.Width) / 2f,
                        (targetBitmap.Height - textSize.Height) - (actualFont.Size * 0.1f),
                        layoutSize.Width,
                        ((float)image.Height) * sqrtFill
                    );

                    break;

                case ContentAlignment.BottomRight :

                    rectangle = new RectangleF
                    (
                        (targetBitmap.Width - textSize.Width) - (actualFont.Size * 0.1f),
                        (targetBitmap.Height - textSize.Height) - (actualFont.Size * 0.1f),
                        layoutSize.Width,
                        ((float)image.Height) * sqrtFill
                    );

                    break;

                default :

                    rectangle = new RectangleF
                    (
                        (targetBitmap.Width - textSize.Width) / 2f,
                        (targetBitmap.Height - textSize.Height) / 2f,
                        layoutSize.Width,
                        ((float)image.Height) * sqrtFill
                    );

                    break;
            } 

            targetGraphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;

            if(addShadow)
            {
                SolidBrush shadowBrush = new SolidBrush(Color.FromArgb((int)(alpha / 2.0), color));

                RectangleF shadowRectangle = new RectangleF
                (
                    (float)rectangle.X - (actualFont.Size * 0.1f),
                    (float)rectangle.Y - (actualFont.Size * 0.1f),
                    rectangle.Width,
                    rectangle.Height
                );

                targetGraphics.DrawString(text, actualFont, shadowBrush, shadowRectangle, stringFormat);
            }

            targetGraphics.DrawString(text, actualFont, brush, rectangle, stringFormat);

            targetGraphics.Dispose();

            brush.Dispose();

            actualFont.Dispose();

            return targetBitmap;
        }

        #endregion
        #region 비트맵 구하기 - GetBitmap(image, text, font, color, addAlpha, addShadow)

        /// <summary>
        /// 비트맵 구하기
        /// </summary>
        /// <param name="image">이미지</param>
        /// <param name="text">텍스트</param>
        /// <param name="font">폰트</param>
        /// <param name="color">색상</param>
        /// <param name="addAlpha">투명도 추가 여부</param>
        /// <param name="addShadow">그림자 추가 여부</param>
        /// <returns>비트맵</returns>
        public static Bitmap GetBitmap(Image image, string text, Font font, Color color, bool addAlpha, bool addShadow)
        {
            return GetBitmap(image, text, font, color, addAlpha, addShadow, ContentAlignment.MiddleCenter, 0.8f, true);
        }

        #endregion
    }
}

 

728x90

 

▶ MainForm.cs

using System.Drawing;
using System.Windows.Forms;

namespace TestProject
{
    /// <summary>
    /// 메인 폼
    /// </summary>
    public partial class MainForm : Form
    {
        //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor
        ////////////////////////////////////////////////////////////////////////////////////////// Public

        #region 생성자 - MainForm()

        /// <summary>
        /// 생성자
        /// </summary>
        public MainForm()
        {
            InitializeComponent();

            Bitmap sourceBitmap = Bitmap.FromFile("maple.png") as Bitmap;

            Bitmap targetBitmap = OverlayTextBitmapHelper.GetBitmap
            (
                sourceBitmap,
                "오버레이 텍스트",
                new Font("나눔고딕코딩", 48f, FontStyle.Bold),
                Color.Beige,
                false,
                false,
                ContentAlignment.BottomRight,
                0.95f,
                false
            );

            this.pictureBox.Image = targetBitmap;
        }

        #endregion
    }
}
728x90
반응형
그리드형
Posted by 사용자 icodebroker

댓글을 달아 주세요