protected override void Apply(ImageProcessingActionExecuteArgs args) { float normalizedRotationAngle = this._RotationAngle % 360F; if (normalizedRotationAngle == 0F) { // No need to rotate the image return; } Bitmap result = null; try { // Intial calculations double t; double t1; double b1; double t2; double b2; this.CalculateValues(args.Image.Size, out t, out t1, out b1, out t2, out b2); // Calculate the new image size Size outputImageSize = this.GetOutputImageSize(t1, b1, t2, b2); // Create the result image result = new Bitmap(outputImageSize.Width, outputImageSize.Height, 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); } // Calculate the points for the DrawImage method Point[] shapePoints = new Point[3]; if ((t >= 0D) && (t < (Math.PI / 2D))) { shapePoints[0] = new Point(Convert.ToInt32(Math.Round(b2)), 0); shapePoints[1] = new Point(outputImageSize.Width, Convert.ToInt32(Math.Round(t2))); shapePoints[2] = new Point(0, Convert.ToInt32(Math.Round(b1))); } else if ((t >= (Math.PI / 2D)) && (t < Math.PI)) { shapePoints[0] = new Point(outputImageSize.Width, Convert.ToInt32(Math.Round(t2))); shapePoints[1] = new Point(Convert.ToInt32(Math.Round(t1)), outputImageSize.Height); shapePoints[2] = new Point(Convert.ToInt32(Math.Round(b2)), 0); } else if ((t >= Math.PI) && (t < (Math.PI + (Math.PI / 2D)))) { shapePoints[0] = new Point(Convert.ToInt32(Math.Round(t1)), outputImageSize.Height); shapePoints[1] = new Point(0, Convert.ToInt32(Math.Round(b1))); shapePoints[2] = new Point(outputImageSize.Width, Convert.ToInt32(Math.Round(t2))); } else { shapePoints[0] = new Point(0, Convert.ToInt32(Math.Round(b1))); shapePoints[1] = new Point(Convert.ToInt32(Math.Round(b2)), 0); shapePoints[2] = new Point(Convert.ToInt32(Math.Round(t1)), outputImageSize.Height); } // Draw the image g.DrawImage(args.Image, shapePoints); } // 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 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; } }