จัดการพื้นหลังการนำเสนอใน .NET

บทนำ

สีทึบ, ไล่ระดับสี, และรูปภาพมักถูกใช้เป็นพื้นหลังของสไลด์ คุณสามารถกำหนดพื้นหลังสำหรับ สไลด์ปกติ (สไลด์เดียว) หรือ สไลด์หลัก (ใช้กับหลายสไลด์พร้อมกัน)

PowerPoint background

ตั้งค่าพื้นหลังสีทึบสำหรับสไลด์ปกติ

Aspose.Slides อนุญาตให้คุณตั้งค่าสีทึบเป็นพื้นหลังของสไลด์เฉพาะในงานนำเสนอ แม้ว่างานนำเสนอจะใช้สไลด์หลัก การเปลี่ยนแปลงจะส่งผลเฉพาะกับสไลด์ที่เลือกเท่านั้น

  1. สร้างอินสแตนซ์ของคลาส Presentation
  2. ตั้งค่า [BackgroundType] ของสไลด์เป็น OwnBackground
  3. ตั้งค่า [FillType] ของพื้นหลังสไลด์เป็น Solid
  4. ใช้คุณสมบัติ SolidFillColor บน FillFormat เพื่อระบุสีพื้นหลังทึบ
  5. บันทึกงานนำเสนอที่แก้ไขแล้ว

ตัวอย่าง C# ด้านล่างแสดงวิธีตั้งค่าสีทึบสีน้ำเงินเป็นพื้นหลังของสไลด์ปกติ:

// สร้างอินสแตนซ์ของคลาส Presentation.
using (Presentation presentation = new Presentation())
{
    ISlide slide = presentation.Slides[0];

    // ตั้งค่าสีพื้นหลังของสไลด์เป็นสีน้ำเงิน.
    slide.Background.Type = BackgroundType.OwnBackground;
    slide.Background.FillFormat.FillType = FillType.Solid;
    slide.Background.FillFormat.SolidFillColor.Color = Color.Blue;

    // บันทึกงานนำเสนอลงดิสก์.
    presentation.Save("SolidColorBackground.pptx", SaveFormat.Pptx);
}

ตั้งค่าพื้นหลังสีทึบสำหรับสไลด์หลัก

Aspose.Slides อนุญาตให้คุณตั้งค่าสีทึบเป็นพื้นหลังของสไลด์หลักในงานนำเสนอ สไลด์หลักทำหน้าที่เป็นแม่แบบที่ควบคุมการจัดรูปแบบของสไลด์ทั้งหมด ดังนั้นเมื่อคุณเลือกสีทึบสำหรับพื้นหลังของสไลด์หลัก สีนี้จะใช้กับทุกสไลด์

  1. สร้างอินสแตนซ์ของคลาส Presentation
  2. ตั้งค่า [BackgroundType] ของสไลด์หลัก (ผ่าน masters) เป็น OwnBackground
  3. ตั้งค่า [FillType] ของพื้นหลังสไลด์หลักเป็น Solid
  4. ใช้ [SolidFillColor] เพื่อระบุสีพื้นหลังทึบ
  5. บันทึกงานนำเสนอที่แก้ไขแล้ว

ตัวอย่าง C# ด้านล่างแสดงวิธีตั้งค่าสีทึบ (สีเขียวป่า) เป็นพื้นหลังของสไลด์หลัก:

// สร้างอินสแตนซ์ของคลาส Presentation.
using (Presentation presentation = new Presentation())
{
    IMasterSlide masterSlide = presentation.Masters[0];

    // ตั้งค่าสีพื้นหลังของสไลด์ Master เป็นสีเขียวป่า.
    masterSlide.Background.Type = BackgroundType.OwnBackground;
    masterSlide.Background.FillFormat.FillType = FillType.Solid;
    masterSlide.Background.FillFormat.SolidFillColor.Color = Color.ForestGreen;

    // บันทึกงานนำเสนอลงดิสก์.
    presentation.Save("MasterSlideBackground.pptx", SaveFormat.Pptx);
}

ตั้งค่าพื้นหลังไล่ระดับสีสำหรับสไลด์

ไล่ระดับสีเป็นเอฟเฟกต์กราฟิกที่สร้างจากการเปลี่ยนแปลงสีอย่างค่อยเป็นค่อยไป เมื่อใช้เป็นพื้นหลังของสไลด์ ไล่ระดับสีสามารถทำให้งานนำเสนอดูศิลป์และเป็นมืออาชีพมากขึ้น Aspose.Slides อนุญาตให้คุณตั้งค่าสีไล่ระดับเป็นพื้นหลังของสไลด์

  1. สร้างอินสแตนซ์ของคลาส Presentation
  2. ตั้งค่า [BackgroundType] ของสไลด์เป็น OwnBackground
  3. ตั้งค่า [FillType] ของพื้นหลังสไลด์เป็น Gradient
  4. ใช้คุณสมบัติ GradientFormat บน FillFormat เพื่อกำหนดการตั้งค่าไล่ระดับที่ต้องการ
  5. บันทึกงานนำเสนอที่แก้ไขแล้ว

ตัวอย่าง C# ด้านล่างแสดงวิธีตั้งค่าสีไล่ระดับเป็นพื้นหลังของสไลด์:

// สร้างอินสแตนซ์ของคลาส Presentation.
using (Presentation presentation = new Presentation())
{
    ISlide slide = presentation.Slides[0];

    // ใช้เอฟเฟกต์ไล่ระดับสีกับพื้นหลัง.
    slide.Background.Type = BackgroundType.OwnBackground;
    slide.Background.FillFormat.FillType = FillType.Gradient;
    slide.Background.FillFormat.GradientFormat.TileFlip = TileFlip.FlipBoth;

    // บันทึกงานนำเสนอลงดิสก์.
    presentation.Save("GradientBackground.pptx", SaveFormat.Pptx);
}

ตั้งค่าภาพเป็นพื้นหลังของสไลด์

นอกจากการเติมสีทึบและไล่ระดับแล้ว Aspose.Slides ยังอนุญาตให้คุณใช้รูปภาพเป็นพื้นหลังของสไลด์ได้

  1. สร้างอินสแตนซ์ของคลาส Presentation
  2. ตั้งค่า [BackgroundType] ของสไลด์เป็น OwnBackground
  3. ตั้งค่า [FillType] ของพื้นหลังสไลด์เป็น Picture
  4. โหลดภาพที่ต้องการใช้เป็นพื้นหลังของสไลด์
  5. เพิ่มภาพลงในคอลเลกชันภาพของงานนำเสนอ
  6. ใช้คุณสมบัติ PictureFillFormat บน FillFormat เพื่อกำหนดภาพเป็นพื้นหลัง
  7. บันทึกงานนำเสนอที่แก้ไขแล้ว

ตัวอย่าง C# ด้านล่างแสดงวิธีตั้งค่าภาพเป็นพื้นหลังของสไลด์:

// Create an instance of the Presentation class.
using (Presentation presentation = new Presentation())
{
    ISlide slide = presentation.Slides[0];

    // Set background image properties.
    slide.Background.Type = BackgroundType.OwnBackground;
    slide.Background.FillFormat.FillType = FillType.Picture;
    slide.Background.FillFormat.PictureFillFormat.PictureFillMode = PictureFillMode.Stretch;

    // Load the image.
    IImage image = Images.FromFile("Tulips.jpg");
    // Add the image to the presentation's image collection.
    IPPImage ppImage = presentation.Images.AddImage(image);
    image.Dispose();

    slide.Background.FillFormat.PictureFillFormat.Picture.Image = ppImage;

    // Save the presentation to disk.
    presentation.Save("ImageAsBackground.pptx", SaveFormat.Pptx);
}

ตัวอย่างโค้ดต่อไปนี้แสดงวิธีตั้งค่าชนิดการเติมพื้นหลังเป็นภาพที่ทำเป็นลายกระเบื้องและแก้ไขคุณสมบัติลายกระเบื้อง:

using (Presentation presentation = new Presentation())
{
    ISlide firstSlide = presentation.Slides[0];

    IBackground background = firstSlide.Background;

    background.Type = BackgroundType.OwnBackground;
    background.FillFormat.FillType = FillType.Picture;

    IPPImage ppImage;
    using (IImage newImage = Aspose.Slides.Images.FromFile("image.png"))
        ppImage = presentation.Images.AddImage(newImage);

    // ตั้งค่าภาพที่ใช้สำหรับเติมพื้นหลัง.
    IPictureFillFormat backPictureFillFormat = background.FillFormat.PictureFillFormat;
    backPictureFillFormat.Picture.Image = ppImage;

    // ตั้งค่าโหมดเติมภาพเป็นแบบกระเบื้องและปรับคุณสมบัติของกระเบื้อง.
    backPictureFillFormat.PictureFillMode = PictureFillMode.Tile;
    backPictureFillFormat.TileOffsetX = 15f;
    backPictureFillFormat.TileOffsetY = 15f;
    backPictureFillFormat.TileScaleX = 46f;
    backPictureFillFormat.TileScaleY = 87f;
    backPictureFillFormat.TileAlignment = RectangleAlignment.Center;
    backPictureFillFormat.TileFlip = TileFlip.FlipY;

    presentation.Save("TileBackground.pptx", SaveFormat.Pptx);
}

เปลี่ยนความโปร่งใสของภาพพื้นหลัง

คุณอาจต้องการปรับความโปร่งใสของภาพพื้นหลังของสไลด์เพื่อให้เนื้อหาในสไลด์โดดเด่นขึ้น โค้ด C# ด้านล่างแสดงวิธีเปลี่ยนความโปร่งใสของภาพพื้นหลังสไลด์:

var transparencyValue = 30; // ตัวอย่างเช่น.

// ดึงคอลเลกชันของการแปลงรูปภาพ.
var imageTransform = slide.Background.FillFormat.PictureFillFormat.Picture.ImageTransform;

// ค้นหาเอฟเฟกต์ความโปร่งใสแบบเปอร์เซ็นต์คงที่ที่มีอยู่.
var transparencyOperation = null as IAlphaModulateFixed;
foreach (var operation in imageTransform)
{
    if (operation is IAlphaModulateFixed alphaModulateFixed)
    {
        transparencyOperation = alphaModulateFixed;
        break;
    }
}

// ตั้งค่าความโปร่งใสใหม่.
if (transparencyOperation == null)
{
    imageTransform.AddAlphaModulateFixedEffect(100 - transparencyValue);
}
else
{
    transparencyOperation.Amount = (100 - transparencyValue);
}

ดึงค่าพื้นหลังของสไลด์

Aspose.Slides มีอินเทอร์เฟซ IBackgroundEffectiveData สำหรับดึงค่าพื้นหลังที่มีผลของสไลด์ อินเทอร์เฟซนี้เปิดเผย FillFormat และ EffectFormat ที่มีผล

โดยใช้คุณสมบัติ background ของคลาส BaseSlide คุณสามารถรับพื้นหลังที่มีผลของสไลด์ได้

ตัวอย่าง C# ด้านล่างแสดงวิธีดึงค่าพื้นหลังที่มีผลของสไลด์:

// สร้างอินสแตนซ์ของคลาส Presentation.
using (Presentation presentation = new Presentation("Sample.pptx"))
{
    ISlide slide = presentation.Slides[0];  

    // ดึงพื้นหลังที่มีผลโดยคำนึงถึง master, layout และ theme.
    IBackgroundEffectiveData effBackground = slide.Background.GetEffective();

    if (effBackground.FillFormat.FillType == FillType.Solid)
        Console.WriteLine("Fill color: " + effBackground.FillFormat.SolidFillColor);
    else
        Console.WriteLine("Fill type: " + effBackground.FillFormat.FillType);
}

คำถามที่พบบ่อย

ฉันสามารถรีเซ็ตพื้นหลังที่กำหนดเองและคืนค่าเป็นพื้นหลังของธีม/เลย์เอาต์ได้หรือไม่?

ได้ค่ะ เพียงลบการเติมที่กำหนดเองของสไลด์ แล้วพื้นหลังจะสืบทอดจากสไลด์ layout/master ที่สอดคล้อง (คือ theme background)

ถ้าฉันเปลี่ยนธีมของงานนำเสนอภายหลัง พื้นหลังจะเกิดอะไรขึ้น?

หากสไลด์มีการเติมของตนเอง มันจะคงอยู่โดยไม่เปลี่ยนแปลง หากพื้นหลังสืบทอดจาก layout/master จะอัปเดตให้ตรงกับ theme ใหม่