| Picture frame is also one of the shapes offered by Aspose.Slides for .NET . Adding picture frame to a slide is a bit trickier than simple shapes. A picture frame is like a picture in the frame. You can add any desired picture to your slide as a picture frame. Let us see, how we can do it. |
Adding Simple Picture Frames to Slide
To add a simple picture frame to your slide, please follow the steps below:
- Create an instance of Presentation class
- Obtain the reference of a slide by using its Position
- Create a Picture object using an image that will be used to fill the Shape
- Add the Picture object to the pictures collection of the presentation. Each picture is assigned a unique Id after being added to the pictures collection. This Id should be stored in some variable so that it can be used later
- Calculate the Width and Height of Picture, Slide and Picture Frame. This step is optional for developers
- For the picture width and picture height values, we take the Width and Height values of the Picture and multiply them by an integer. If you don't want to calculate it, you may have to test several integer values to produce the desired results. The reason for this is that the relative size of an image displayed on a slide is not only based on its pixel resolution but also its DPI
- Next, get the pixel resolution on the slide
- Calculate where to place the X and Y coordinates of the Picture Frame so that it is centered on the slide
- Add Picture Frame (containing the picture) to the slide
- Write the modified presentation as a PPT file
The above steps are implemented in the example given below.
Example
//Instantiate a Presentation object that represents a PPT file Presentation pres = new Presentation("demo.ppt"); //Accessing a slide using its slide position Slide slide = pres.GetSlideByPosition(2); //Creating a picture object that will be used to fill the ellipse Picture pic = new Picture(pres, "C:\\demo.jpg"); //Adding the picture object to pictures collection of the presentation //After the picture object is added, the picture is given a uniqe picture Id int picId = pres.Pictures.Add(pic); //Calculating picture width and height int pictureWidth = pres.Pictures[picId - 1].Image.Width * 4; int pictureHeight = pres.Pictures[picId - 1].Image.Height * 4; //Calculating slide width and height int slideWidth = slide.Background.Width; int slideHeight = slide.Background.Height; //Calculating the width and height of picture frame int pictureFrameWidth = Convert.ToInt32(slideWidth / 2 - pictureWidth / 2); int pictureFrameHeight = Convert.ToInt32(slideHeight / 2 - pictureHeight / 2); //Adding picture frame to the slide slide.Shapes.AddPictureFrame(picId, pictureFrameWidth, pictureFrameHeight, pictureWidth, pictureHeight); //Writing the presentation as a PPT file pres.Write("C:\\modified.ppt");
'Instantiate a Presentation object that represents a PPT file Dim pres As Presentation = New Presentation("demo.ppt") 'Accessing a slide using its slide position Dim slide As Slide = pres.GetSlideByPosition(2) 'Creating a picture object that will be used to fill the ellipse Dim pic As Picture = New Picture(pres, "C:\\demo.jpg") 'Adding the picture object to pictures collection of the presentation 'After the picture object is added, the picture is given a uniqe picture Id Dim picId As Integer = pres.Pictures.Add(pic) 'Calculating picture width and height Dim pictureWidth As Integer = pres.Pictures(picId - 1).Image.Width * 4 Dim pictureHeight As Integer = pres.Pictures(picId - 1).Image.Height * 4 'Calculating slide width and height Dim slideWidth As Integer = slide.Background.Width Dim slideHeight As Integer = slide.Background.Height 'Calculating the width and height of picture frame Dim pictureFrameWidth As Integer = Convert.ToInt32(slideWidth / 2 - pictureWidth / 2) Dim pictureFrameHeight As Integer = Convert.ToInt32(slideHeight / 2 - pictureHeight / 2) 'Adding picture frame to the slide slide.Shapes.AddPictureFrame(picId, pictureFrameWidth, pictureFrameHeight, pictureWidth, pictureHeight) 'Writing the presentation as a PPT file pres.Write("C:\modified.ppt")
The above code snippet adds a simple Picture Frame to the slide as shown below:

Figure : Picture Frame added to a slide
Controlling Picture Frame Formatting
The picture frame that we created in the above section is simple. We can also control the formatting of a picture frame according to our desire. There are many formatting settings that can be applied on a picture frame according to the desire of a user.
To control the formatting of a picture frame in your slide, please follow the steps below:
- Create an instance of Presentation class
- Obtain the reference of a slide by using its Position
- Create a Picture object using an image that will be used to fill the Shape
- Add the Picture object to the pictures collection of the presentation. Each picture is assigned a unique Id after being added to the pictures collection. This Id should be stored in some variable so that it can be used later
- Calculate the Width and Height of Picture, Slide and Picture Frame. This step is optional for developers
- For the picture width and height values, we take the Width and Height values of the Picture and multiple them by an integer. If you don't want to calculate it then you may have to test several integer values to produce the desired results. The reason for this is that the relative size of an image displayed on a slide is not only based on its pixel resolution but also its DPI
- Next, get the pixel resolution on the slide
- Calculate where to place the X and Y coordinates of the Picture Frame so that it is centered on the slide
- Enable the lines of Picture Frame to be visible or not
- Set the Foreground Color of the lines of Picture Frame
- Set the Width of the lines of Picture Frame
- Rotate the Picture Frame by giving it either a positive or negative value. Positive value will rotate the Picture Frame clockwise where as negative value will rotate the Picture Frame anti-clockwise.
- Add Picture Frame (containing the picture) to the slide
- Write the modified presentation as a PPT file
The above steps are implemented in the example given below.
Example
//Instantiate a Presentation object that represents a PPT file Presentation pres = new Presentation("demo.ppt"); //Accessing a slide using its slide position Slide slide = pres.GetSlideByPosition(2); //Creating a picture object that will be used to fill the ellipse Picture pic = new Picture(pres, "C:\\demo.jpg"); //Adding the picture object to pictures collection of the presentation //After the picture object is added, the picture is given a uniqe picture Id int picId = pres.Pictures.Add(pic); //Calculating picture width and height int pictureWidth = pres.Pictures[picId - 1].Image.Width * 3; int pictureHeight = pres.Pictures[picId - 1].Image.Height * 3; //Calculating slide width and height int slideWidth = slide.Background.Width; int slideHeight = slide.Background.Height; //Calculating the width and height of picture frame int pictureFrameWidth = Convert.ToInt32(slideWidth / 2 - pictureWidth / 2); int pictureFrameHeight = Convert.ToInt32(slideHeight / 2 - pictureHeight / 2); //Adding picture frame to the slide PictureFrame pf = slide.Shapes.AddPictureFrame(picId, pictureFrameWidth, pictureFrameHeight, pictureWidth, pictureHeight); //Showing the lines of the picture frame pf.LineFormat.ShowLines = true; //Setting the foreground color of the picture frame pf.LineFormat.ForeColor = Color.Blue; //Setting the width of the picture frame lines pf.LineFormat.Width = 20; //Rotate the picture frame to 45 degrees pf.Rotation = 45; //Writing the presentation as a PPT file pres.Write("C:\\modified.ppt");
'Instantiate a Presentation object that represents a PPT file Dim pres As Presentation = New Presentation("demo.ppt") 'Accessing a slide using its slide position Dim slide As Slide = pres.GetSlideByPosition(2) 'Creating a picture object that will be used to fill the ellipse Dim pic As Picture = New Picture(pres, "C:\demo.jpg") 'Adding the picture object to pictures collection of the presentation 'After the picture object is added, the picture is given a uniqe picture Id Dim picId As Integer = pres.Pictures.Add(pic) 'Calculating picture width and height Dim pictureWidth As Integer = pres.Pictures(picId - 1).Image.Width * 3 Dim pictureHeight As Integer = pres.Pictures(picId - 1).Image.Height * 3 'Calculating slide width and height Dim slideWidth As Integer = slide.Background.Width Dim slideHeight As Integer = slide.Background.Height 'Calculating the width and height of picture frame Dim pictureFrameWidth As Integer = Convert.ToInt32(slideWidth / 2 - pictureWidth / 2) Dim pictureFrameHeight As Integer = Convert.ToInt32(slideHeight / 2 - pictureHeight / 2) 'Adding picture frame to the slide Dim pf As PictureFrame = slide.Shapes.AddPictureFrame(picId, pictureFrameWidth,pictureFrameHeight,pictureWidth,pictureHeight) 'Showing the lines of the picture frame pf.LineFormat.ShowLines = True 'Setting the foreground color of the picture frame pf.LineFormat.ForeColor = Color.Blue 'Setting the width of the picture frame lines pf.LineFormat.Width = 20 'Rotate the picture frame to 45 degrees pf.Rotation = 45 'Writing the presentation as a PPT file pres.Write("C:\modified.ppt")
The above code snippet adds a more controlled formatted Picture Frame to the slide as shown below:
Figure : Formatted Picture Frame added to a slide
