示例#1
0
    protected void ddlCropOrientation_SelectedIndexChanged(object sender, EventArgs e)
    {
        // Image orientation changed...

        // Re-Load the image wit the new Crop constraint
        CropConstraint cropConstraint = this.getCropConstraint();

        this.InlinePictureTrimmer1.LoadImageFromFileSystem(this.lastLoadImagePath, cropConstraint);
    }
示例#2
0
    protected void loadImage(string sourceImagePath)
    {
        // Store the source image path (it's used in the ddlCropOrientation_SelectedIndexChanged event handler)
        this.lastLoadImagePath = sourceImagePath;

        // Get the source iamge size
        System.Drawing.Size sourceImageSize;
        using (LoadedImage image = ImageArchiver.LoadImage(sourceImagePath))
        {
            sourceImageSize = image.Size;
        }

        // Auto detect the image orientation
        if (sourceImageSize.Width > sourceImageSize.Height)
        {
            // Is a landscape image
            this.ddlCropOrientation.SelectedIndex = 0;
        }
        else
        {
            // Is a portrait image
            this.ddlCropOrientation.SelectedIndex = 1;
        }

        // Load the image
        CropConstraint cropConstraint = this.getCropConstraint();

        this.InlinePictureTrimmer1.LoadImageFromFileSystem(sourceImagePath, cropConstraint);

        if (!this.ddlCropOrientation.Enabled)
        {
            // This is the first time an image is loaded...
            // Refresh some UI elements

            // Enable the drop down  list control
            this.ddlCropOrientation.Enabled = true;
            // Enable the "Preview" button
            this.btnProcessImage.Enabled = true;
        }
    }
 /// <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)
 {
 }
 /// <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 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 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);
            }
        }
    }
    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();
            }
        }
    }
    protected void btnLoadImage_Click(object sender, EventArgs e)
    {
        // Update the Output resolution and the UI Unit
        GfxUnit unit = (GfxUnit)Enum.Parse(typeof(GfxUnit), this.ddlUnit.SelectedValue);

        this.InlinePictureTrimmer1.UIUnit = unit;

        float outputResolution = float.Parse(this.ddlDPI.SelectedValue, CultureInfo.InvariantCulture);

        CropConstraint cropConstrant = null;

        switch (this.ddlCropMode.SelectedValue)
        {
        case "Fixed":
            float fixedWidth  = float.Parse(this.ddlConstraint_Fixed_Width.SelectedValue, CultureInfo.InvariantCulture);
            float fixedHeight = float.Parse(this.ddlConstraint_Fixed_Height.SelectedValue, CultureInfo.InvariantCulture);
            FixedCropConstraint fixedCropConstraint = new FixedCropConstraint(unit, fixedWidth, fixedHeight);
            cropConstrant = fixedCropConstraint;
            break;

        case "FixedAspectRatio":
            float aspectRatioX = float.Parse(this.ddlConstraint_FixedAspectRatio_X.SelectedValue, CultureInfo.InvariantCulture);
            float aspectRatioY = float.Parse(this.ddlConstraint_FixedAspectRatio_Y.SelectedValue, CultureInfo.InvariantCulture);
            float aspectRatio  = aspectRatioX / aspectRatioY;
            FixedAspectRatioCropConstraint fixedAspectRatioCropConstraint = new FixedAspectRatioCropConstraint(aspectRatio);
            fixedAspectRatioCropConstraint.Unit = unit;

            if ((this.cb_FixedAspectRatio_Min.Checked) || (this.cb_FixedAspectRatio_Max.Checked))
            {
                float minValue = float.Parse(this.ddlConstraint_FixedAspectRatio_Min.SelectedValue, CultureInfo.InvariantCulture);
                float maxValue = float.Parse(this.ddlConstraint_FixedAspectRatio_Max.SelectedValue, CultureInfo.InvariantCulture);

                // Ensure that Min value is not greater than Max value
                if ((this.cb_FixedAspectRatio_Min.Checked) && (this.cb_FixedAspectRatio_Max.Checked))
                {
                    if (maxValue < minValue)
                    {
                        // ERROR
                        ScriptManager.RegisterClientScriptBlock(this.Page, this.Page.GetType(), "FixedAspectRatioCropConstraintError1", "alert(\"Error: Min value cannot be greater than Max value.\");", true);
                        return;
                    }
                }

                fixedAspectRatioCropConstraint.LimitedDimension = (SizeDimension)Enum.Parse(typeof(SizeDimension), this.ddlConstraint_FixedAspectRatio_LimitedDimension.SelectedValue);
                if (this.cb_FixedAspectRatio_Min.Checked)
                {
                    fixedAspectRatioCropConstraint.Min = minValue;
                }
                if (this.cb_FixedAspectRatio_Max.Checked)
                {
                    fixedAspectRatioCropConstraint.Max = maxValue;
                }
            }

            cropConstrant = fixedAspectRatioCropConstraint;
            break;

        case "Free":
            FreeCropConstraint freeCropConstraint = new FreeCropConstraint();
            freeCropConstraint.Unit = unit;

            if ((this.cb_Free_Width_Min.Checked) || (this.cb_Free_Width_Max.Checked))
            {
                float minWidth = float.Parse(this.ddlConstraint_Free_Width_Min.SelectedValue, CultureInfo.InvariantCulture);
                float maxWidth = float.Parse(this.ddlConstraint_Free_Width_Max.SelectedValue, CultureInfo.InvariantCulture);

                // Ensure that Min width is not greater than Max width
                if ((this.cb_Free_Width_Min.Checked) && (this.cb_Free_Width_Max.Checked))
                {
                    if (maxWidth < minWidth)
                    {
                        // ERROR
                        ScriptManager.RegisterClientScriptBlock(this.Page, this.Page.GetType(), "FreeCropConstraintError1", "alert(\"Error: Min width cannot be greater than Max width.\");", true);
                        return;
                    }
                }

                if (this.cb_Free_Width_Min.Checked)
                {
                    freeCropConstraint.MinWidth = minWidth;
                }
                if (this.cb_Free_Width_Max.Checked)
                {
                    freeCropConstraint.MaxWidth = maxWidth;
                }
            }

            if ((this.cb_Free_Height_Min.Checked) || (this.cb_Free_Height_Max.Checked))
            {
                float minHeight = float.Parse(this.ddlConstraint_Free_Height_Min.SelectedValue, CultureInfo.InvariantCulture);
                float maxHeight = float.Parse(this.ddlConstraint_Free_Height_Max.SelectedValue, CultureInfo.InvariantCulture);

                // Ensure that Min height is not greater than Max height
                if ((this.cb_Free_Height_Min.Checked) && (this.cb_Free_Height_Max.Checked))
                {
                    if (maxHeight < minHeight)
                    {
                        // ERROR
                        ScriptManager.RegisterClientScriptBlock(this.Page, this.Page.GetType(), "FreeCropConstraintError2", "alert(\"Error: Min height cannot be greater than Max height.\");", true);
                        return;
                    }
                }

                if (this.cb_Free_Height_Min.Checked)
                {
                    freeCropConstraint.MinHeight = minHeight;
                }
                if (this.cb_Free_Height_Max.Checked)
                {
                    freeCropConstraint.MaxHeight = maxHeight;
                }
            }

            cropConstrant = freeCropConstraint;
            break;
        }

        // Setup the margins
        if (this.ddlMarginsH.SelectedValue == "")
        {
            // Horizontal margin = automatic
            cropConstrant.Margins.Horizontal = null;
        }
        else
        {
            // Hortizontal margin - custom value
            cropConstrant.Margins.Horizontal = float.Parse(this.ddlMarginsH.SelectedValue, CultureInfo.InvariantCulture);
        }
        if (this.ddlMarginsV.SelectedValue == "")
        {
            // Horizontal margin = automatic
            cropConstrant.Margins.Vertical = null;
        }
        else
        {
            // Hortizontal margin - custom value
            cropConstrant.Margins.Vertical = float.Parse(this.ddlMarginsV.SelectedValue, CultureInfo.InvariantCulture);
        }

        // Setup the DefaultImageSelectionStrategy
        cropConstrant.DefaultImageSelectionStrategy = (CropConstraintImageSelectionStrategy)Enum.Parse(typeof(CropConstraintImageSelectionStrategy), this.ddlImageSelectionStrategy.SelectedValue);

        // Load the image
        this.InlinePictureTrimmer1.LoadImageFromFileSystem("~/repository/source/donkey1.jpg", outputResolution, cropConstrant);

        this.InlinePictureTrimmer1.Visible = true;
        this.phBeforeLoad.Visible          = false;
        this.phAfterLoad.Visible           = true;

        this.DisplayCode();
    }
    protected void ProcessImage()
    {
        // Setup the source file name and the output file name
        string sourceImageFileName = this.imgSource.ImageUrl;
        string outputImageFileName = "~/repository/output/Ex_A_212.jpg";

        // Setup the crop filter
        CropConstraint cropFilter = null;
        switch (this.ddlCropMode.SelectedValue)
        {
            case "Fixed":
                FixedCropConstraint fixedCropConstraint = null;
                switch (this.ddlConstraints_Fixed.SelectedIndex)
                {
                    case 0:
                        // 180 x 100 Pixel
                        fixedCropConstraint = new FixedCropConstraint(180, 100);
                        break;
                    case 1:
                        // 50 x 100 Mm (96 DPI)
                        fixedCropConstraint = new FixedCropConstraint(GfxUnit.Mm, 50F, 100F);
                        break;
                }
                cropFilter = fixedCropConstraint;
                break;
            case "FixedAspectRatio":
                FixedAspectRatioCropConstraint fixedAspectRatioCropFilter = null;
                switch (this.ddlAspectRatio.SelectedIndex)
                {
                    case 0:
                        fixedAspectRatioCropFilter = new FixedAspectRatioCropConstraint(1F / 1F);
                        break;
                    case 1:
                        fixedAspectRatioCropFilter = new FixedAspectRatioCropConstraint(2F / 1F);
                        break;
                    case 2:
                        fixedAspectRatioCropFilter = new FixedAspectRatioCropConstraint(1F / 2F);
                        break;
                    case 3:
                        fixedAspectRatioCropFilter = new FixedAspectRatioCropConstraint(16F / 9F);
                        break;
                }
                switch (this.ddlConstraints_FixedAspectRatio.SelectedIndex)
                {
                    case 0:
                        // No constraint
                        break;
                    case 1:
                        // Min width: 500px
                        fixedAspectRatioCropFilter.LimitedDimension = SizeDimension.Width;
                        fixedAspectRatioCropFilter.Min = 500;
                        break;
                    case 2:
                        // Min height: 5.2 inch (96 DPI)
                        fixedAspectRatioCropFilter.Unit = GfxUnit.Inch;
                        fixedAspectRatioCropFilter.LimitedDimension = SizeDimension.Height;
                        fixedAspectRatioCropFilter.Min = 5.2F;
                        break;
                    case 3:
                        // Max width: 200px
                        fixedAspectRatioCropFilter.LimitedDimension = SizeDimension.Width;
                        fixedAspectRatioCropFilter.Max = 200;
                        break;
                    case 4:
                        // Max height: 5 cm (96 DPI)
                        fixedAspectRatioCropFilter.Unit = GfxUnit.Cm;
                        fixedAspectRatioCropFilter.LimitedDimension = SizeDimension.Height;
                        fixedAspectRatioCropFilter.Max = 5F;
                        break;
                }
                cropFilter = fixedAspectRatioCropFilter;
                break;
            case "Free":
                FreeCropConstraint freeCropFilter = null;
                switch (this.ddlConstraints_Free.SelectedIndex)
                {
                    case 0:
                        // No constraints
                        freeCropFilter = new FreeCropConstraint();
                        break;
                    case 1:
                        // Max width: 200px
                        freeCropFilter = new FreeCropConstraint(null, 200, null, null);
                        break;
                    case 2:
                        // Max height: 5 cm (96 DPI)
                        freeCropFilter = new FreeCropConstraint(GfxUnit.Cm, null, null, null, 5F);
                        break;
                    case 3:
                        // Fixed width: 4.1 inch (96 DPI)
                        freeCropFilter = new FreeCropConstraint(GfxUnit.Inch, 4.1F, 4.1F, null, null);
                        break;
                    case 4:
                        // Fixed height: 400px
                        freeCropFilter = new FreeCropConstraint(null, null, 400, 400);
                        break;
                }
                cropFilter = freeCropFilter;
                break;
        }

        cropFilter.DefaultImageSelectionStrategy = (CropConstraintImageSelectionStrategy)Enum.Parse(typeof(CropConstraintImageSelectionStrategy), this.ddlImageSelectionStrategy.SelectedValue);
        if (this.ddlMargins.SelectedValue != "Auto")
        {
            // Disabled both the margins (horizontal and vertical).
            // Note: By default the margins are automatically calculated
            cropFilter.Margins.SetZero();
        }

        // Set the canvas color
        cropFilter.CanvasColor = System.Drawing.ColorTranslator.FromHtml(this.txtCanvasColor.Text);
        
        // Process the image
        cropFilter.SaveProcessedImageToFileSystem(sourceImageFileName, outputImageFileName);

        // Update the displayed image (add a timestamp parameter to the query URL to ensure that the image is reloaded by the browser)
        this.imgOutput.ImageUrl = outputImageFileName + "?timestamp=" + DateTime.UtcNow.Ticks.ToString();

        // Display the generated image
        this.phOutputContainer.Visible = true;
    }