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 slide = pres.Slides[0];
// Loads an image and adds it to the presentation image collection
IImage image = Images.FromFile("aspose-logo.jpg");
IPPImage ppImage = pres.Images.AddImage(image);
image.Dispose();
// Adds a picture frame with the same height and width
IPictureFrame pictureFrame = slide.Shapes.AddPictureFrame(ShapeType.Rectangle, 50, 150, ppImage.Width, ppImage.Height, ppImage);
// Applies some formatting to the picture frame
pictureFrame.LineFormat.FillFormat.FillType = FillType.Solid;
pictureFrame.LineFormat.FillFormat.SolidFillColor.Color = Color.Blue;
pictureFrame.LineFormat.Width = 20;
pictureFrame.Rotation = 45;
// Writes the presentation to a PPTX file
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 an image and adds it to the presentation image collection
IImage image = Images.FromFile("aspose-logo.jpg");
IPPImage ppImage = presentation.Images.AddImage(image);
image.Dispose();
// Adds a picture frame to the slide
IPictureFrame pictureFrame = presentation.Slides[0].Shapes.AddPictureFrame(ShapeType.Rectangle, 50, 50, 100, 100, ppImage);
// Sets the relative scale width and height
pictureFrame.RelativeScaleHeight = 0.8f;
pictureFrame.RelativeScaleWidth = 1.35f;
// Saves the presentation
presentation.Save("Adding Picture Frame with Relative Scale_out.pptx", SaveFormat.Pptx);
}
Extract Image from Picture Frame
You can extract images from PictureFrame objects and save them in PNG, JPG, and other formats. The code example below demonstrates how to extract an image from the document “sample.pptx” and save it in PNG format.
using (var presentation = new Presentation("sample.pptx"))
{
var firstSlide = presentation.Slides[0];
var firstShape = firstSlide.Shapes[0];
if (firstShape is IPictureFrame pictureFrame)
{
var image = pictureFrame.PictureFormat.Picture.Image.SystemImage;
image.Save("slide_1_shape_1.png", ImageFormat.Png);
}
}
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 presentation = new Presentation())
{
// Gets the first slide
ISlide slide = presentation.Slides[0];
// Loads an image and adds it to the presentation image collection
IImage image = Images.FromFile("aspose-logo.jpg");
IPPImage ppImage = presentation.Images.AddImage(image);
image.Dispose();
// Adds a picture frame with the picture's equivalent height and width
IPictureFrame pictureFrame = slide.Shapes.AddPictureFrame(ShapeType.Rectangle, 50, 150, ppImage.Width, ppImage.Height, ppImage);
// Applies some formatting to the picture frame
pictureFrame.LineFormat.FillFormat.FillType = FillType.Solid;
pictureFrame.LineFormat.FillFormat.SolidFillColor.Color = Color.Blue;
pictureFrame.LineFormat.Width = 20;
pictureFrame.Rotation = 45;
// Writes the presentation to a PPTX file
presentation.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 a new image object
IImage image = Images.FromFile(imagePath);
IPPImage newImage = presentation.Images.AddImage(image);
image.Dispose();
// 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("PictureFrameCrop.pptx", SaveFormat.Pptx);
}
Delete Cropped Areas of Picture
If you want to delete the cropped areas of an image contained in a frame, you can use the IPictureFillFormat.DeletePictureCroppedAreas method. This method returns the cropped image or the origin image if cropping is unnecessary.
This C# code demonstrates the operation:
using (Presentation presentation = new Presentation("PictureFrameCrop.pptx"))
{
ISlide slide = presentation.Slides[0];
// Gets the PictureFrame from the first slide
IPictureFrame picFrame = slide.Shapes[0] as IPictureFrame;
// Deletes cropped areas of the PictureFrame image and returns the cropped image
IPPImage croppedImage = picFrame.PictureFormat.DeletePictureCroppedAreas();
// Saves the result
presentation.Save("PictureFrameDeleteCroppedAreas.pptx", SaveFormat.Pptx);
}
NOTE
The IPictureFillFormat.DeletePictureCroppedAreas method adds the cropped image to the presentation image collection. If the image is only used in the processed PictureFrame, this setup can reduce the presentation size. Otherwise, the number of images in the resulting presentation will increase.
This method converts WMF/EMF metafiles to raster PNG image in the cropping operation.
Lock Aspect Ratio
If you want a shape containing an image to retain its aspect ratio even after you change the image dimensions, you can use the IPictureFrameLock.AspectRatioLocked property to set the Lock Aspect Ratio setting.
This C# code shows you how to lock a shape’s aspect ratio:
using (Presentation pres = new Presentation("pres.pptx"))
{
ILayoutSlide layout = pres.LayoutSlides.GetByType(SlideLayoutType.Custom);
ISlide emptySlide = pres.Slides.AddEmptySlide(layout);
IImage image = Images.FromFile("image.png");
IPPImage presImage = pres.Images.AddImage(image);
image.Dispose();
IPictureFrame pictureFrame = emptySlide.Shapes.AddPictureFrame(ShapeType.Rectangle, 50, 150, presImage.Width, presImage.Height, presImage);
// Sets shape to preserve aspect ratio on resizing
pictureFrame.PictureFrameLock.AspectRatioLocked = true;
}
NOTE
This Lock Aspect Ratio setting preserves only the aspect ratio of the shape and not the image it contains.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())
{
IImage image = Images.FromFile("image.png");
IPPImage ppImage = pres.Images.AddImage(image);
image.Dispose();
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);
}