如何在 C# 中給文本添加光暈效果

How to Add Glow Effect to Text in C

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

螢光效果可在文字字元周圍營造出發光的光環。 這種視覺效果會讓文字看起來發出柔和輪廓的光線。在 C# 應用程式中,尤其是 Word 文件,發光效果有助於強調標題、標題和重要內容。 該效果廣泛應用於簡報、行銷資料和專業文件等文字需要視覺強調的地方。

快速入門:快速為文字元素套用發光效果

建立一個 Glow 物件,設定其 GlowRadiusGlowColor,將其嵌入 TextEffect 中,並將其指派給您的文字樣式。 一行在您的 Word 文件中建立發光的文字。

  1. 使用NuGet套件管理器安裝https://www.nuget.org/packages/IronWord

    PM > Install-Package IronWord
  2. 複製並運行這段程式碼。

    using IronWord;
    using IronWord.Models;
    
    WordDocument doc = new WordDocument();
    TextStyle textStyle = new TextStyle();
    textStyle.TextEffect = new TextEffect() { GlowEffect = new Glow() { GlowColor = IronWord.Models.Color.Aqua, GlowRadius = 10 } };
    Paragraph paragraph = new Paragraph();
    Run textRun = new Run(new TextContent("Glowing Text"));
    textRun.Style = textStyle;
    paragraph.AddChild(textRun);
    doc.AddParagraph(paragraph);
    doc.SaveAs("glow.docx");
  3. 部署到您的生產環境進行測試

    今天就在您的專案中開始使用免費試用IronWord

    arrow pointer

如何在 C# 中為文字加入光暈效果?

若要套用發光效果,請建立一個 TextStyle,並將其 TextEffect 屬性填入 GlowEffect。 接著建立一個 Paragraph,其後接一個包含 TextContentRun。 將 TextStyle 指派給 Run(而非 TextContent),然後使用 AddChildRun 加入 Paragraph。 此文件遵循以下層級結構:文件 → 段落 → 執行 → 文字內容。 此方法可完全控制螢光外觀和強度。

為什麼要先建立 Glow 物件?

先建立 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);

        // 儲存文件
        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);

        // 儲存文件
        doc.SaveAs("glowing-text-example.docx");
    }
}
$vbLabelText   $csharpLabel
Microsoft Word 顯示已套用青色螢光效果的

什麼是 Glow 效果屬性?

了解螢光效果的屬性,對於建立專業的外觀效果,在不壓迫內容的情況下增強文件的效果是非常重要的。 適當的授權可確保這些功能在生產環境中不受任何限制地運作。

哪些屬性控制 Glow 外觀?

  • GlowRadius:以點(1/72 英吋)為單位設定光暈效果的半徑。 分值通常在 5 到 30 分之間。 較大的數值會產生更散漫、更分散的光線。 5-10 點的半徑可創造微妙的亮點; 20-30 點會產生戲劇性的光暈。

  • GlowColor:設定發光效果的顏色。 支援 System.Drawing.Color 數值,包含用於控制透明度的 ARGB 值。 鮮豔的色彩(青色、黃色、洋紅色)可創造出活潑的效果; 深色提供微妙的強調。

如何使用 Alpha 透明度設定顏色值?

Alpha 透明度可創造逼真的發光效果,與背景自然融合。 Alpha 值範圍從 0 (透明) 到 255 (不透明)。 以下是一個顯示不同 alpha 值的範例:

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");
    }
}
$vbLabelText   $csharpLabel

Alpha 值指引:

  • 50-100:非常微妙、幾乎看不見的水印式效果
  • 100-150:柔和的光澤,商業文件的專業外觀
  • 150-200:中等強度,平衡標頭和標題
  • 200-255:強烈的光彩,宣傳材料的高影響力

有哪些炫光效果範例?

Glow 效果接受 ARGB 顏色值。 alpha 值控制不透明度。 這些範例展示了在各種文件情境中的實際光彩應用。 在生產實作之前,配置適當的 授權金鑰

何時應該使用不同的半徑值?

不同的半徑值可達到不同的目的。 小半徑(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");
    }
}
$vbLabelText   $csharpLabel

光暈效果的常見顏色組合有哪些?

有效的顏色組合取決於文件目的和品牌。 專業文件使用微妙的藍色、灰色或 Alpha 值較低的品牌顏色。 行銷材料採用鮮豔的互補色或高對比的組合。 使用多個 Iron Software 產品時,請檢查 產品更新,以確保相容性。

常見的顏色組合:

1.Professional Blue:帶有淺藍色光彩的深藍色文字 (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 物件屬性提供完全自訂的 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 熱衷於創建直觀且美觀的用戶界面,喜歡使用現代框架並打造結構良好、視覺吸引人的手冊。

除了開發之外,Curtis 對物聯網 (IoT) 有著濃厚的興趣,探索將硬體和軟體結合的創新方式。在閒暇時間,他喜愛遊戲並構建 Discord 機器人,結合科技與創意的樂趣。

準備好開始了嗎?
Nuget 下載 36,374 | 版本: 2026.3 剛剛發布
Still Scrolling Icon

還在捲動嗎?

想要快速證明? PM > Install-Package IronWord
執行範例 觀看您的資料變成 Word doc。