Beispiel #1
0
    protected void ProcessImage()
    {
        // Setup the source file name and the output file name
        string sourceImageFileName = this.imgSource.ImageUrl;
        string outputImageFileName = "~/repository/output/Ex_A_102.jpg";

        // Setup a collection and add filters selected by the user
        ImageProcessingFilterCollection filters = new ImageProcessingFilterCollection();

        string[] sortedFilterIDS = this.hfFilterList.Value.Split(',');
        // The filterIDS contains the sorted filters that we have to apply...
        for (int i = 0; i < sortedFilterIDS.Length; i++)
        {
            string filterID = sortedFilterIDS[i];
            switch (filterID)
            {
            case "filterRotate":
                if (this.cbFilterRotate.Checked)
                {
                    // Rotate the image by 90°
                    filters.Add(ImageTransformation.Rotate90);
                }
                break;

            case "filterResize":
                if (this.cbFilterResize.Checked)
                {
                    // Resize the image so that it can be contained within a 280 x 280 square
                    filters.Add(new ScaledResizeConstraint(280, 280));
                }
                break;

            case "filterChangeColors":
                if (this.cbFilterChangeColors.Checked)
                {
                    // Change hue, saturation, brightness and contrast of the image
                    filters.Add(new ImageAdjustmentsFilter(30, 50, 20, -50));
                }
                break;

            case "filterWatermark":
                if (this.cbFilterWatermark.Checked)
                {
                    // Apply an image watermak
                    ImageWatermark watermark = new ImageWatermark("~/repository/watermark/piczardWatermark1.png");
                    watermark.ContentAlignment = System.Drawing.ContentAlignment.BottomRight;
                    filters.Add(watermark);
                }
                break;
            }
        }

        // Process the image
        filters.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();
    }
    protected void ProcessImage()
    {
        // Setup the source file name and the output file name
        string sourceImageFileName = this.imgSource.ImageUrl;
        string outputImageFileName = "~/repository/output/Ex_A_102.jpg";

        // Setup a collection and add filters selected by the user
        ImageProcessingFilterCollection filters = new ImageProcessingFilterCollection();

        string[] sortedFilterIDS = this.hfFilterList.Value.Split(',');
        // The filterIDS contains the sorted filters that we have to apply...
        for (int i = 0; i < sortedFilterIDS.Length; i++)
        {
            string filterID = sortedFilterIDS[i];
            switch (filterID)
            {
                case "filterRotate":
                    if (this.cbFilterRotate.Checked)
                    {
                        // Rotate the image by 90°
                        filters.Add(ImageTransformation.Rotate90);
                    }
                    break;
                case "filterResize":
                    if (this.cbFilterResize.Checked)
                    {
                        // Resize the image so that it can be contained within a 280 x 280 square
                        filters.Add(new ScaledResizeConstraint(280, 280));
                    }
                    break;
                case "filterChangeColors":
                    if (this.cbFilterChangeColors.Checked)
                    {
                        // Change hue, saturation, brightness and contrast of the image
                        filters.Add(new ImageAdjustmentsFilter(30, 50, 20, -50));
                    }
                    break;
                case "filterWatermark":
                    if (this.cbFilterWatermark.Checked)
                    {
                        // Apply an image watermak
                        ImageWatermark watermark = new ImageWatermark("~/repository/watermark/piczardWatermark1.png");
                        watermark.ContentAlignment = System.Drawing.ContentAlignment.BottomRight;
                        filters.Add(watermark);
                    }
                    break;
            }
        }

        // Process the image
        filters.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();
    }
    protected void UpdateDemoParameters()
    {
        // Unload the current image if necessary
        if (this.ImageUpload1.HasImage)
        {
            this.ImageUpload1.UnloadImage();
        }

        this.ddlCropConstraintMode.Enabled = this.cbEnableInteractiveImageProcessing.Checked;
        this.cbAutoOpenImageEditPopupAfterUpload.Enabled = this.cbEnableInteractiveImageProcessing.Checked;

        this.ImageUpload1.EnableEdit            = this.cbEnableInteractiveImageProcessing.Checked;
        this.txtInteractiveImageProcessing.Text = this.ImageUpload1.EnableEdit ? "" : "// Disable interactive image processing features\r\nImageUpload1.EnableEdit = false;\r\n";
        if (this.ImageUpload1.EnableEdit)
        {
            switch (this.ddlCropConstraintMode.SelectedIndex)
            {
            case 0:
                // Crop enabled: Fixed size
                this.ImageUpload1.CropConstraint         = new FixedCropConstraint(300, 200);
                this.txtInteractiveImageProcessing.Text += "// Add a fixed size crop constraint (300 x 200 pixels)\r\n";
                this.txtInteractiveImageProcessing.Text += "ImageUpload1.CropConstraint = new FixedCropConstraint(300, 200);\r\n";
                break;

            case 1:
                // Crop enabled: Fixed aspect ratio
                this.ImageUpload1.CropConstraint         = new FixedAspectRatioCropConstraint(16F / 9F);
                this.txtInteractiveImageProcessing.Text += "// Add a fixed aspect ratio crop constraint (asperct ratio = 16:9)\r\n";
                this.txtInteractiveImageProcessing.Text += "ImageUpload1.CropConstraint = new FixedAspectRatioCropConstraint(16F / 9F);\r\n";
                break;

            case 2:
                // Crop enabled: Free size
                this.ImageUpload1.CropConstraint         = new FreeCropConstraint();
                this.txtInteractiveImageProcessing.Text += "// Add a free crop constraint\r\n";
                this.txtInteractiveImageProcessing.Text += "ImageUpload1.CropConstraint = new FreeCropConstraint();\r\n";
                break;

            case 3:
                // Crop disabled
                this.ImageUpload1.CropConstraint = null;
                break;
            }
        }
        else
        {
            // Reset the CropConstraint
            this.ImageUpload1.CropConstraint = null;
        }

        this.ImageUpload1.AutoOpenImageEditPopupAfterUpload = this.ddlCropConstraintMode.Enabled ? this.cbAutoOpenImageEditPopupAfterUpload.Checked : false;

        this.txtInteractiveImageProcessing.Text   += this.ImageUpload1.AutoOpenImageEditPopupAfterUpload ? "// Automatically open image edit window after upload\r\nImageUpload1.AutoOpenImageEditPopupAfterUpload = true;\r\n" : "";
        this.txtInteractiveImageProcessing.Rows    = this.txtInteractiveImageProcessing.Text.Split('\n').Length - 1;
        this.txtInteractiveImageProcessing.Visible = !string.IsNullOrEmpty(this.txtInteractiveImageProcessing.Text);


        // Create a filter collection to handle multiple filters
        ImageProcessingFilterCollection postProcessing = new ImageProcessingFilterCollection();
        string postProcessingCode = "";

        if (this.cbPostProcessing_Resize.Checked)
        {
            // Add the resize filter
            postProcessing.Add(new ScaledResizeConstraint(200, 200));
            postProcessingCode += "new ScaledResizeConstraint(200, 200)";
        }
        if (this.cbPostProcessing_Grayscale.Checked)
        {
            // Add the graysacale filter
            postProcessing.Add(CodeCarvings.Piczard.Filters.Colors.DefaultColorFilters.Grayscale);
            if (!string.IsNullOrEmpty(postProcessingCode))
            {
                postProcessingCode += " + ";
            }
            postProcessingCode += "DefaultColorFilters.Grayscale";
        }
        if (this.cbPostProcessing_Watermark.Checked)
        {
            // Add the watermark filter
            postProcessing.Add(new CodeCarvings.Piczard.Filters.Watermarks.ImageWatermark("~/repository/watermark/piczardWatermark1.png", System.Drawing.ContentAlignment.BottomRight));
            if (!string.IsNullOrEmpty(postProcessingCode))
            {
                postProcessingCode += " + ";
            }
            postProcessingCode += "new ImageWatermark(\"~/watermark1.png\", ContentAlignment.BottomRight)";
        }
        this.ImageUpload1.PostProcessingFilter = postProcessing;

        if (!string.IsNullOrEmpty(postProcessingCode))
        {
            this.txtPostProcessing.Text = "// Add the post-processing filters\r\nImageUpload1.PostProcessingFilter = " + postProcessingCode + ";\r\n";
        }
        else
        {
            this.txtPostProcessing.Text = "";
        }
        this.txtPostProcessing.Visible = !string.IsNullOrEmpty(this.txtPostProcessing.Text);
        this.txtPostProcessing.Rows    = postProcessing.Count + 2;

        if (this.cbPreviewConstriant.Checked)
        {
            this.ImageUpload1.PreviewFilter = new FixedResizeConstraint(200, 200, Color.Black);
        }
        else
        {
            this.ImageUpload1.PreviewFilter = null;
        }

        this.txtPreviewConstriant.Text    = this.cbPreviewConstriant.Checked ? "// Set a square resize constraint (200 x 200 pixels, bg:black) for the preview\r\nImageUpload1.PreviewFilter = new FixedResizeConstraint(200, 200, Color.Black);\r\n" : "";
        this.txtPreviewConstriant.Visible = !string.IsNullOrEmpty(this.txtPreviewConstriant.Text);

        this.txtValidateImageSize.Visible = this.cbValidateImageSize.Checked;
    }
    protected void UpdateDemoParameters()
    {
        // Unload the current image if necessary
        if (this.ImageUpload1.HasImage)
        {
            this.ImageUpload1.UnloadImage();
        }

        this.ddlCropConstraintMode.Enabled = this.cbEnableInteractiveImageProcessing.Checked;
        this.cbAutoOpenImageEditPopupAfterUpload.Enabled = this.cbEnableInteractiveImageProcessing.Checked;

        this.ImageUpload1.EnableEdit = this.cbEnableInteractiveImageProcessing.Checked;
        this.txtInteractiveImageProcessing.Text = this.ImageUpload1.EnableEdit ? "" : "// Disable interactive image processing features\r\nImageUpload1.EnableEdit = false;\r\n";
        if (this.ImageUpload1.EnableEdit)
        {
            switch (this.ddlCropConstraintMode.SelectedIndex)
            {
                case 0:
                    // Crop enabled: Fixed size
                    this.ImageUpload1.CropConstraint = new FixedCropConstraint(300, 200);
                    this.txtInteractiveImageProcessing.Text += "// Add a fixed size crop constraint (300 x 200 pixels)\r\n";
                    this.txtInteractiveImageProcessing.Text += "ImageUpload1.CropConstraint = new FixedCropConstraint(300, 200);\r\n";
                    break;
                case 1:
                    // Crop enabled: Fixed aspect ratio
                    this.ImageUpload1.CropConstraint = new FixedAspectRatioCropConstraint(16F / 9F);
                    this.txtInteractiveImageProcessing.Text += "// Add a fixed aspect ratio crop constraint (asperct ratio = 16:9)\r\n";
                    this.txtInteractiveImageProcessing.Text += "ImageUpload1.CropConstraint = new FixedAspectRatioCropConstraint(16F / 9F);\r\n";
                    break;
                case 2:
                    // Crop enabled: Free size
                    this.ImageUpload1.CropConstraint = new FreeCropConstraint();
                    this.txtInteractiveImageProcessing.Text += "// Add a free crop constraint\r\n";
                    this.txtInteractiveImageProcessing.Text += "ImageUpload1.CropConstraint = new FreeCropConstraint();\r\n";
                    break;
                case 3:
                    // Crop disabled
                    this.ImageUpload1.CropConstraint = null;
                    break;
            }
        }
        else
        {
            // Reset the CropConstraint
            this.ImageUpload1.CropConstraint = null;
        }

        this.ImageUpload1.AutoOpenImageEditPopupAfterUpload = this.ddlCropConstraintMode.Enabled ? this.cbAutoOpenImageEditPopupAfterUpload.Checked : false;

        this.txtInteractiveImageProcessing.Text += this.ImageUpload1.AutoOpenImageEditPopupAfterUpload ? "// Automatically open image edit window after upload\r\nImageUpload1.AutoOpenImageEditPopupAfterUpload = true;\r\n" : "";
        this.txtInteractiveImageProcessing.Rows = this.txtInteractiveImageProcessing.Text.Split('\n').Length - 1;
        this.txtInteractiveImageProcessing.Visible = !string.IsNullOrEmpty(this.txtInteractiveImageProcessing.Text);
        

        // Create a filter collection to handle multiple filters
        ImageProcessingFilterCollection postProcessing = new ImageProcessingFilterCollection();
        string postProcessingCode = "";
        if (this.cbPostProcessing_Resize.Checked)
        {
            // Add the resize filter
            postProcessing.Add(new ScaledResizeConstraint(200, 200));
            postProcessingCode += "new ScaledResizeConstraint(200, 200)";
        }
        if (this.cbPostProcessing_Grayscale.Checked)
        {
            // Add the graysacale filter
            postProcessing.Add(CodeCarvings.Piczard.Filters.Colors.DefaultColorFilters.Grayscale);
            if (!string.IsNullOrEmpty(postProcessingCode))
            {
                postProcessingCode += " + ";
            }
            postProcessingCode += "DefaultColorFilters.Grayscale";
        }
        if (this.cbPostProcessing_Watermark.Checked)
        {
            // Add the watermark filter
            postProcessing.Add(new CodeCarvings.Piczard.Filters.Watermarks.ImageWatermark("~/repository/watermark/piczardWatermark1.png", System.Drawing.ContentAlignment.BottomRight));
            if (!string.IsNullOrEmpty(postProcessingCode))
            {
                postProcessingCode += " + ";
            }
            postProcessingCode += "new ImageWatermark(\"~/watermark1.png\", ContentAlignment.BottomRight)";
        }
        this.ImageUpload1.PostProcessingFilter = postProcessing; 

        if (!string.IsNullOrEmpty(postProcessingCode))
        {
            this.txtPostProcessing.Text = "// Add the post-processing filters\r\nImageUpload1.PostProcessingFilter = " + postProcessingCode + ";\r\n";
        }
        else
        {
            this.txtPostProcessing.Text = "";
        }        
        this.txtPostProcessing.Visible = !string.IsNullOrEmpty(this.txtPostProcessing.Text);
        this.txtPostProcessing.Rows = postProcessing.Count + 2;

        if (this.cbPreviewConstriant.Checked)
        {
            this.ImageUpload1.PreviewFilter = new FixedResizeConstraint(200, 200, Color.Black);
        }
        else
        {
            this.ImageUpload1.PreviewFilter = null;
        }

        this.txtPreviewConstriant.Text = this.cbPreviewConstriant.Checked ? "// Set a square resize constraint (200 x 200 pixels, bg:black) for the preview\r\nImageUpload1.PreviewFilter = new FixedResizeConstraint(200, 200, Color.Black);\r\n" : "";
        this.txtPreviewConstriant.Visible = !string.IsNullOrEmpty(this.txtPreviewConstriant.Text);

        this.txtValidateImageSize.Visible = this.cbValidateImageSize.Checked;
    }