/// <summary> /// Returns a trimmed image according to the given parameters if Trimming is true, the original image otherwise. /// </summary> /// <param name="originalImage">The image that need to be trimmed.</param> /// <param name="trimming">Does the image need to be trimmed.</param> /// <param name="isGrayed">Is the image grayed ?</param> /// <param name="trimmingValue">Between 0 and 255, the closer to 0, the more will be trimmed.</param> /// <param name="trimmingMethod">The trimming method to be used.</param> /// <returns>A trimmed image according to the given parameters if Trimming is true, the original image otherwise.</returns> private Bitmap TrimImage(Bitmap originalImage, bool trimming, bool isGrayed, int trimmingValue, TrimmingMethod trimmingMethod) { if (trimming) { using (Bitmap grayedImage = GrayImage(originalImage, !isGrayed)) { int startX = -2; int startY = -2; int endX = -2; int endY = -2; switch (trimmingMethod) { case TrimmingMethod.Absolute: AbsoluteTrimming(grayedImage, trimmingValue, ref startX, ref startY, ref endX, ref endY); break; case TrimmingMethod.Average: AverageTrimming(grayedImage, trimmingValue, ref startX, ref startY, ref endX, ref endY); break; } if (startX < 0) { startX = 0; } if (startY < 0) { startY = 0; } if (endX < 0) { endX = 0; } if (endY < 0) { endY = 0; } if (startX > endX) { startX = endX; } if (startY > endY) { startY = endY; } return(originalImage.Clone(new RectangleF(startX, startY, endX - startX, endY - startY), originalImage.PixelFormat)); } } else { return(originalImage); } }
/// <summary> /// Perform the full treatment on an image, that is graying, trimming and resizing, and returns the result. /// </summary> /// <param name="originalImage">The image that need to be treated.</param> /// <param name="height">The height of the treated image.</param> /// <param name="grayscale">Does the image need to be grayed.</param> /// <param name="trimming">Does the image need to be trimmed.</param> /// <param name="trimmingValue">Between 0 and 255, the closer to 0, the more will be trimmed.</param> /// <param name="leftMargin">Between 0 and 1, the width of the left margin, 0.5 in order to have the left and right margin equals.</param> /// <param name="trimmingMethod">The trimming method to be used.</param> /// <returns>An image grayed, trimmed and resized according to the given parameters.</returns> public Bitmap TreatImage(Bitmap originalImage, int height, bool grayscale, bool trimming, int trimmingValue, double leftMargin, TrimmingMethod trimmingMethod) { int theoreticalWidth = (Int32)Math.Round((double)(height * 0.75), 0, MidpointRounding.AwayFromZero); Bitmap treatedImage = new Bitmap(theoreticalWidth, height); using (Bitmap grayedImage = GrayImage(originalImage, grayscale)) { using (Bitmap trimmedImage = TrimImage(grayedImage, trimming, grayscale, trimmingValue, trimmingMethod)) { using (Graphics g = Graphics.FromImage((System.Drawing.Image)treatedImage)) { int width = (Int32)Math.Round((double)(height * trimmedImage.Width / trimmedImage.Height), 0, MidpointRounding.AwayFromZero); g.FillRectangle(new SolidBrush(Color.White), 0, 0, theoreticalWidth, height); g.DrawImage(trimmedImage, (float)((theoreticalWidth - width) * leftMargin), 0, width, height); } } } return(treatedImage); }