protected void Page_Load(object sender, EventArgs e) { if (this.ScriptManager1.IsInAsyncPostBack) { // After every Ajax postback re-initialize the JQuery UI elements ScriptManager.RegisterClientScriptBlock(this.Page, this.Page.GetType(), "initializeUI", "initializeUI();", true); } if (!this.ScriptManager1.IsInAsyncPostBack) { // Load the fancybox script ExamplesHelper.LoadLibrary_FancyBox(this); } // Reset some settings after every postback... this.MyUpdateProgress1.AssociatedUpdatePanelID = this.UpdatePanel1.UniqueID; // Reset the OnClientControlLoad event hanlder (used to display the output image when the btnProcessImage is clicked this.InlinePictureTrimmer1.OnClientControlLoadFunction = ""; if (!this.IsPostBack) { // Load the image FixedCropConstraint cropConstraint = new FixedCropConstraint(250, 150); cropConstraint.Margins.SetZero(); this.InlinePictureTrimmer1.LoadImageFromFileSystem(SourceImageFileName, cropConstraint); } }
protected void btnLoadImage_Click(object sender, EventArgs e) { if (this.InlinePictureTrimmer1.ImageLoaded) { // Unload the image this.InlinePictureTrimmer1.UnloadImage(); this.btnProcessImage.Enabled = false; this.ddlImageSelectionStrategy.Enabled = true; this.ddlOutputResolution.Enabled = true; this.btnLoadImage.Text = "Load image"; } else { // Load the image float outputResolution = float.Parse(this.ddlOutputResolution.SelectedValue, System.Globalization.CultureInfo.InvariantCulture); CropConstraint cropConstrant = new FixedCropConstraint(GfxUnit.Pixel, 350, 250); cropConstrant.DefaultImageSelectionStrategy = (CropConstraintImageSelectionStrategy)Enum.Parse(typeof(CropConstraintImageSelectionStrategy), this.ddlImageSelectionStrategy.SelectedValue); if (cropConstrant.DefaultImageSelectionStrategy == CropConstraintImageSelectionStrategy.Slice) { cropConstrant.Margins.SetZero(); } this.InlinePictureTrimmer1.AllowResize = cropConstrant.DefaultImageSelectionStrategy != CropConstraintImageSelectionStrategy.DoNotResize; this.InlinePictureTrimmer1.LoadImageFromFileSystem(SourceImageFileName, outputResolution, cropConstrant); this.btnProcessImage.Enabled = true; this.ddlImageSelectionStrategy.Enabled = false; this.ddlOutputResolution.Enabled = false; this.btnLoadImage.Text = "Unload image"; } }
protected void laodImage(string sourceImagePath) { FixedCropConstraint cropConstraint = new FixedCropConstraint(350, 250); cropConstraint.DefaultImageSelectionStrategy = CropConstraintImageSelectionStrategy.DoNotResize; // Load the image in the PictureTrimmer control this.InlinePictureTrimmer1.LoadImageFromFileSystem(sourceImagePath, cropConstraint); // Store the source image file path in the viewstate this.SourceImageFilePath = Server.MapPath(sourceImagePath); }
private CropConstraint GetCropConstraint(int configuration) { FixedCropConstraint result; if (configuration == 0) { // Landscape crop constraint result = new FixedCropConstraint(300, 200); } else { // Portrait crop constraint result = new FixedCropConstraint(200, 300); } return(result); }
protected void Page_Load(object sender, EventArgs e) { if (this.ScriptManager1.IsInAsyncPostBack) { // After every Ajax postback re-initialize the JQuery UI elements ScriptManager.RegisterClientScriptBlock(this.Page, this.Page.GetType(), "initializeUI", "initializeUI(); initializeExampleUI();", true); } if (!this.ScriptManager1.IsInAsyncPostBack) { // Load the custom CSS used by the PopupPictureTrimmer System.Web.UI.HtmlControls.HtmlLink lightBoxCustomCSS = new System.Web.UI.HtmlControls.HtmlLink(); lightBoxCustomCSS.Href = this.ResolveUrl("CodeCarvings.Piczard.LightBox.Custom.css"); lightBoxCustomCSS.Attributes.Add("rel", "stylesheet"); lightBoxCustomCSS.Attributes.Add("type", "text/css"); this.Header.FindControl("phHeader").Controls.Add(lightBoxCustomCSS); // Load the colorpicker script ExamplesHelper.LoadLibrary_ColorPicker(this); } if (!this.IsPostBack) { // Load the image CropConstraint cropConstraint = new FixedCropConstraint(100, 100); cropConstraint.DefaultImageSelectionStrategy = CropConstraintImageSelectionStrategy.DoNotResize; this.InlinePictureTrimmer1.LoadImageFromFileSystem("~/repository/source/flowers2.png", cropConstraint); this.PopupPictureTrimmer1.LoadImageFromFileSystem("~/repository/source/flowers2.png", cropConstraint); } // Configure the PictureTrimmer instances this.SetPictureTrimmerConfiguration(this.InlinePictureTrimmer1); this.SetPictureTrimmerConfiguration(this.PopupPictureTrimmer1); this.PopupPictureTrimmer1.LightBoxCssClass = this.ddlPopupLightBoxCssClass.SelectedValue; this.cbShowResizePanel.Enabled = this.cbAllowResize.Checked; // Reset some settings after every postback... this.MyUpdateProgress1.AssociatedUpdatePanelID = this.UpdatePanel1.UniqueID; }
protected CropConstraint getCropConstraint() { // Get the crop constraint associated with the image orientation currently selected FixedCropConstraint result; if (this.ddlCropOrientation.SelectedIndex == 0) { // Return the landscape crop constraint result = new FixedCropConstraint(250, 150); result.Margins.SetZero(); } else { // Return the portrait crop constraint result = new FixedCropConstraint(150, 250); result.Margins.SetZero(); } return(result); }
private void LoadImageInPiczard(string photoImage) { string imageUrl = Session["ImageUrl"].ToString(); string lastPart = imageUrl.Split('/').Last(); string[] getPath = CommonUtilities.GetPath(); //for normal pictures string galleryPath = getPath[0] + "/" + getPath[1] + "/" + _tributeUrl.Replace(" ", "_") + "_" + _tributeType.Replace(" ", "_"); galleryPath = galleryPath + "/" + lastPart; System.Drawing.Image uploadedImage = System.Drawing.Image.FromFile(galleryPath); float uploadedImageWidth = uploadedImage.PhysicalDimension.Width; float uploadedImageHeight = uploadedImage.PhysicalDimension.Height; float outputResolution = float.Parse(this.ddlOutputResolution.SelectedValue, System.Globalization.CultureInfo.InvariantCulture); CropConstraint cropConstrant = new FixedCropConstraint(GfxUnit.Pixel, uploadedImageWidth, uploadedImageHeight); cropConstrant.DefaultImageSelectionStrategy = (CropConstraintImageSelectionStrategy) Enum.Parse(typeof(CropConstraintImageSelectionStrategy), this.ddlImageSelectionStrategy.SelectedValue); if (cropConstrant.DefaultImageSelectionStrategy == CropConstraintImageSelectionStrategy.Slice) { cropConstrant.Margins.SetZero(); } this.InlinePictureTrimmer1.AllowResize = cropConstrant.DefaultImageSelectionStrategy != CropConstraintImageSelectionStrategy.DoNotResize; this.InlinePictureTrimmer1.LoadImageFromFileSystem(galleryPath, outputResolution, cropConstrant); }
private CropConstraint GetCropConstraint(int configuration) { FixedCropConstraint result; if (configuration == 0) { // Landscape crop constraint result = new FixedCropConstraint(300, 200); } else { // Portrait crop constraint result = new FixedCropConstraint(200, 300); } return result; }
protected override void Apply(ImageProcessingActionExecuteArgs args) { Bitmap result = null; try { // Create the result image result = new Bitmap(350, 350, CodeCarvings.Piczard.CommonData.DefaultPixelFormat); // Set the right image resolution (DPI) ImageHelper.SetImageResolution(result, args.ImageProcessingJob.OutputResolution); using (Graphics g = Graphics.FromImage(result)) { // Use the max quality ImageHelper.SetGraphicsMaxQuality(g); if ((args.IsLastAction) && (!args.AppliedImageBackColorValue.HasValue)) { // Optimization (not mandatory) // This is the last filter action and the ImageBackColor has not been yet applied... // Apply the ImageBackColor now to save RAM & CPU args.ApplyImageBackColor(g); } using (ImageAttributes imageAttributes = new ImageAttributes()) { // Important imageAttributes.SetWrapMode(WrapMode.TileFlipXY); // Draw the scaled image Rectangle destinationRectangle = new Rectangle(75, 52, 200, 200); using (Image resizedImage = new FixedCropConstraint(GfxUnit.Pixel, destinationRectangle.Size).GetProcessedImage(args.Image)) { g.DrawImage(resizedImage, destinationRectangle, 0, 0, resizedImage.Width, resizedImage.Height, GraphicsUnit.Pixel, imageAttributes); // Draw the reflection destinationRectangle = new Rectangle(75, 252, 200, 98); using (Image flippedImage = ImageTransformation.FlipVertical.GetProcessedImage(resizedImage)) { g.DrawImage(flippedImage, destinationRectangle, 0, 0, flippedImage.Width, flippedImage.Height, GraphicsUnit.Pixel, imageAttributes); } } // Draw the mask destinationRectangle = new Rectangle(0, 0, result.Width, result.Height); using (LoadedImage loadedImage = ImageArchiver.LoadImage("~/repository/filters/MyCustomFilter1Mask.png")) { g.DrawImage(loadedImage.Image, destinationRectangle, 0, 0, loadedImage.Size.Width, loadedImage.Size.Height, GraphicsUnit.Pixel, imageAttributes); } } // Draw the text string text = "Generated by 'MyCustomFilter1' on " + DateTime.Now.ToString(); FontDefinition fontDefinition = new FontDefinition(); fontDefinition.Size = 12; //12px using (Font font = fontDefinition.GetFont()) { // Setup the custom parameters g.TextRenderingHint = TextRenderingHint.AntiAliasGridFit; using (StringFormat stringFormat = new StringFormat()) { SizeF textSize = g.MeasureString(text, font, int.MaxValue, stringFormat); Size pixelTextSize = new Size(Convert.ToInt32(Math.Round(textSize.Width)), Convert.ToInt32(Math.Round(textSize.Height))); // Calculate the text position Point location = new Point((result.Width - pixelTextSize.Width) / 2, result.Height - 14 - pixelTextSize.Height); // Draw the text using (Brush brush = new SolidBrush(ColorTranslator.FromHtml("#5b615d"))) { g.DrawString(text, font, brush, location, stringFormat); } } } } // Return the image args.Image = result; } catch { // An error has occurred... // Release the resources if (result != null) { result.Dispose(); result = null; } // Re-throw the exception throw; } }
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 CropConstraint getCropConstraint() { // Get the crop constraint associated with the image orientation currently selected FixedCropConstraint result; if (this.ddlCropOrientation.SelectedIndex == 0) { // Return the landscape crop constraint result = new FixedCropConstraint(250, 150); result.Margins.SetZero(); } else { // Return the portrait crop constraint result = new FixedCropConstraint(150, 250); result.Margins.SetZero(); } return result; }
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; }