crop effect in VSTO addin like crop to fill

19 Views Asked by At

I am using this code to get a crop effect in VSTO addin like crop to fill but setting cropleft or bottom gives exception of value out of range , here is my code try { // Get the active presentation and active window PowerPoint.Presentation activePresentation = Globals.ThisAddIn.Application.ActivePresentation; PowerPoint.DocumentWindow activeWindow = Globals.ThisAddIn.Application.ActiveWindow;

   if (activePresentation != null && activeWindow != null)
   {
       // Get the selection in the active window
       PowerPoint.Selection selection = activeWindow.Selection;

       if (selection != null)
       {

           // Check if the selection is a shape
           if (selection.Type == PowerPoint.PpSelectionType.ppSelectionShapes)
           {
               // Get the first selected shape
               PowerPoint.Shape selectedShape = selection.ShapeRange[1];

               // Check if the selected shape is a picture placeholder

               // Set the picture format properties
                             // Insert the image into the selected shape
               selectedShape.Fill.UserPicture(imageUrl);

               float currentRotation = selectedShape.Rotation;
               float cropLeft = selectedShape.PictureFormat.CropLeft;
               float cropRight = selectedShape.PictureFormat.CropRight;

               // Adjust the Crop properties for CropToFill effect
     selectedShape.PictureFormat.CropLeft = cropLeft;
            //   selectedShape.LockAspectRatio = MsoTriState.msoTrue;


           }
           else
           {
               PowerPoint.Slide activeSlide = (PowerPoint.Slide)Globals.ThisAddIn.Application.ActiveWindow.View.Slide;

               // Specify the position and size for the image
               float left = 200f;  // adjust the values according to your needs
               float top = 0;
               float width = 400f;
               float height = activeSlide.Master.Height;

               // Add a picture to the slide
               // replace with the actual path to your image file
               activeSlide.Shapes.AddPicture(imageUrl, Microsoft.Office.Core.MsoTriState.msoFalse, Microsoft.Office.Core.MsoTriState.msoCTrue,
                                             left, top, width, height);

           }
           if (downloadUrl != null)
           {
               unSplash unSplash = new unSplash();
               unSplash.MakeDownloadCall(downloadUrl);
           }
       }
   }

}

I am using this code to get a crop effect in VSTO addin like crop to fill but setting cropleft or bottom gives exception of value out of range , here is my code

1

There are 1 best solutions below

0
Eugene Astafiev On

The CropLeft property returns or sets the number of points that are cropped off the left side of the specified picture or OLE object. Be aware, cropping is calculated relative to the original size of the picture. For example, if you insert a picture that is originally 100 points wide, rescale it so that it is 200 points wide, and then set the CropLeft property to 50, 100 points (not 50) will be cropped off the left side of your picture.

This following VBA sample code crops the percentage specified by the user off the left of the selected shape, regardless of whether the shape has been scaled. For the example to work, the selected shape must be either a picture or an OLE object.

percentToCrop = InputBox("What percentage do you " & _
    "want to crop off the bottom of this picture?")

Set shapeToCrop = ActiveWindow.Selection.ShapeRange(1)

With shapeToCrop.Duplicate
    .ScaleHeight 1, True
    origHeight = .Height
    .Delete
End With

cropPoints = origHeight * percentToCrop / 100

shapeToCrop.PictureFormat.CropLeft = cropPoints