In order to change the look of a shape, users can add a solid, gradient, pattern or picture fill to it. Spire.Presentation supports all fill types listed. This article will introduce how to add a picture fill to a shape in C# and VB.NET.
Code Snippets:
Step 1: Initialize an instance of Presentation class.
Presentation ppt = new Presentation();
Step 2: Add a shape to the first slide.
IAutoShape shape = (IAutoShape)ppt.Slides[0].Shapes.AppendShape(ShapeType.DoubleWave, new RectangleF(100, 100, 400, 200));
Step 3: Set the FillType as picture and fill the shape with an image.
string picUrl = @"C:\Users\Administrator\Desktop\image.jpg"; shape.Fill.FillType = FillFormatType.Picture; shape.Fill.PictureFill.Picture.Url = picUrl;
Step 4: Set the picture fill mode to stretch.
shape.Fill.PictureFill.FillType = PictureFillType.Stretch;
Step 5: Save to file.
ppt.SaveToFile("shape.pptx", FileFormat.Pptx2010);
Output:
Full Code:
[C#]
using Spire.Presentation; using Spire.Presentation.Drawing; using System.Drawing; namespace FillShape { class Program { static void Main(string[] args) { Presentation ppt = new Presentation(); IAutoShape shape = (IAutoShape)ppt.Slides[0].Shapes.AppendShape(ShapeType.DoubleWave, new RectangleF(100, 100, 400, 200)); string picUrl = @"C:\Users\Administrator\Desktop\image.jpg"; shape.Fill.FillType = FillFormatType.Picture; shape.Fill.PictureFill.Picture.Url = picUrl; shape.Fill.PictureFill.FillType = PictureFillType.Stretch; shape.ShapeStyle.LineColor.Color = Color.Transparent; ppt.SaveToFile("shape.pptx", FileFormat.Pptx2010); System.Diagnostics.Process.Start("shape.pptx"); } } }
[VB.NET]
Imports Spire.Presentation Imports Spire.Presentation.Drawing Imports System.Drawing Namespace FillShape Class Program Private Shared Sub Main(args As String()) Dim ppt As New Presentation() Dim shape As IAutoShape = DirectCast(ppt.Slides(0).Shapes.AppendShape(ShapeType.DoubleWave, New RectangleF(100, 100, 400, 200)), IAutoShape) Dim picUrl As String = "C:\Users\Administrator\Desktop\image.jpg" shape.Fill.FillType = FillFormatType.Picture shape.Fill.PictureFill.Picture.Url = picUrl shape.Fill.PictureFill.FillType = PictureFillType.Stretch shape.ShapeStyle.LineColor.Color = Color.Transparent ppt.SaveToFile("shape.pptx", FileFormat.Pptx2010) System.Diagnostics.Process.Start("shape.pptx") End Sub End Class End Namespace