MS PowerPoint可以对填充在图形中的图片进行透明设置,使图形中的文字能显示于图片上方而不受图片内容的干扰。本文将展示如何使用Spire.Presetation,在PowerPoint中添加图片并设置透明度。
C#
//创建PowerPoint文档
Presentation presentation = new Presentation();
//加载图片到Image对象
string imagePath = "background.png";
Image image = Image.FromFile(imagePath);
//获取图片长宽
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;
//用图片填充图形
shape.Fill.FillType = FillFormatType.Picture;
shape.Fill.PictureFill.Picture.Url = imagePath;
shape.Fill.PictureFill.FillType = PictureFillType.Stretch;
//设置图片透明度(0-100)
shape.Fill.PictureFill.Picture.Transparency = 70;
//在图形上添加文字
shape.TextFrame.Text = "知识就是力量";
shape.TextFrame.TextRange.IsBold = TriState.True;
shape.TextFrame.TextRange.Fill.FillType = FillFormatType.Solid;
shape.TextFrame.TextRange.Fill.SolidColor.Color = Color.Black;
shape.TextFrame.TextRange.FontHeight = 30f;
//保存文档
presentation.SaveToFile("output.pptx", FileFormat.Pptx2013);
VB.NET
'创建PowerPoint文档
Dim presentation As New Presentation()
'加载图片到Image对象
Dim imagePath As String = "background.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)
Dim shape As IAutoShape = presentation.Slides(0).Shapes.AppendShape(ShapeType.Rectangle, rect)
shape.Line.FillType = FillFormatType.None
'用图片填充图形
shape.Fill.FillType = FillFormatType.Picture
shape.Fill.PictureFill.Picture.Url = imagePath
shape.Fill.PictureFill.FillType = PictureFillType.Stretch
'设置图片透明度(0-100)
shape.Fill.PictureFill.Picture.Transparency = 70
'在图形上添加文字
shape.TextFrame.Text = "知识就是力量"
shape.TextFrame.TextRange.IsBold = TriState.[True]
shape.TextFrame.TextRange.Fill.FillType = FillFormatType.Solid
shape.TextFrame.TextRange.Fill.SolidColor.Color = Color.Black
shape.TextFrame.TextRange.FontHeight = 30F
'保存文档
presentation.SaveToFile("output.pptx", FileFormat.Pptx2013)