Introduction
Picture frame is also one of the shapes offered by Aspose.Slides. Adding picture frame to a slide is bit more tricky than simple shapes. A picture frame is like a picture in a frame. You can add any desired picture to your slide as a picture frame. Let's see, how can we do it.
Adding Picture Frames to Slides
Simple Picture Frame
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 pictureWidth and pictureHeight 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
- 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:
[C#]
//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");
[VB.NET]
'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")
[Java]
//Note: This code sample requires you to use the Java Advanced Imaging Component
try
{
//Instantiate a Presentation object that represents a PPT file
Presentation pres = new Presentation(new FileInputStream(new File("demo.ppt")));
//Accessing a slide using its slide position
Slide slide = pres.getSlideByPosition(2);
//Creating a stream to hold the image file
InputStream iStream = new BufferedInputStream(new FileInputStream("C:\\demo.jpg"));
//Creating a picture object
Picture pic = new com.Aspose.Slides.Picture(pres, iStream);
//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.getPictures().add(pic);
//Using the Java Advanced Imaging Component
RenderedOp img = JAI.create("fileload", "C:\\demo.jpg");
//Calculating picture width and height
int pictureWidth = img.getWidth() * 4;
int pictureHeight = img.getHeight() * 4;
//Calculating slide width and height
int slideWidth = slide.getBackground().getWidth();
int slideHeight = slide.getBackground().getHeight();
//Calculating the width and height of picture frame
int pictureFrameWidth = (slideWidth/2 - pictureWidth/2);
int pictureFrameHeight = (slideHeight/2 - pictureHeight/2);
//Adding picture frame to the slide
slide.getShapes().addPictureFrame(picId, pictureFrameWidth,
pictureFrameHeight, pictureWidth, pictureHeight);
//Writing the presentation as a PPT file
pres.write(new FileOutputStream(new File("modified.ppt")));
}
catch(Exception ex)
{
System.out.println(ex.toString());
}
[PHP]
<?php
//Using aspose.slides.jar file so that the classes inside the jar file
//can be used
java_require("aspose.slides.jar");
try
{
//Creating a file input stream to read the PPT file
$fistream1=new Java("java.io.FileInputStream","C:\\demo.ppt");
//Instantiate a Presentation object that represents a PPT file
$pres=new Java("com.aspose.slides.Presentation",$fistream1);
//Accessing a slide using its slide position
$slide=$pres->getSlideByPosition(2);
//Creating a temporary stream to hold the image file
$fistream2=new Java("java.io.FileInputStream","C:\\demo.jpg");
//Converting the image stream to a simple input stream
$iStream=new Java("java.io.BufferedInputStream",$fistream2);
//Creating a picture object
$pic = new Java("com.aspose.slides.Picture",$pres,$iStream);
//Adding the picture object to pictures collection of the presentation
//After the picture object is added, the picture is given a uniqe picture Id
$picId = $pres->getPictures()->add($pic);
//Creating JAI class of Java Advanced Imaging API
$JAI=new JavaClass("javax.media.jai.JAI");
//Using the Java Advanced Imaging Component
$img = $JAI->create("fileload","C:\\demo.jpg");
//Calculating picture width and height
$pictureWidth = $img->getWidth() * 4;
$pictureHeight = $img->getHeight() * 4;
//Calculating slide width and height
$slideWidth = $slide->getBackground()->getWidth();
$slideHeight = $slide->getBackground()->getHeight();
//Calculating the width and height of picture frame
$pictureFrameWidth = ($slideWidth/2 - $pictureWidth/2);
$pictureFrameHeight = ($slideHeight/2 - $pictureHeight/2);
//Adding picture frame to the slide
$slide->getShapes()->addPictureFrame($picId, $pictureFrameWidth,
$pictureFrameHeight, $pictureWidth, $pictureHeight);
//Creating a file output stream to write the output file
$fostream=new Java("java.io.FileOutputStream","C:\\modified.ppt");
//Writing the presentation as a PPT file
$pres->write($fostream);
//Closing the streams
$fistream1->close();
$fistream2->close();
$iStream->close();
$fostream->close();
}
catch(JavaException $ex)
{
echo $ex->toString();
}
?>
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 pictureWidth and pictureHeight 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:
[C#]
//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");
[VB.NET]
'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