Picture Frame
A picture frame is a shape that contains an image—it is like a picture in a frame.
You can add an image to a slide through a picture frame. This way, you get to format the image by formatting the picture frame.
Tip
Aspose provides free converters—JPEG to PowerPoint and PNG to PowerPoint—that allow people to create presentations quickly from images.Create Picture Frame
- Create an instance of the Presentation class.
- Get a slide’s reference through its index.
- Create an IPPImage object by adding an image to the IImagescollection associated with the presentation object that will be used to fill the shape.
- Specify the image’s width and height.
- Create a PictureFrame based on the image’s width and height through the
AddPictureFrame
method exposed by the shape object associated with the referenced slide. - Add a picture frame (containing the picture) to the slide.
- Write the modified presentation as a PPTX file.
This C# code shows you how to create a picture frame:
// Instantiates the Presentation class that represents a PPTX file
using (Presentation pres = new Presentation())
{
// Gets the first slide
ISlide sld = pres.Slides[0];
// Instantiates the ImageEx class
System.Drawing.Image img = (System.Drawing.Image)new Bitmap("aspose-logo.jpg");
IPPImage imgx = pres.Images.AddImage(img);
// Adds a picture frame with the picture's equivalent height and width
IPictureFrame pf = sld.Shapes.AddPictureFrame(ShapeType.Rectangle, 50, 150, imgx.Width, imgx.Height, imgx);
// Applies some formatting to the PictureFrameEx
pf.LineFormat.FillFormat.FillType = FillType.Solid;
pf.LineFormat.FillFormat.SolidFillColor.Color = Color.Blue;
pf.LineFormat.Width = 20;
pf.Rotation = 45;
// Writes the PPTX file to disk
pres.Save("RectPicFrameFormat_out.pptx", SaveFormat.Pptx);
}
Create Picture Frame with Relative Scale
By altering an image’s relative scaling, you can create a more complicated picture frame.
- Create an instance of the Presentation class.
- Get a slide’s reference through its index.
- Add an image to the presentation image collection.
- Create an IPPImage object by adding an image to the IImagescollection associated with the presentation object that will be used to fill the shape.
- Specify the image’s relative width and height in the picture frame.
- Write the modified presentation as a PPTX file.
This C# code shows you how to create a picture frame with relative scale:
// Instantiates the Presentation class that represents a PPTX file
using (Presentation presentation = new Presentation())
{
// Loads the image that will be added to the presentation image collection
Image img = new Bitmap("aspose-logo.jpg");
IPPImage image = presentation.Images.AddImage(img);
// Adds a picture frame to the slide
IPictureFrame pf = presentation.Slides[0].Shapes.AddPictureFrame(ShapeType.Rectangle, 50, 50, 100, 100, image);
// Sets the relative scale width and height
pf.RelativeScaleHeight = 0.8f;
pf.RelativeScaleWidth = 1.35f;
// Saves the presentation
presentation.Save("Adding Picture Frame with Relative Scale_out.pptx", SaveFormat.Pptx);
}
Get Transparency of Image
Aspose.Slides allows you to get the transparency of an image. This C# code demonstrates the operation:
using (var presentation = new Presentation(folderPath + "Test.pptx"))
{
var pictureFrame = (IPictureFrame)presentation.Slides[0].Shapes[0];
var imageTransform = pictureFrame.PictureFormat.Picture.ImageTransform;
foreach (var effect in imageTransform)
{
if (effect is IAlphaModulateFixed alphaModulateFixed)
{
var transparencyValue = 100 - alphaModulateFixed.Amount;
Console.WriteLine("Picture transparency: " + transparencyValue);
}
}
}
Picture Frame Formatting
Aspose.Slides provides many formatting options that can be applied to a picture frame. Using those options, you can alter a picture frame to make it match specific requirements.
- Create an instance of the Presentation class.
- Get a slide’s reference through its index.
- Create an IPPImage object by adding an image to the IImagescollection associated with the presentation object that will be used to fill the shape.
- Specify the image’s width and height.
- Create a
PictureFrame
based on the image’s width and height through the AddPictureFrame method exposed by the IShapes object associated with the referenced slide. - Add the picture frame (containing the picture) to the slide.
- Set the picture frame’s line color.
- Set the picture frame’s line width.
- Rotate the picture frame by giving it either a positive or negative value.
- A positive value rotates the image clockwise.
- A negative value rotates the image anti-clockwise.
- Add the picture frame (containing the picture) to the slide.
- Write the modified presentation as a PPTX file.
This C# code demonstrates the picture frame formatting process:
// Instantiates the Presentation class that represents a PPTX file
using (Presentation pres = new Presentation())
{
// Gets the first slide
ISlide sld = pres.Slides[0];
// Instantiates the ImageEx class
System.Drawing.Image img = (System.Drawing.Image)new Bitmap("aspose-logo.jpg");
IPPImage imgx = pres.Images.AddImage(img);
// Adds a picture frame with the picture's equivalent height and width
IPictureFrame pf = sld.Shapes.AddPictureFrame(ShapeType.Rectangle, 50, 150, imgx.Width, imgx.Height, imgx);
// Applies some formatting to PictureFrameEx
pf.LineFormat.FillFormat.FillType = FillType.Solid;
pf.LineFormat.FillFormat.SolidFillColor.Color = Color.Blue;
pf.LineFormat.Width = 20;
pf.Rotation = 45;
// Writes the PPTX file to disk
pres.Save("RectPicFrameFormat_out.pptx", SaveFormat.Pptx);
}
Add Image as Link
To avoid large presentation sizes, you can add images (or videos) through links instead of embedding the files directly into presentations. This C# code shows you how to add an image and video into a placeholder:
using (var presentation = new Presentation("input.pptx"))
{
var shapesToRemove = new List<IShape>();
int shapesCount = presentation.Slides[0].Shapes.Count;
for (var i = 0; i < shapesCount; i++)
{
var autoShape = presentation.Slides[0].Shapes[i];
if (autoShape.Placeholder == null)
{
continue;
}
switch (autoShape.Placeholder.Type)
{
case PlaceholderType.Picture:
var pictureFrame = presentation.Slides[0].Shapes.AddPictureFrame(ShapeType.Rectangle,
autoShape.X, autoShape.Y, autoShape.Width, autoShape.Height, null);
pictureFrame.PictureFormat.Picture.LinkPathLong =
"https://upload.wikimedia.org/wikipedia/commons/3/3a/I.M_at_Old_School_Public_Broadcasting_in_October_2016_02.jpg";
shapesToRemove.Add(autoShape);
break;
case PlaceholderType.Media:
var videoFrame = presentation.Slides[0].Shapes.AddVideoFrame(
autoShape.X, autoShape.Y, autoShape.Width, autoShape.Height, "");
videoFrame.PictureFormat.Picture.LinkPathLong =
"https://upload.wikimedia.org/wikipedia/commons/3/3a/I.M_at_Old_School_Public_Broadcasting_in_October_2016_02.jpg";
videoFrame.LinkPathLong = "https://youtu.be/t_1LYZ102RA";
shapesToRemove.Add(autoShape);
break;
}
}
foreach (var shape in shapesToRemove)
{
presentation.Slides[0].Shapes.Remove(shape);
}
presentation.Save("output.pptx", SaveFormat.Pptx);
}
Crop Image
This C# code shows you how to crop an existing image on a slide:
using (Presentation presentation = new Presentation())
{
// Creates new image object
IPPImage newImage = presentation.Images.AddImage(Image.FromFile(imagePath));
// Adds a PictureFrame to a Slide
IPictureFrame picFrame = presentation.Slides[0].Shapes.AddPictureFrame(
ShapeType.Rectangle, 100, 100, 420, 250, newImage);
// Crops the image (percentage values)
picFrame.PictureFormat.CropLeft = 23.6f;
picFrame.PictureFormat.CropRight = 21.5f;
picFrame.PictureFormat.CropTop = 3;
picFrame.PictureFormat.CropBottom = 31;
// Saves the result
presentation.Save(outPptxFile, SaveFormat.Pptx);
}
Use StretchOff Property
Using the StretchOffsetLeft, StretchOffsetTop, StretchOffsetRight and StretchOffsetBottom properties from the IPictureFillFormat interface and PictureFillFormat class, you can specify a fill rectangle.
When stretching is specified for an image, a source rectangle is scaled to fit the specified fill rectangle. Each edge of the fill rectangle is defined by a percentage offset from the corresponding edge of the shape’s bounding box. A positive percentage specifies an inset while a negative percentage specifies an outset.
- Create an instance of the Presentation class.
- Get a slide’s reference through its index.
- Add a rectangle
AutoShape
. - Create an image.
- Set the shape’s fill type.
- Set the shape’s picture fill mode.
- Add a set image to fill the shape.
- Specify image offsets from the corresponding edge of the shape’s bounding box
- Write the modified presentation as a PPTX file.
This C# code demonstrates a process in which a StretchOff property is used:
using (Presentation pres = new Presentation())
{
IPPImage ppImage;
using (Image bitmap = new Bitmap("image.png"))
{
ppImage = pres.Images.AddImage(bitmap);
}
IPictureFrame pictureFrame = pres.Slides[0].Shapes.AddPictureFrame(ShapeType.Rectangle, 10, 10, 400, 400, ppImage);
// Sets the image stretched from each side in the shape body
pictureFrame.PictureFormat.PictureFillMode = PictureFillMode.Stretch;
pictureFrame.PictureFormat.StretchOffsetLeft = 24;
pictureFrame.PictureFormat.StretchOffsetRight = 24;
pictureFrame.PictureFormat.StretchOffsetTop = 24;
pictureFrame.PictureFormat.StretchOffsetBottom = 24;
pres.Save("imageStretch.pptx", SaveFormat.Pptx);
}