NEWS

[C#] Chia sẽ code tạo Text Effect trong lập trình Winform

[C#] Chia sẽ code tạo Text Effect trong lập trình Winform
Đăng bởi: Thảo Meo - Lượt xem: 3698 09:11:13, 26/04/2021C#   In bài viết

Xin chào các bạn, bài viết hôm nay mình chia sẻ đến các bạn, 8 đoạn code tạo hình ảnh từ Text có hiệu ứng trong lập trình C#, Winform.

[C#] Text Effect Winform

Trong bài này, mình sẽ sử dụng thư viện: TextdesignerCSLibrary

Các bạn có thể download ở source code của mình

Dưới đây là hình ảnh các hiệu ứng chữ C# winform: Trông cũng giống WordArt trong Microsoft Word. ^^

text_effect

Full source code Text Effect C#:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using TextDesignerCSLibrary;

namespace testeffect
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            pictureBox1.Image = TextToImage();
            pictureBox2.Image = TextToImage2();
            pictureBox3.Image = TextToImage3();
            pictureBox4.Image = TextToImage4();
            pictureBox5.Image = TextToImage5();
            pictureBox6.Image = TextToImage6();
            pictureBox7.Image = TextToImage7();
            pictureBox8.Image = TextToImage8();
        }

      
        public Image TextToImage()
        {
            using (Graphics g = Graphics.FromHwnd(IntPtr.Zero))
            {
                string szbuf = "LAPTRINHVB.NET";
                FontFamily fontFamily = new FontFamily("Roboto");
                StringFormat strformat = new StringFormat();
                var font = new Font("Roboto", 48, FontStyle.Bold);
                SizeF sz = g.MeasureString(szbuf, font);
                Bitmap bmp = new Bitmap((int)sz.Width, (int)sz.Height);
                    using (Graphics gBmp = Graphics.FromImage(bmp))
                    {
                       gBmp.SmoothingMode = SmoothingMode.AntiAlias;
                       gBmp.InterpolationMode = InterpolationMode.HighQualityBicubic;
                        OutlineText text = new OutlineText();
                        text.TextDblOutline(Color.FromArgb(255, 255, 255),
                            Color.FromArgb(0, 128, 128), Color.FromArgb(0, 255, 0), 4, 4);
                        text.EnableShadow(true);
                        text.SetShadowBkgd(Color.FromArgb(255, 128, 192), bmp.Width, bmp.Height);
                        text.Shadow(Color.FromArgb(128, 0, 0, 0), 4, new Point(4, 8));
                        text.DrawString(gBmp, fontFamily,
                            FontStyle.Bold, 48, szbuf, new Point(10, 10), strformat);
                    }
                    return bmp;
               
            }
        }

        public Image TextToImage2()
        {
            using (Graphics g = Graphics.FromHwnd(IntPtr.Zero))
            {
                string szbuf = "LAPTRINHVB.NET";
                FontFamily fontFamily = new FontFamily("Roboto");
                StringFormat strformat = new StringFormat();
                var font = new Font("Roboto", 48, FontStyle.Bold);
                SizeF sz = g.MeasureString(szbuf, font);
                Bitmap bmp = new Bitmap((int)sz.Width, (int)sz.Height);
                using (Graphics gBmp = Graphics.FromImage(bmp))
                {
                    gBmp.SmoothingMode = SmoothingMode.AntiAlias;
                    gBmp.InterpolationMode = InterpolationMode.HighQualityBicubic;

                  

                    OutlineText text = new OutlineText();
                    text.TextGlow(Color.FromArgb(191, 255, 255), Color.FromArgb(24, 0, 128, 128), 14);
                    text.EnableShadow(false);
                    text.DrawString(gBmp, fontFamily, FontStyle.Bold,
                        48, szbuf, new Point(10, 10), strformat);

                  
                }
                return bmp;

            }
        }

        public Image TextToImage3()
        {
            using (Graphics g = Graphics.FromHwnd(IntPtr.Zero))
            {
                string szbuf = "LAPTRINHVB.NET";
                FontFamily fontFamily = new FontFamily("Roboto");
                StringFormat strformat = new StringFormat();
                var font = new Font("Roboto", 48, FontStyle.Bold);
                SizeF sz = g.MeasureString(szbuf, font);
                Bitmap bmp = new Bitmap((int)sz.Width, (int)sz.Height);
                using (Graphics gBmp = Graphics.FromImage(bmp))
                {
                    gBmp.SmoothingMode = SmoothingMode.AntiAlias;
                    gBmp.InterpolationMode = InterpolationMode.HighQualityBicubic;
                    OutlineText outlineText = new OutlineText();
                    outlineText.TextOutline(
                        Color.FromArgb(255, 128, 192),
                        Color.FromArgb(255, 128, 0, 0),
                        4);

                    outlineText.EnableShadow(true);
                    //Rem to SetNullShadow() to release memory if a previous shadow has been set.
                    outlineText.SetNullShadow();
                    outlineText.Extrude(
                        Color.FromArgb(255, 128, 0, 0),
                        4,
                        new Point(8, 8));

                    Color m_clrBkgd = Color.FromArgb(255, 255, 255);
                    outlineText.SetShadowBkgd(m_clrBkgd, bmp.Width, bmp.Height);
                    

                    StringFormat strFormat = new StringFormat();
                    outlineText.DrawString(gBmp, fontFamily,
                        FontStyle.Regular, 48, szbuf,
                        new Point(10, 10), strFormat);

                  
                }
                return bmp;

            }
        }

        public Image TextToImage4()
        {
            using (Graphics g = Graphics.FromHwnd(IntPtr.Zero))
            {
                string szbuf = "LAPTRINHVB.NET";
                FontFamily fontFamily = new FontFamily("Roboto");
                StringFormat strformat = new StringFormat();
                var font = new Font("Roboto", 48, FontStyle.Bold);
                SizeF sz = g.MeasureString(szbuf, font);
                Bitmap bmp = new Bitmap((int)sz.Width, (int)sz.Height);
                using (Graphics gBmp = Graphics.FromImage(bmp))
                {
                    gBmp.SmoothingMode = SmoothingMode.AntiAlias;
                    gBmp.InterpolationMode = InterpolationMode.HighQualityBicubic;

                    //Drawing the back ground color
                    Color m_clrBkgd = Color.FromArgb(255, 255, 255);
                    SolidBrush brushBkgnd = new SolidBrush(m_clrBkgd);
                    gBmp.FillRectangle(brushBkgnd, 0, 0, this.ClientSize.Width, this.ClientSize.Width);

                    PngOutlineText m_PngOutlineText = new PngOutlineText();
                    m_PngOutlineText.SetPngImage(new Bitmap(ClientSize.Width, ClientSize.Height));
                    m_PngOutlineText.TextGradOutline(
                        Color.FromArgb(255, 128, 64),
                        Color.FromArgb(255, 64, 0, 64),
                        Color.FromArgb(255, 255, 128, 255),
                        10, GradientType.Linear);
                    m_PngOutlineText.EnableReflection(false);

                    m_PngOutlineText.EnableShadow(true);
                    //Rem to SetNullShadow() to release memory if a previous shadow has been set.
                    m_PngOutlineText.SetNullShadow();
                    m_PngOutlineText.Shadow(
                        Color.FromArgb(128, 0, 0, 0), 8,
                        new Point(4, 4));
                    LOGFONT m_LogFont = new LOGFONT();
                    m_LogFont.lfFaceName = "Arial Black";
                    m_LogFont.lfHeight = -48;

                    m_LogFont.lfOrientation = 0;
                    m_LogFont.lfEscapement = 0;
                    m_LogFont.lfItalic = false;
                    float fStartX = 0.0f;
                    float fStartY = 0.0f;
                    float fDestWidth = 0.0f;
                    float fDestHeight = 0.0f;
                    m_PngOutlineText.GdiMeasureString(
                        gBmp,
                        m_LogFont,
                        szbuf,
                        new Point(10, 10),
                        ref fStartX,
                        ref fStartY,
                        ref fDestWidth,
                        ref fDestHeight);
                    m_PngOutlineText.SetShadowBkgd(m_clrBkgd, (int)fDestWidth + 10, (int)fDestHeight + 10);
                    LinearGradientBrush gradientBrush = new LinearGradientBrush(new RectangleF(fStartX, fStartY, fDestWidth - (fStartX - 10), fDestHeight - (fStartY - 10)),
                            Color.FromArgb(255, 128, 64), Color.FromArgb(255, 0, 0), LinearGradientMode.Vertical);
                    m_PngOutlineText.TextGradOutline(
                        gradientBrush,
                        Color.FromArgb(255, 64, 0, 64),
                        Color.FromArgb(255, 255, 128, 255),
                        10, GradientType.Linear);

                    m_PngOutlineText.GdiDrawString(
                        gBmp,
                        m_LogFont,
                        szbuf,
                        new Point(10, 10));

                    gBmp.DrawImage(m_PngOutlineText.GetPngImage(), new Point(0, 0));

                

                }
                return bmp;

            }
        }
        public Image TextToImage5()
        {
            using (Graphics g = Graphics.FromHwnd(IntPtr.Zero))
            {
                string szbuf = "LAPTRINHVB.NET";
                FontFamily fontFamily = new FontFamily("Roboto");
                StringFormat strformat = new StringFormat();
                var font = new Font("Roboto", 48, FontStyle.Bold);
                SizeF sz = g.MeasureString(szbuf, font);
                Bitmap bmp = new Bitmap((int)sz.Width, (int)sz.Height);
                using (Graphics gBmp = Graphics.FromImage(bmp))
                {
                    gBmp.SmoothingMode = SmoothingMode.AntiAlias;
                    gBmp.InterpolationMode = InterpolationMode.HighQualityBicubic;

                    SolidBrush brushWhite = new SolidBrush(Color.White);
                    gBmp.FillRectangle(brushWhite, 0, 0,
                    bmp.Width, bmp.Height);                

                    GraphicsPath path = new GraphicsPath();
                    path.AddString(szbuf, fontFamily,
                        (int)FontStyle.Bold, 48.0f, new Point(10, 10), strformat);
                    Pen pen = new Pen(Color.FromArgb(0, 0, 160), 5);
                    pen.LineJoin = LineJoin.Round;
                    gBmp.DrawPath(pen, path);
                    LinearGradientBrush brush = new LinearGradientBrush(new Rectangle(10, 10, 30, 70),
                        Color.FromArgb(132, 200, 251),
                        Color.FromArgb(0, 0, 160), LinearGradientMode.Vertical);
                    gBmp.FillPath(brush, path);

                    brushWhite.Dispose();
                    fontFamily.Dispose();
                    path.Dispose();
                    pen.Dispose();
                    brush.Dispose();
                    gBmp.Dispose();


                }
                return bmp;

            }
        }

        public Image TextToImage6()
        {
            using (Graphics g = Graphics.FromHwnd(IntPtr.Zero))
            {
                string szbuf = "LAPTRINHVB.NET";
                FontFamily fontFamily = new FontFamily("Roboto");
                StringFormat strformat = new StringFormat();
                var font = new Font("Roboto", 48, FontStyle.Bold);
                SizeF sz = g.MeasureString(szbuf, font);
                Bitmap bmp = new Bitmap((int)sz.Width, (int)sz.Height);
                using (Graphics gBmp = Graphics.FromImage(bmp))
                {
                    gBmp.SmoothingMode = SmoothingMode.AntiAlias;
                    gBmp.InterpolationMode = InterpolationMode.HighQualityBicubic;

                    SolidBrush brushWhite = new SolidBrush(Color.White);
                    gBmp.FillRectangle(brushWhite, 0, 0,
                   bmp.Width, bmp.Height);

                    GraphicsPath path = new GraphicsPath();
                    path.AddString(szbuf, fontFamily,
                    (int)FontStyle.Regular, 48.0f, new Point(10, 10), strformat);

                    Pen penOut = new Pen(Color.FromArgb(32, 117, 81), 12);
                    penOut.LineJoin = LineJoin.Round;
                    gBmp.DrawPath(penOut, path);

                    Pen pen = new Pen(Color.FromArgb(234, 137, 6), 6);
                    pen.LineJoin = LineJoin.Round;
                    gBmp.DrawPath(pen, path);
                    SolidBrush brush = new SolidBrush(Color.FromArgb(128, 0, 255));
                    gBmp.FillPath(brush, path);

                    brushWhite.Dispose();
                    fontFamily.Dispose();
                    path.Dispose();
                    penOut.Dispose();
                    pen.Dispose();
                    brush.Dispose();
                    gBmp.Dispose();


                }
                return bmp;

            }
        }

        public Image TextToImage7()
        {
            using (Graphics g = Graphics.FromHwnd(IntPtr.Zero))
            {
                string szbuf = "LAPTRINHVB.NET";
                FontFamily fontFamily = new FontFamily("Roboto");
                StringFormat strformat = new StringFormat();
                var font = new Font("Roboto", 48, FontStyle.Bold);
                SizeF sz = g.MeasureString(szbuf, font);
                Bitmap bmp = new Bitmap((int)sz.Width, (int)sz.Height);
                using (Graphics gBmp = Graphics.FromImage(bmp))
                {
                    gBmp.SmoothingMode = SmoothingMode.AntiAlias;
                    gBmp.InterpolationMode = InterpolationMode.HighQualityBicubic;

                    SolidBrush brushWhite = new SolidBrush(Color.White);
                    gBmp.FillRectangle(brushWhite, 0, 0,
                    bmp.Width, bmp.Height);

                    GraphicsPath path = new GraphicsPath();
                    path.AddString(szbuf, fontFamily,
                    (int)FontStyle.Regular, 48.0f, new Point(10, 10), strformat);

                    for (int i = 1; i < 8; ++i)
                    {
                        Pen pen = new Pen(Color.FromArgb(32, 0, 128, 192), i);
                        pen.LineJoin = LineJoin.Round;
                        gBmp.DrawPath(pen, path);
                        pen.Dispose();
                    }

                    SolidBrush brush = new SolidBrush(Color.FromArgb(255, 255, 255));
                    gBmp.FillPath(brush, path);

                    brushWhite.Dispose();
                    fontFamily.Dispose();
                    path.Dispose();
                    brush.Dispose();
                    gBmp.Dispose();
                }
                return bmp;

            }
        }

        public Image TextToImage8()
        {
            using (Graphics g = Graphics.FromHwnd(IntPtr.Zero))
            {
                string szbuf = "LAPTRINHVB.NET";
                FontFamily fontFamily = new FontFamily("Roboto");
                StringFormat strformat = new StringFormat();
                var font = new Font("Roboto", 48, FontStyle.Bold);
                SizeF sz = g.MeasureString(szbuf, font);
                Bitmap bmp = new Bitmap((int)sz.Width, (int)sz.Height);
                using (Graphics gBmp = Graphics.FromImage(bmp))
                {
                    gBmp.SmoothingMode = SmoothingMode.AntiAlias;
                    gBmp.InterpolationMode = InterpolationMode.HighQualityBicubic;
                    OutlineText outlineText = new OutlineText();
                    outlineText.TextOutline(
                        Color.FromArgb(255, 128, 192),
                        Color.FromArgb(255, 0, 0, 160),
                        4);

                    outlineText.EnableShadow(true);
                    //Rem to SetNullShadow() to release memory if a previous shadow has been set.
                    outlineText.SetNullShadow();
                    outlineText.Shadow(Color.FromArgb(128, 0, 0, 0), 4, new Point(4, 8));

                    Color m_clrBkgd = Color.FromArgb(255, 255, 255);
                    outlineText.SetShadowBkgd(m_clrBkgd, bmp.Width, bmp.Height);
                    StringFormat strFormat = new StringFormat();

                    float fDestWidth = 0.0f;
                    float fDestHeight = 0.0f;
                    float fDestStart = 0.0f;
                    float fDestEnd = 0.0f;

                    outlineText.MeasureString(
                        gBmp,
                        fontFamily,
                        FontStyle.Italic,
                        48,
                        szbuf,
                        new Point(10, 10),
                        strFormat,
                        ref fDestStart,
                        ref fDestEnd,
                        ref fDestWidth,
                        ref fDestHeight);

                    LinearGradientBrush brush = new LinearGradientBrush(new Rectangle(10, 10,
                                (int)fDestWidth, (int)fDestHeight),
                        Color.FromArgb(132, 200, 251), Color.FromArgb(0, 0, 160),
                        System.Drawing.Drawing2D.LinearGradientMode.Vertical);

                    outlineText.TextOutline(
                        brush,
                        Color.FromArgb(255, 0, 0, 160),
                        4);

                    outlineText.DrawString(gBmp, fontFamily,
                        FontStyle.Italic, 48, szbuf,
                        new Point(10, 10), strFormat);

                    gBmp.Dispose();
                }
                return bmp;

            }
        }


    }
}

Thanks for watching!

DOWNLOAD SOURCE

THÔNG TIN TÁC GIẢ

BÀI VIẾT LIÊN QUAN

[C#] Chia sẽ code tạo Text Effect trong lập trình Winform
Đăng bởi: Thảo Meo - Lượt xem: 3698 09:11:13, 26/04/2021C#   In bài viết

CÁC BÀI CÙNG CHỦ ĐỀ

Đọc tiếp
.