Ejemplo n.º 1
0
        public static PowerPoint.ShapeRange Crop(PowerPoint.ShapeRange shapeRange, float aspectRatio)
        {
            bool hasChange = false;

            for (int i = 1; i <= shapeRange.Count; i++)
            {
                PowerPoint.ShapeRange origShape = shapeRange[i].Duplicate();
                origShape.ScaleWidth(1, Office.MsoTriState.msoTrue);
                origShape.ScaleHeight(1, Office.MsoTriState.msoTrue);
                float origWidth  = origShape.Width;
                float origHeight = origShape.Height;
                origShape.Delete();

                float currentWidth       = shapeRange[i].Width - (shapeRange[i].PictureFormat.CropLeft + shapeRange[i].PictureFormat.CropRight) / origWidth;
                float currentHeight      = shapeRange[i].Height - (shapeRange[i].PictureFormat.CropTop + shapeRange[i].PictureFormat.CropBottom) / origHeight;
                float currentProportions = currentWidth / currentHeight;

                if (IsApproximatelyEquals(currentProportions, aspectRatio))
                {
                    continue;
                }
                else if (currentProportions > aspectRatio)
                {
                    // Crop the width
                    float desiredWidth = currentHeight * aspectRatio;
                    float widthToCrop  = origWidth * ((currentWidth - desiredWidth) / currentWidth);
                    CropHorizontal(shapeRange[i], widthToCrop);
                    hasChange = true;
                }
                else if (currentProportions < aspectRatio)
                {
                    // Crop the height
                    float desiredHeight = currentWidth / aspectRatio;
                    float heightToCrop  = origHeight * ((currentHeight - desiredHeight) / currentHeight);
                    CropVertical(shapeRange[i], heightToCrop);
                    hasChange = true;
                }
            }

            if (!hasChange)
            {
                throw new CropLabException(CropLabErrorHandler.ErrorCodeNoAspectRatioCropped.ToString());
            }

            return(shapeRange);
        }
Ejemplo n.º 2
0
        public static PowerPoint.ShapeRange Crop(PowerPoint.ShapeRange shapeRange)
        {
            bool hasChange = false;

            for (int i = 1; i <= shapeRange.Count; i++)
            {
                PowerPoint.Shape shape = shapeRange[i];

                // Store initial properties
                float currentRotation = shape.Rotation;
                float cropLeft        = shape.PictureFormat.CropLeft;
                float cropRight       = shape.PictureFormat.CropRight;
                float cropTop         = shape.PictureFormat.CropTop;
                float cropBottom      = shape.PictureFormat.CropBottom;

                // Set properties to zero to do proper calculations
                shape.PictureFormat.CropLeft   = 0;
                shape.PictureFormat.CropRight  = 0;
                shape.PictureFormat.CropTop    = 0;
                shape.PictureFormat.CropBottom = 0;
                shape.Rotation = 0;

                // Get unscaled dimensions
                PowerPoint.ShapeRange origShape = shape.Duplicate();
                origShape.ScaleWidth(1, Office.MsoTriState.msoTrue);
                origShape.ScaleHeight(1, Office.MsoTriState.msoTrue);
                float origWidth  = origShape.Width;
                float origHeight = origShape.Height;
                origShape.SafeDelete();

                Rectangle origImageRect    = new Rectangle();
                Rectangle croppedImageRect = new Rectangle();

                Utils.GraphicsUtil.ExportShape(shape, TempPngFileExportPath);
                using (Bitmap shapeBitmap = new Bitmap(TempPngFileExportPath))
                {
                    origImageRect = new Rectangle(0, 0, shapeBitmap.Width, shapeBitmap.Height);
                    try
                    {
                        croppedImageRect = GetImageBoundingRect(shapeBitmap, shape.Name);
                    }
                    catch (NotSupportedException e)
                    {
                        throw e;
                    }
                }

                float cropRatioLeft   = croppedImageRect.Left / (float)origImageRect.Width;
                float cropRatioRight  = (origImageRect.Width - croppedImageRect.Width) / (float)origImageRect.Width;
                float cropRatioTop    = croppedImageRect.Top / (float)origImageRect.Height;
                float cropRatioBottom = (origImageRect.Height - croppedImageRect.Height) / (float)origImageRect.Height;

                float newCropLeft   = Math.Max(origWidth * cropRatioLeft, cropLeft);
                float newCropRight  = Math.Max(origWidth * cropRatioRight, cropRight);
                float newCropTop    = Math.Max(origHeight * cropRatioTop, cropTop);
                float newCropBottom = Math.Max(origHeight * cropRatioBottom, cropBottom);

                if (!hasChange &&
                    (!IsApproximatelySame(newCropLeft, cropLeft) ||
                     !IsApproximatelySame(newCropRight, cropRight) ||
                     !IsApproximatelySame(newCropTop, cropTop) ||
                     !IsApproximatelySame(newCropBottom, cropBottom)))
                {
                    hasChange = true;
                }

                shape.Rotation = currentRotation;
                shape.PictureFormat.CropLeft   = newCropLeft;
                shape.PictureFormat.CropRight  = newCropRight;
                shape.PictureFormat.CropTop    = newCropTop;
                shape.PictureFormat.CropBottom = newCropBottom;
            }

            if (!hasChange)
            {
                throw new CropLabException(CropLabErrorHandler.ErrorCodeNoPaddingCropped.ToString());
            }

            return(shapeRange);
        }