News Category

Apply Transparency to Image in PowerPoint in C#, VB.NET

2017-12-13 07:15:41 Written by  support iceblue
Rate this item
(0 votes)

When you position some text over an image shape, you may want to make the image lighter or transparent so it doesn’t interfere with text. This article will demonstrate how to apply transparency to the image inside of a shape using Spire.Presentation with C# and VB.NET.

Step 1: Create a Presentation instance.

Presentation presentation = new Presentation();

Step 2: Create an Image from the specified file.

string imagePath = "logo.png";
Image image = Image.FromFile(imagePath);

Step 3: Add a shape to the first slide.

float width = image.Width;
float height = image.Height;
RectangleF rect = new RectangleF(50, 50, width, height);
IAutoShape shape = presentation.Slides[0].Shapes.AppendShape(ShapeType.Rectangle, rect);
shape.Line.FillType = FillFormatType.None;

Step 4: Fill the shape with image.

shape.Fill.FillType = FillFormatType.Picture;
shape.Fill.PictureFill.Picture.Url = imagePath;
shape.Fill.PictureFill.FillType = PictureFillType.Stretch;

Step 5: Set transparency on the image.

shape.Fill.PictureFill.Picture.Transparency = 50;

Step 6: Save to file.

presentation.SaveToFile("output.pptx", FileFormat.Pptx2013);

Output:

Apply Transparency to Image in PowerPoint in C#, VB.NET

Full Code:

[C#]
using Spire.Presentation;
using Spire.Presentation.Drawing;
using System.Drawing;

namespace ApplyTransparency
{

    class Program
    {

        static void Main(string[] args)
        {
            {
                //create a PowerPoint document
                Presentation presentation = new Presentation();
                string imagePath = "logo.png";
                Image image = Image.FromFile(imagePath);
                float width = image.Width;
                float height = image.Height;
                RectangleF rect = new RectangleF(50, 50, width, height);
                //add a shape
                IAutoShape shape = presentation.Slides[0].Shapes.AppendShape(ShapeType.Rectangle, rect);
                shape.Line.FillType = FillFormatType.None;
                //fill shape with image
                shape.Fill.FillType = FillFormatType.Picture;
                shape.Fill.PictureFill.Picture.Url = imagePath;
                shape.Fill.PictureFill.FillType = PictureFillType.Stretch;
                //set transparency on image
                shape.Fill.PictureFill.Picture.Transparency = 50;
                //save file
                presentation.SaveToFile("output.pptx", FileFormat.Pptx2013);
            }
        }

    }
}
[VB.NET]
Imports Spire.Presentation
Imports Spire.Presentation.Drawing
Imports System.Drawing

Namespace ApplyTransparency

	Class Program

		Private Shared Sub Main(args As String())
			If True Then
				'create a PowerPoint document
				Dim presentation As New Presentation()
				Dim imagePath As String = "logo.png"
				Dim image__1 As Image = Image.FromFile(imagePath)
				Dim width As Single = image__1.Width
				Dim height As Single = image__1.Height
				Dim rect As New RectangleF(50, 50, width, height)
				'add a shape
				Dim shape As IAutoShape = presentation.Slides(0).Shapes.AppendShape(ShapeType.Rectangle, rect)
				shape.Line.FillType = FillFormatType.None
				'fill shape with image
				shape.Fill.FillType = FillFormatType.Picture
				shape.Fill.PictureFill.Picture.Url = imagePath
				shape.Fill.PictureFill.FillType = PictureFillType.Stretch
				'set transparency on image
				shape.Fill.PictureFill.Picture.Transparency = 50
				'save file
				presentation.SaveToFile("output.pptx", FileFormat.Pptx2013)
			End If
		End Sub

	End Class
End Namespace

Additional Info

  • tutorial_title:
Last modified on Friday, 24 September 2021 09:19