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; }
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(); }