internal StateToken(CropDecoratorSettings settings)
 {
     this.settings = settings;
     cropRectangle = settings.CropRectangle;
     aspectRatio   = settings.AspectRatio;
     aspectRatioId = settings.AspectRatioId;
 }
 public void AdjustOriginalSize(IProperties properties, ref Size size)
 {
     CropDecoratorSettings settings = new CropDecoratorSettings(properties);
     Rectangle? rect = settings.CropRectangle;
     if (rect != null)
         size = rect.Value.Size;
 }
        public void AdjustOriginalSize(IProperties properties, ref Size size)
        {
            CropDecoratorSettings settings = new CropDecoratorSettings(properties);
            Rectangle?            rect     = settings.CropRectangle;

            if (rect != null)
            {
                size = rect.Value.Size;
            }
        }
        public void Decorate(ImageDecoratorContext context)
        {
            CropDecoratorSettings settings = new CropDecoratorSettings(context.Settings);
            //ImageUtils.AdjustBrightness(bsettings.Brightness, bsettings.Contrast, context.Image);
            Rectangle?rect = settings.CropRectangle;

            // only do the following if polaroid is active
            if (context.EnforcedAspectRatio != null)
            {
                float          aspectRatio = context.EnforcedAspectRatio.Value;
                RotateFlipType flip;
                if (context.InvocationSource == ImageDecoratorInvocationSource.InitialInsert ||
                    context.InvocationSource == ImageDecoratorInvocationSource.Reset)
                {
                    flip = ImageUtils.GetFixupRotateFlipFromExifOrientation(context.Image);
                }
                else
                {
                    flip = context.ImageRotation;
                }
                if (ImageUtils.IsRotated90(flip))
                {
                    aspectRatio = 1 / aspectRatio;
                }

                rect = RectangleHelper.EnforceAspectRatio(rect ?? new Rectangle(Point.Empty, context.Image.Size), aspectRatio);
            }

            if (rect != null)
            {
                Bitmap cropped = ImageHelper2.CropBitmap(context.Image, (Rectangle)rect);
                try
                {
                    PropertyItem orientation = context.Image.GetPropertyItem(0x112);
                    if (orientation != null)
                    {
                        cropped.SetPropertyItem(orientation);
                    }
                }
                catch (ArgumentException)
                {
                    // orientation data was not present
                }
                context.Image = cropped;
            }
        }
        protected override void OnSaveSettings()
        {
            base.OnSaveSettings();
            CropDecoratorSettings settings = new CropDecoratorSettings(Settings);

            if (imageCropControl.Enabled)
            {
                settings.CropRectangle = RectangleHelper.UndoRotateFlip(imageCropControl.Bitmap.Size, imageCropControl.CropRectangle, EditorContext.ImageRotation);
                settings.AspectRatioId = ((AspectRatioItem)(cbAspectRatio.SelectedItem ?? cbAspectRatio.Items[1])).Id;
                settings.AspectRatio   = imageCropControl.AspectRatio;
            }
            else
            {
                // indicates that crop should be removed
                settings.CropRectangle = null;
                settings.AspectRatioId = null;
                settings.AspectRatio   = null;
            }
        }
        public void Decorate(ImageDecoratorContext context)
        {
            CropDecoratorSettings settings = new CropDecoratorSettings(context.Settings);
            //ImageUtils.AdjustBrightness(bsettings.Brightness, bsettings.Contrast, context.Image);
            Rectangle? rect = settings.CropRectangle;

            // only do the following if polaroid is active
            if (context.EnforcedAspectRatio != null)
            {
                float aspectRatio = context.EnforcedAspectRatio.Value;
                RotateFlipType flip;
                if (context.InvocationSource == ImageDecoratorInvocationSource.InitialInsert ||
                        context.InvocationSource == ImageDecoratorInvocationSource.Reset)
                {
                    flip = ImageUtils.GetFixupRotateFlipFromExifOrientation(context.Image);
                }
                else
                {
                    flip = context.ImageRotation;
                }
                if (ImageUtils.IsRotated90(flip))
                    aspectRatio = 1 / aspectRatio;

                rect = RectangleHelper.EnforceAspectRatio(rect ?? new Rectangle(Point.Empty, context.Image.Size), aspectRatio);
            }

            if (rect != null)
            {
                Bitmap cropped = ImageHelper2.CropBitmap(context.Image, (Rectangle)rect);
                try
                {
                    PropertyItem orientation = context.Image.GetPropertyItem(0x112);
                    if (orientation != null)
                        cropped.SetPropertyItem(orientation);
                }
                catch (ArgumentException)
                {
                    // orientation data was not present
                }
                context.Image = cropped;
            }
        }
 internal StateToken(CropDecoratorSettings settings)
 {
     this.settings = settings;
     cropRectangle = settings.CropRectangle;
     aspectRatio = settings.AspectRatio;
     aspectRatioId = settings.AspectRatioId;
 }
        protected override void LoadEditor()
        {
            bitmap = (Bitmap)State;
            Size origSize = bitmap.Size;

            bitmap.RotateFlip(EditorContext.ImageRotation);
            imageCropControl.Bitmap = bitmap;
            ((AspectRatioItem)cbAspectRatio.Items[0]).AspectRatio = bitmap.Width / (double)bitmap.Height;

            CropDecoratorSettings settings = new CropDecoratorSettings(Settings);

            originalState = settings.CreateStateToken();

            Rectangle?cropRectangle = settings.CropRectangle;

            if (EditorContext.EnforcedAspectRatio != null)
            {
                Rectangle rect;
                if (cropRectangle != null)
                {
                    rect = RectangleHelper.RotateFlip(origSize, cropRectangle.Value, EditorContext.ImageRotation);
                }
                else
                {
                    rect = new Rectangle(Point.Empty, bitmap.Size);
                }

                rect = RectangleHelper.EnforceAspectRatio(rect, EditorContext.EnforcedAspectRatio.Value);
                imageCropControl.CropRectangle = rect;
                imageCropControl.AspectRatio   = EditorContext.EnforcedAspectRatio.Value;
            }
            else if (cropRectangle != null)
            {
                string savedAspectRatioId = settings.AspectRatioId;
                double?savedAspectRatio   = settings.AspectRatio;
                if (savedAspectRatioId != null)
                {
                    foreach (AspectRatioItem item in cbAspectRatio.Items)
                    {
                        if (item.Id == savedAspectRatioId)
                        {
                            // doubles can't be accurately compared after they've been round-tripped
                            // to strings, due to lossy conversion to/from strings.
                            if ((float)(item.AspectRatio ?? 0.0) != (float)(savedAspectRatio ?? 0.0))
                            {
                                if (item.AspectRatio == null || savedAspectRatio == null)
                                {
                                    continue;
                                }
                                if (item.AspectRatio.Value != 1 / savedAspectRatio.Value)
                                {
                                    continue;
                                }
                            }

                            cbAspectRatio.SelectedItem   = item;
                            imageCropControl.AspectRatio = savedAspectRatio;
                            break;
                        }
                    }
                }
                if (cbAspectRatio.SelectedIndex == -1)
                {
                    cbAspectRatio.SelectedIndex = 1;
                }
                imageCropControl.CropRectangle = RectangleHelper.RotateFlip(origSize, cropRectangle.Value, EditorContext.ImageRotation);
            }
            else
            {
                cbAspectRatio.SelectedIndex = 1;
                UpdatePreview(true);
            }
        }