如何在 C# 中为文本添加发光效果

如何在 C# 中为文本添加发光效果

This article was translated from English: Does it need improvement?
Translated
View the article in English

<! -- 待办事项:在此处添加图片 --> <! --介绍实现的示意图 --> <!--说明:说明代码概念的图表或截图 -->

发光效果可在文本字符周围营造出发光的光环。 这种视觉效果会使文本看起来发出柔和轮廓的光。在 C# 应用程序,尤其是 Word 文档中,发光效果有助于强调标题、标题和重要内容。 该效果广泛应用于演示文稿、营销材料和专业文档中需要视觉强调的文本。

快速入门:快速为文本元素应用发光效果

创建一个Glow对象,设置其GlowRadiusGlowColor,将其嵌入到TextEffect中,并将其分配给文本样式。 在 Word 文档中创建一行发光文本。

Nuget Icon立即开始使用 NuGet 创建 PDF 文件:

  1. 使用 NuGet 包管理器安装 IronWord

    PM > Install-Package IronWord

  2. 复制并运行这段代码。

    someTextElement.Style.TextEffect = new IronWord.Models.TextEffect { GlowEffect = new IronWord.Models.Glow { GlowRadius = 8, GlowColor = System.Drawing.Color.FromArgb(180, 0, 128, 255) } };
  3. 部署到您的生产环境中进行测试

    立即开始在您的项目中使用 IronWord,免费试用!
    arrow pointer

如何在 C# 中为文本添加光晕效果?

要为文本添加发光效果,请首先创建并配置 Glow 对象。然后创建一个包含该 Glow 对象的 TextEffect 对象。 最后,将 TextEffect 分配给文本的 TextEffect 属性。 这种方法可以完全控制发光的外观和强度。

为什么首先创建 Glow 对象很重要?

首先创建一个 Glow 对象,这样就可以在应用之前配置所有辉光属性。 这要遵循关注点分离原则,并提高代码的可维护性。 通过独立定义发光属性,可以在多个文本元素中重复使用,并根据应用需求进行动态修改。 这种模式与 Iron Software 其他产品处理类似效果的方式相匹配,保持了文档处理工作流程的一致性。

using IronWord;
using IronWord.Models;
using System.Drawing;

public class TextGlowEffectExample
{
    public void ApplyGlowEffect()
    {
        // Create a new Word document
        WordDocument doc = new WordDocument();

        // Add a paragraph with text
        Paragraph paragraph = new Paragraph();
        Text textRun = new Text("This text has a bright glow!");
        paragraph.AddTextRun(textRun);

        // Initialize a new Glow object
        Glow glow = new Glow();

        // Set the properties for the glow effect
        glow.GlowRadius = 15; // Radius of the glow effect in points
        glow.GlowColor = Color.FromArgb(200, 0, 255, 255); // Semi-transparent cyan

        // Create a TextEffect object and assign the glow effect to it
        TextEffect textEffect = new TextEffect();
        textEffect.GlowEffect = glow;

        // Apply the TextEffect to the text
        textRun.Style = new TextStyle();
        textRun.Style.TextEffect = textEffect;

        // Add the paragraph to the document
        doc.AddParagraph(paragraph);

        // Save the document
        doc.SaveAs("glowing-text-example.docx");
    }
}
using IronWord;
using IronWord.Models;
using System.Drawing;

public class TextGlowEffectExample
{
    public void ApplyGlowEffect()
    {
        // Create a new Word document
        WordDocument doc = new WordDocument();

        // Add a paragraph with text
        Paragraph paragraph = new Paragraph();
        Text textRun = new Text("This text has a bright glow!");
        paragraph.AddTextRun(textRun);

        // Initialize a new Glow object
        Glow glow = new Glow();

        // Set the properties for the glow effect
        glow.GlowRadius = 15; // Radius of the glow effect in points
        glow.GlowColor = Color.FromArgb(200, 0, 255, 255); // Semi-transparent cyan

        // Create a TextEffect object and assign the glow effect to it
        TextEffect textEffect = new TextEffect();
        textEffect.GlowEffect = glow;

        // Apply the TextEffect to the text
        textRun.Style = new TextStyle();
        textRun.Style.TextEffect = textEffect;

        // Add the paragraph to the document
        doc.AddParagraph(paragraph);

        // Save the document
        doc.SaveAs("glowing-text-example.docx");
    }
}
Imports IronWord
Imports IronWord.Models
Imports System.Drawing

Public Class TextGlowEffectExample
    Public Sub ApplyGlowEffect()
        ' Create a new Word document
        Dim doc As New WordDocument()

        ' Add a paragraph with text
        Dim paragraph As New Paragraph()
        Dim textRun As New Text("This text has a bright glow!")
        paragraph.AddTextRun(textRun)

        ' Initialize a new Glow object
        Dim glow As New Glow()

        ' Set the properties for the glow effect
        glow.GlowRadius = 15 ' Radius of the glow effect in points
        glow.GlowColor = Color.FromArgb(200, 0, 255, 255) ' Semi-transparent cyan

        ' Create a TextEffect object and assign the glow effect to it
        Dim textEffect As New TextEffect()
        textEffect.GlowEffect = glow

        ' Apply the TextEffect to the text
        textRun.Style = New TextStyle()
        textRun.Style.TextEffect = textEffect

        ' Add the paragraph to the document
        doc.AddParagraph(paragraph)

        ' Save the document
        doc.SaveAs("glowing-text-example.docx")
    End Sub
End Class
$vbLabelText   $csharpLabel
Microsoft Word 显示应用了青色辉光效果的'Hello World'文本,演示文本辉光格式化结果

什么是辉光效果属性?

要创建既能增强文档效果又不会淹没内容的专业外观效果,了解辉光效果属性至关重要。 适当的许可可确保这些功能在生产环境中不受任何限制地运行。

哪些属性控制辉光外观?

  • GlowRadius:设置辉光效果的半径,单位为点(1/72 英寸)。 分值通常在 5 到 30 分之间。 较大的数值会产生更多分散的光晕。 以 5-10 点为半径创建微妙的亮点; 20-30 分会产生明显的光晕。

  • GlowColor:设置辉光效果颜色。 接受 System.Drawing.Color 值,包括用于透明度控制的 ARGB。 鲜艳的色彩(青色、黄色、洋红色)营造出生动的效果; 深色可以起到微妙的强调作用。

如何使用 Alpha 透明度设置颜色值?

Alpha 透明度可产生逼真的发光效果,与背景自然融合。 Alpha 值范围从 0(透明)到 255(不透明)。 下面是一个显示不同字母值的示例:

using IronWord;
using IronWord.Models;
using System.Drawing;

public class AlphaTransparencyExample
{
    public void DemonstrateAlphaValues()
    {
        WordDocument doc = new WordDocument();

        // Create multiple text samples with different alpha values
        int[] alphaValues = { 50, 100, 150, 200, 255 };

        foreach (int alpha in alphaValues)
        {
            Paragraph para = new Paragraph();
            Text text = new Text($"Alpha: {alpha} - Glow Effect Sample");

            // Create glow with specific alpha transparency
            Glow glow = new Glow
            {
                GlowRadius = 12,
                GlowColor = Color.FromArgb(alpha, 255, 215, 0) // Gold with varying transparency
            };

            // Apply the glow effect
            TextEffect effect = new TextEffect { GlowEffect = glow };
            text.Style = new TextStyle 
            { 
                TextEffect = effect,
                FontSize = 24,
                FontFamily = "Arial"
            };

            para.AddTextRun(text);
            doc.AddParagraph(para);
        }

        doc.SaveAs("alpha-transparency-demo.docx");
    }
}
using IronWord;
using IronWord.Models;
using System.Drawing;

public class AlphaTransparencyExample
{
    public void DemonstrateAlphaValues()
    {
        WordDocument doc = new WordDocument();

        // Create multiple text samples with different alpha values
        int[] alphaValues = { 50, 100, 150, 200, 255 };

        foreach (int alpha in alphaValues)
        {
            Paragraph para = new Paragraph();
            Text text = new Text($"Alpha: {alpha} - Glow Effect Sample");

            // Create glow with specific alpha transparency
            Glow glow = new Glow
            {
                GlowRadius = 12,
                GlowColor = Color.FromArgb(alpha, 255, 215, 0) // Gold with varying transparency
            };

            // Apply the glow effect
            TextEffect effect = new TextEffect { GlowEffect = glow };
            text.Style = new TextStyle 
            { 
                TextEffect = effect,
                FontSize = 24,
                FontFamily = "Arial"
            };

            para.AddTextRun(text);
            doc.AddParagraph(para);
        }

        doc.SaveAs("alpha-transparency-demo.docx");
    }
}
Imports IronWord
Imports IronWord.Models
Imports System.Drawing

Public Class AlphaTransparencyExample
    Public Sub DemonstrateAlphaValues()
        Dim doc As New WordDocument()

        ' Create multiple text samples with different alpha values
        Dim alphaValues As Integer() = {50, 100, 150, 200, 255}

        For Each alpha As Integer In alphaValues
            Dim para As New Paragraph()
            Dim text As New Text($"Alpha: {alpha} - Glow Effect Sample")

            ' Create glow with specific alpha transparency
            Dim glow As New Glow With {
                .GlowRadius = 12,
                .GlowColor = Color.FromArgb(alpha, 255, 215, 0) ' Gold with varying transparency
            }

            ' Apply the glow effect
            Dim effect As New TextEffect With {.GlowEffect = glow}
            text.Style = New TextStyle With {
                .TextEffect = effect,
                .FontSize = 24,
                .FontFamily = "Arial"
            }

            para.AddTextRun(text)
            doc.AddParagraph(para)
        Next

        doc.SaveAs("alpha-transparency-demo.docx")
    End Sub
End Class
$vbLabelText   $csharpLabel

Alpha 值指南:

  • 50-100:非常微妙、几乎不可见的水印式效果
  • 100-150:柔和的光彩,商务文件的专业外观
  • 150-200:中等强度,兼顾页眉和标题
  • 200-255:强光、高影响力的宣传材料

有哪些辉光效果示例?

辉光效果接受 ARGB 颜色值。 阿尔法值控制不透明度。 这些示例展示了在各种文档上下文中的实际发光应用。 在生产实施前配置正确的 许可证密钥

何时应使用不同的半径值?

不同的半径值服务于不同的目的。 小半径(5-10 点)创建聚焦光晕,以便对术语或链接进行微妙的强调。 中等弧度(15-20 点)适用于章节标题,提供清晰的层次结构。 大半径(25 点以上)适合需要最大影响力的封面页或宣传页。

下面是一个显示半径应用的实施方案:

public class RadiusExamples
{
    public void CreateRadiusComparison()
    {
        WordDocument doc = new WordDocument();

        // Example 1: Subtle emphasis for inline text
        Paragraph p1 = new Paragraph();
        Text subtleText = new Text("Important: This deadline cannot be extended.");
        subtleText.Style = new TextStyle
        {
            TextEffect = new TextEffect
            {
                GlowEffect = new Glow
                {
                    GlowRadius = 6,
                    GlowColor = Color.FromArgb(180, 255, 0, 0) // Soft red glow
                }
            }
        };
        p1.AddTextRun(subtleText);

        // Example 2: Section header with medium glow
        Paragraph p2 = new Paragraph();
        Text headerText = new Text("Chapter 1: Getting Started");
        headerText.Style = new TextStyle
        {
            FontSize = 28,
            FontFamily = "Calibri",
            TextEffect = new TextEffect
            {
                GlowEffect = new Glow
                {
                    GlowRadius = 18,
                    GlowColor = Color.FromArgb(150, 0, 120, 215) // Corporate blue
                }
            }
        };
        p2.AddTextRun(headerText);

        // Example 3: Promotional text with large glow
        Paragraph p3 = new Paragraph();
        Text promoText = new Text("SPECIAL OFFER - LIMITED TIME!");
        promoText.Style = new TextStyle
        {
            FontSize = 36,
            Bold = true,
            TextEffect = new TextEffect
            {
                GlowEffect = new Glow
                {
                    GlowRadius = 30,
                    GlowColor = Color.FromArgb(220, 255, 255, 0) // Bright yellow
                }
            }
        };
        p3.AddTextRun(promoText);

        doc.AddParagraph(p1);
        doc.AddParagraph(p2);
        doc.AddParagraph(p3);
        doc.SaveAs("radius-examples.docx");
    }
}
public class RadiusExamples
{
    public void CreateRadiusComparison()
    {
        WordDocument doc = new WordDocument();

        // Example 1: Subtle emphasis for inline text
        Paragraph p1 = new Paragraph();
        Text subtleText = new Text("Important: This deadline cannot be extended.");
        subtleText.Style = new TextStyle
        {
            TextEffect = new TextEffect
            {
                GlowEffect = new Glow
                {
                    GlowRadius = 6,
                    GlowColor = Color.FromArgb(180, 255, 0, 0) // Soft red glow
                }
            }
        };
        p1.AddTextRun(subtleText);

        // Example 2: Section header with medium glow
        Paragraph p2 = new Paragraph();
        Text headerText = new Text("Chapter 1: Getting Started");
        headerText.Style = new TextStyle
        {
            FontSize = 28,
            FontFamily = "Calibri",
            TextEffect = new TextEffect
            {
                GlowEffect = new Glow
                {
                    GlowRadius = 18,
                    GlowColor = Color.FromArgb(150, 0, 120, 215) // Corporate blue
                }
            }
        };
        p2.AddTextRun(headerText);

        // Example 3: Promotional text with large glow
        Paragraph p3 = new Paragraph();
        Text promoText = new Text("SPECIAL OFFER - LIMITED TIME!");
        promoText.Style = new TextStyle
        {
            FontSize = 36,
            Bold = true,
            TextEffect = new TextEffect
            {
                GlowEffect = new Glow
                {
                    GlowRadius = 30,
                    GlowColor = Color.FromArgb(220, 255, 255, 0) // Bright yellow
                }
            }
        };
        p3.AddTextRun(promoText);

        doc.AddParagraph(p1);
        doc.AddParagraph(p2);
        doc.AddParagraph(p3);
        doc.SaveAs("radius-examples.docx");
    }
}
Public Class RadiusExamples
    Public Sub CreateRadiusComparison()
        Dim doc As New WordDocument()

        ' Example 1: Subtle emphasis for inline text
        Dim p1 As New Paragraph()
        Dim subtleText As New Text("Important: This deadline cannot be extended.")
        subtleText.Style = New TextStyle With {
            .TextEffect = New TextEffect With {
                .GlowEffect = New Glow With {
                    .GlowRadius = 6,
                    .GlowColor = Color.FromArgb(180, 255, 0, 0) ' Soft red glow
                }
            }
        }
        p1.AddTextRun(subtleText)

        ' Example 2: Section header with medium glow
        Dim p2 As New Paragraph()
        Dim headerText As New Text("Chapter 1: Getting Started")
        headerText.Style = New TextStyle With {
            .FontSize = 28,
            .FontFamily = "Calibri",
            .TextEffect = New TextEffect With {
                .GlowEffect = New Glow With {
                    .GlowRadius = 18,
                    .GlowColor = Color.FromArgb(150, 0, 120, 215) ' Corporate blue
                }
            }
        }
        p2.AddTextRun(headerText)

        ' Example 3: Promotional text with large glow
        Dim p3 As New Paragraph()
        Dim promoText As New Text("SPECIAL OFFER - LIMITED TIME!")
        promoText.Style = New TextStyle With {
            .FontSize = 36,
            .Bold = True,
            .TextEffect = New TextEffect With {
                .GlowEffect = New Glow With {
                    .GlowRadius = 30,
                    .GlowColor = Color.FromArgb(220, 255, 255, 0) ' Bright yellow
                }
            }
        }
        p3.AddTextRun(promoText)

        doc.AddParagraph(p1)
        doc.AddParagraph(p2)
        doc.AddParagraph(p3)
        doc.SaveAs("radius-examples.docx")
    End Sub
End Class
$vbLabelText   $csharpLabel

光晕效果的常见颜色组合有哪些?

有效的颜色组合取决于文件的目的和品牌。 专业文档使用含蓄的蓝色、灰色或阿尔法值较低的品牌色。 营销材料采用鲜艳的互补色或高对比度组合。 使用多个 Iron Software 产品时,请检查 产品更新,以确保兼容性。

常用颜色组合:

1.专业蓝:带有淡蓝色光晕的藏青色文本(RGB:100, 150, 255) 2.暖色强调:带有金色光芒的深棕色文本(RGB:255, 200, 50) 3.高对比度:带有白色/银色光晕的黑色文本(RGB:220、220、220) 4.品牌颜色:带有互补光彩的公司颜色文本 5.季节主题:绿色/红色代表节日,橙色/黑色代表万圣节

显示辉光效果的四个文本示例:水蓝色 10pt、天蓝色 20pt、金色 30pt 和自定义绿色 40pt 半径

发光效果应增强可读性,而不是妨碍可读性。 测试不同背景下的组合,并遵循可访问性指南。 对于需要扩展支持的企业应用程序,请探索许可扩展以获得持续更新和功能。 在扩展应用程序时,升级选项可为不断增长的团队和不断扩大的需求提供灵活性。

常见问题解答

如何使用 C# 为 Word 文档中的文本添加发光效果?

要使用 IronWord 添加发光效果,请创建一个具有所需半径和颜色设置的 Glow 对象,然后将其嵌入 TextEffect 对象中,并将其赋值给文本元素的 Style.TextEffect 属性。这可以在一行中完成: SomeTextElement.Style.TextEffect = new IronWord.Models.TextEffect { GlowEffect = new IronWord.Models.Glow { GlowRadius = 8, GlowColor = System.Drawing.Color.FromArgb(180, 0, 128, 255) }; }};

创建发光文本至少需要多少代码?

IronWord 允许您只用一行代码就能创建发光文本,方法是将文本元素的 Style.TextEffect 属性设置为包含已配置 Glow 对象的新 TextEffect。这包括在一条语句中设置发光半径和颜色。

我可以自定义发光效果的外观吗?

是的,IronWord 通过 Glow 对象属性提供了完全自定义的辉光效果。您可以调整 GlowRadius(以点为单位)来控制辉光的大小,还可以使用 System.Drawing.Color 设置 GlowColor,并使用 ARGB 值来实现精确的颜色和透明度控制。

为什么要创建单独的 Glow 对象而不是内联配置?

在 IronWord 中创建独立的 Glow 对象遵循了关注点分离原则,提高了代码的可维护性。这种方法允许您在多个文本元素中重复使用相同的 Glow 配置,并根据应用需求动态修改属性,与 Iron Software 其他产品中使用的一致模式相匹配。

在文本中应用发光效果的主要步骤是什么?

IronWord 的工作流程包括 5 个步骤:1) 下载 IronWord C# 库;2) 将文本效果应用于新文本或现有文本;3) 用半径和颜色设置配置 Glow 对象;4) 将 Glow 赋值给 TextEffect 对象的 GlowEffect 属性;5) 将编辑后的 Word 文档导出为新文件。

哪些类型的文档可以从文字发光效果中获益?

IronWord 的发光效果功能特别适用于制作演示文稿、营销材料和专业文档等需要在视觉上强调文本的地方。发光光环效果有助于突出 Word 文档中的页眉、标题和重要内容。

Curtis Chau
技术作家

Curtis Chau 拥有卡尔顿大学的计算机科学学士学位,专注于前端开发,精通 Node.js、TypeScript、JavaScript 和 React。他热衷于打造直观且美观的用户界面,喜欢使用现代框架并创建结构良好、视觉吸引力强的手册。

除了开发之外,Curtis 对物联网 (IoT) 有浓厚的兴趣,探索将硬件和软件集成的新方法。在空闲时间,他喜欢玩游戏和构建 Discord 机器人,将他对技术的热爱与创造力相结合。

准备开始了吗?
Nuget 下载 32,629 | 版本: 2026.2 刚刚发布