/// <summary>
 /// Intializes new instace of the SelectedConfigurationIndexChangedEventArgs class.</summary>
 /// <param name="outputResolution">The resolution (DPI) of the image that is generated by the control.</param>
 /// <param name="cropConstraint">The constraints that have to be satisfied by the cropped image.</param>
 /// <param name="postProcessingFilter">The filter(s) to apply to the image.</param>
 /// <param name="previewFilter">The filter(s) to apply to the preview image.</param>
 public SelectedConfigurationIndexChangedEventArgs(float outputResolution, CropConstraint cropConstraint, ImageProcessingFilter postProcessingFilter, ImageProcessingFilter previewFilter)
     : base(outputResolution, cropConstraint, postProcessingFilter, previewFilter)
 {
 }
 /// <summary>
 /// Intializes new instace of the ImageUploadEventArgs class.</summary>
 /// <param name="outputResolution">The resolution (DPI) of the image that is generated by the control.</param>
 /// <param name="cropConstraint">The constraints that have to be satisfied by the cropped image.</param>
 /// <param name="postProcessingFilter">The filter(s) to apply to the image.</param>
 /// <param name="previewFilter">The filter(s) to apply to the preview image.</param>
 public ImageUploadEventArgs(float outputResolution, CropConstraint cropConstraint, ImageProcessingFilter postProcessingFilter, ImageProcessingFilter previewFilter)
     : base(outputResolution, cropConstraint, postProcessingFilter, previewFilter)
 {
 }
    protected void ProcessSelectedConfigurationIndexChanged()
    {
        // The new image has been edited
        this._ImageUploaded = false;
        this._ImageEdited = true;

        // Update the preview
        this._UpdatePreview = true;

        // Open the image edit popup
        this._AutoOpenImageEditPopup = true;

        if (this.SelectedConfigurationIndexChanged != null)
        {
            string pictureTrimmerTID = this.popupPictureTrimmer1.TemporaryFileId;
            /// EVENT: Configuration index changed
            SelectedConfigurationIndexChangedEventArgs args = new SelectedConfigurationIndexChangedEventArgs(this._OutputResolution, this.CropConstraint, this.PostProcessingFilter, this.PreviewFilter);
            this.OnSelectedConfigurationIndexChanged(args);
            if (this.HasImage)
            {
                if (this.popupPictureTrimmer1.TemporaryFileId != pictureTrimmerTID)
                {
                    // The image has been reloeaded outside the control, exit.
                    return;
                }
            }
            else
            {
                // The image has been unloaded, exit.
                return;
            } 

            bool reloadImage = false;
            if (args.OutputResolutionChanged)
            {
                this._OutputResolution = args.OutputResolution;
                reloadImage = true;
            }
            if (args.CropConstraintChanged)
            {
                this._CropConstraint = args.CropConstraint;
                reloadImage = true;
            }
            if (args.PostProcessingFilterChanged)
            {
                this._PostProcessingFilter = args.PostProcessingFilter;
                // No need to reload if only the post filter has changed
                // AND - the updatePreview is surely already TRUE
            }
            if (args.PreviewFilterChanged)
            {
                this._PreviewFilter = args.PreviewFilter;
                // No need to reload if only the preview filter has changed
                // AND - the updatePreview is surely already TRUE
            }
            if (args.ReloadImageSet)
            {
                // Forced to reload the source image
                reloadImage = true;
            }

            if (reloadImage)
            {
                // Reload the image
                if (!args.ReloadImageSet)
                {
                    // Standard reload, use the current source image size to save memory
                    this.popupPictureTrimmer1.SetLoadImageData_ImageSize(this.SourceImageSize);
                    this.popupPictureTrimmer1.SetLoadImageData_ImageResolution(this.SourceImageResolution);
                    this.popupPictureTrimmer1.SetLoadImageData_ImageFormatId(this.SourceImageFormatId);
                }
                this.popupPictureTrimmer1.LoadImageFromFileSystem(this.TemporarySourceImageFilePath, this._OutputResolution, this.CropConstraint);
            }
        }
    }
        /// <summary>
        /// Intializes new instace of the ConfigurationEventArgs class.</summary>
        /// <param name="outputResolution">The resolution (DPI) of the image that is generated by the control.</param>
        /// <param name="cropConstraint">The constraints that have to be satisfied by the cropped image.</param>
        /// <param name="postProcessingFilter">The filter(s) to apply to the image.</param>
        /// <param name="previewFilter">The filter(s) to apply to the preview image.</param>
        public ConfigurationEventArgs(float outputResolution, CropConstraint cropConstraint, ImageProcessingFilter postProcessingFilter, ImageProcessingFilter previewFilter)
        {
            this._OutputResolution = outputResolution;
            this._CropConstraint = cropConstraint;
            this._PostProcessingFilter = postProcessingFilter;
            this._PreviewFilter = previewFilter;

            this._OriginalOutputResolution = this._OutputResolution;
            this._OriginalCropConstraintString = JSONSerializer.SerializeToString(this._CropConstraint, true);
            this._OriginalPostProcessingFilterString = JSONSerializer.SerializeToString(this._PostProcessingFilter, true);
            this._OriginalPreviewFilterString = JSONSerializer.SerializeToString(this._PreviewFilter, true);

            this._ReloadImageSet = false;
        }
    protected void ProcessUploadSuccess()
    {
        string sourceImageClientFileName = this._SourceImageClientFileName;

        if (this.HasImage)
        {
            // Unload the current image
            this.UnloadImage(false);
        }

        // Delete old files
        if (File.Exists(this.TemporarySourceImageFilePath))
        {
            File.Delete(this.TemporarySourceImageFilePath);
        }

        if (this._ImageUploadPreProcessingFilter == null)
        {
            // Just copy the source image
            File.Copy(this.UploadFilePath, this.TemporarySourceImageFilePath, true);
        }

        try
        {
            if (this._ImageUploadPreProcessingFilter != null)
            {
                // Pre-process the just uploaded image
                using (LoadedImage sourceImage = ImageArchiver.LoadImage(this.UploadFilePath))
                {
                    //  Use PNG to preserve transparency
                    FormatEncoderParams format = new PngFormatEncoderParams();
                    using (System.Drawing.Image tempImage = this._ImageUploadPreProcessingFilter.GetProcessedImage(sourceImage, sourceImage.Resolution, format))
                    {
                        ImageArchiver.SaveImageToFileSystem(tempImage, this.TemporarySourceImageFilePath, format);

                        // Optimization: save server resources...
                        this.popupPictureTrimmer1.SetLoadImageData_ImageSize(tempImage.Size);
                        this.popupPictureTrimmer1.SetLoadImageData_ImageResolution(sourceImage.Resolution);
                        this.popupPictureTrimmer1.SetLoadImageData_ImageFormatId(sourceImage.FormatId);
                    }
                }
            }

            // Load the image in the PictureTrimmer control
            this.popupPictureTrimmer1.LoadImageFromFileSystem(this.TemporarySourceImageFilePath, this._OutputResolution, this.CropConstraint);
        }
        catch (InvalidImageSizeException ex)
        {
            // Invalid image size
            ex.ToString();

            // Display the invalid image size message
            this.SetCurrentStatusMessage(this.StatusMessage_InvalidImageSize, true);

            // EVENT: Upload error (invalid image size)
            this.OnUploadError(EventArgs.Empty);
        }
        catch
        {
            // Invalid image

            // Display the invalid image message
            this.SetCurrentStatusMessage(this.StatusMessage_InvalidImage, true);

            // EVENT: Upload error (invalid image)
            this.OnUploadError(EventArgs.Empty);
        }

        if (this.HasImage)
        {
            // Restore the source image client file name (changed in the UnloadImage method)
            this._SourceImageClientFileName = sourceImageClientFileName;

            // The new image has been uploaded
            this._ImageUploaded = true;
            this._ImageEdited = false;

            // Update the preview
            this._UpdatePreview = true;

            if (this.ImageUpload != null)
            {
                // EVENT: Image upload
                string pictureTrimmerTID = this.popupPictureTrimmer1.TemporaryFileId;
                ImageUploadEventArgs args = new ImageUploadEventArgs(this._OutputResolution, this.CropConstraint, this.PostProcessingFilter, this.PreviewFilter);
                this.OnImageUpload(args);
                if (this.HasImage)
                {
                    if (this.popupPictureTrimmer1.TemporaryFileId != pictureTrimmerTID)
                    {
                        // The image has been reloeaded outside the control

                        if (this.AutoOpenImageEditPopupAfterUpload)
                        {
                            // Open the image edit popup if necessary
                            this.OpenImageEditPopup();
                        }

                        // Exit !!!
                        return;
                    }
                }
                else
                {
                    // The image has been unloaded, exit.
                    return;
                }

                bool reloadImage = false;
                if (args.OutputResolutionChanged)
                {
                    this._OutputResolution = args.OutputResolution;
                    reloadImage = true;
                }
                if (args.CropConstraintChanged)
                {
                    this._CropConstraint = args.CropConstraint;
                    reloadImage = true;
                }
                if (args.PostProcessingFilterChanged)
                {
                    this._PostProcessingFilter = args.PostProcessingFilter;
                    // No need to reload if only the post processing filter has changed
                    // AND - the updatePreview is surely already TRUE
                }
                if (args.PreviewFilterChanged)
                {
                    this._PreviewFilter = args.PreviewFilter;
                    // No need to reload if only the preview filter has changed
                    // AND - the updatePreview is surely already TRUE
                }
                if (args.ReloadImageSet)
                {
                    // Forced to reload the source image
                    reloadImage = true;
                }

                if (reloadImage)
                {
                    // Reload the image
                    if (!args.ReloadImageSet)
                    {
                        // Standard reload, use the current source image size, resolutaion and format to save memory
                        this.popupPictureTrimmer1.SetLoadImageData_ImageSize(this.SourceImageSize);
                        this.popupPictureTrimmer1.SetLoadImageData_ImageResolution(this.SourceImageResolution);
                        this.popupPictureTrimmer1.SetLoadImageData_ImageFormatId(this.SourceImageFormatId);
                    }
                    this.popupPictureTrimmer1.LoadImageFromFileSystem(this.TemporarySourceImageFilePath, this._OutputResolution, this.CropConstraint);
                }
            }

            // Invoke the OpenImageEditPopup after the event, so the eventhandler may change
            // the AutoOpenImageEditPopupAfterUpload property
            if (this.AutoOpenImageEditPopupAfterUpload)
            {
                // Open the image edit popup
                this.OpenImageEditPopup();
            }
        }
    }