internal static Point GetSourceOrigin(Size imgSize, Size targetSize, TargetSpot targetSpot) { float x, y = 0; switch (targetSpot) { case TargetSpot.Center: x = (imgSize.Width - targetSize.Width) / 2; y = (imgSize.Height - targetSize.Height) / 2; break; case TargetSpot.TopMiddle: x = (imgSize.Width - targetSize.Width) / 2; y = 0; break; case TargetSpot.BottomMiddle: x = (imgSize.Width - targetSize.Width) / 2; y = imgSize.Height - targetSize.Height; break; case TargetSpot.MiddleRight: x = imgSize.Width - targetSize.Width; y = (imgSize.Height - targetSize.Height) / 2; break; case TargetSpot.BottomRight: x = imgSize.Width - targetSize.Width; y = imgSize.Height - targetSize.Height; break; case TargetSpot.TopRight: x = imgSize.Width - targetSize.Width; y = 0; break; case TargetSpot.MiddleLeft: x = 0; y = (imgSize.Height - targetSize.Height) / 2; break; case TargetSpot.BottomLeft: x = 0; y = imgSize.Height - targetSize.Height * 1; break; case TargetSpot.TopLeft: default: x = 0; y = 0; break; } return(new Point((int)x, (int)y)); }
/// <summary> /// add image watermark over the uploaded image /// </summary> /// <param name="img"></param> /// <param name="wmFileName"></param> /// <param name="spot"></param> /// <param name="stickToBorder"></param> public static void ImageWatermark(this Image img, string wmFileName, TargetSpot spot = TargetSpot.TopRight, int margin = 10, int opacity = 35) { if (opacity > 0) { var graphics = Graphics.FromImage(img); graphics.SmoothingMode = SmoothingMode.None; graphics.CompositingMode = CompositingMode.SourceOver; var wmImage = Image.FromFile(wmFileName); if (opacity < 100) { wmImage = ImageOpacity.ChangeImageOpacity(wmImage, opacity); } var wmW = wmImage.Width; var wmH = wmImage.Height; var drawingPoint = ImageWatermarkPos(img.Width, img.Height, wmW, wmH, spot, margin); graphics.DrawImage(wmImage, drawingPoint.X, drawingPoint.Y, wmW, wmH); graphics.Dispose(); } }
/// <summary> /// watermark text pos /// </summary> /// <param name="imgWidth"></param> /// <param name="imgHeight"></param> /// <param name="fontSize"></param> /// <param name="pos"></param> /// <returns></returns> private static Rectangle SetBGPos(int imgWidth, int imgHeight, int fontSize, TargetSpot spot, int margin) { Rectangle rect; var bgHeight = fontSize * 2; switch (spot) { case TargetSpot.TopLeft: case TargetSpot.TopMiddle: case TargetSpot.TopRight: rect = new Rectangle(0, margin, imgWidth, bgHeight); break; case TargetSpot.MiddleLeft: case TargetSpot.MiddleRight: case TargetSpot.Center: rect = new Rectangle(0, imgHeight / 2 - bgHeight / 2, imgWidth, bgHeight); break; case TargetSpot.BottomLeft: case TargetSpot.BottomMiddle: case TargetSpot.BottomRight: default: rect = new Rectangle(0, imgHeight - bgHeight - margin, imgWidth, bgHeight); break; } return(rect); }
/// <summary> /// Crop an image according to the specified values /// </summary> /// <param name="imgSize">The source image size</param> /// <param name="targetSize">The target image size</param> /// <param name="targetSpot">The pre-defined spot of the source image to read and crop. /// <see cref="TargetSpot"/></param> public Crop(Size imgSize, Size targetSize, TargetSpot targetSpot) { TargetSize = targetSize; SourceSize = targetSize; SourceOrigin = ResizeMethods.SourceOrigin.GetSourceOrigin(imgSize, targetSize, targetSpot); TargetOrigin = new Point(0, 0); }
public Crop(Size imgSize, Size targetSize, TargetSpot targetSpot) { _targetSize = targetSize; _sourceSize = targetSize; _sourceOrigin = SourceOrigin.GetSourceOrigin(imgSize, targetSize, targetSpot); _targetOrigin = new Point(0, 0); }
/// <summary> /// add watermark text over image /// </summary> /// <param name="img"></param> /// <param name="text"></param> /// <param name="color">hex8 Color e.g. #77FFFFFF</param> /// <param name="bgColor">hex8 Color e.g. #00000000</param> /// <param name="fontFamily"></param> /// <param name="size"></param> /// <param name="align"></param> /// <param name="vAlign"></param> /// <param name="style"></param> public static void TextWatermark(this Image img, string text, string color = "#77FFFFFF", string bgColor = "#00000000", string fontFamily = "Arial", int size = 24, TargetSpot spot = TargetSpot.BottomLeft, FontStyle style = FontStyle.Regular, int margin = 10) { var graphics = Graphics.FromImage(img); graphics.TextRenderingHint = TextRenderingHint.AntiAlias; graphics.TextContrast = 12; graphics.SmoothingMode = SmoothingMode.None; graphics.CompositingMode = CompositingMode.SourceOver; var _bgAlpha = int.Parse(bgColor.Substring(1, 2), NumberStyles.HexNumber); var _bgColor = bgColor.Substring(3, 6); var bgBrush = new SolidBrush( Color.FromArgb(_bgAlpha, ColorTranslator.FromHtml($"#{_bgColor}"))); var rectPos = SetBGPos(img.Width, img.Height, size, spot, margin); graphics.FillRectangle(bgBrush, rectPos); var textFont = new Font(fontFamily, size, style, GraphicsUnit.Pixel); var _alpha = int.Parse(color.Substring(1, 2), NumberStyles.HexNumber); var _color = color.Substring(3, 6); var textBrush = new SolidBrush( Color.FromArgb(_alpha, ColorTranslator.FromHtml($"#{_color}"))); var textMetrics = graphics.MeasureString(text, textFont); var beforeText = SetTextAlign(textMetrics, img.Width, spot); var drawPoint = new PointF(beforeText, rectPos.Y + (rectPos.Height / 4)); graphics.DrawString(text, textFont, textBrush, drawPoint); graphics.Dispose(); }
internal static int SetTextAlign(SizeF textMetrics, int imgWidth, TargetSpot spot) { int space; switch (spot) { case TargetSpot.BottomMiddle: case TargetSpot.TopMiddle: case TargetSpot.Center: space = (int)(imgWidth - textMetrics.Width) / 2; break; case TargetSpot.BottomRight: case TargetSpot.MiddleRight: case TargetSpot.TopRight: space = (int)(imgWidth - textMetrics.Width) - 5; break; case TargetSpot.BottomLeft: case TargetSpot.MiddleLeft: case TargetSpot.TopLeft: default: space = 5; break; } return(space); }
private static PointF ImageWatermarkPos(int imgWidth, int imgHeight, int wmWidth, int wmHeight, TargetSpot spot, int margin) { PointF point; switch (spot) { case TargetSpot.BottomLeft: point = new PointF(margin, imgHeight - (wmHeight + margin)); break; case TargetSpot.BottomMiddle: point = new PointF((imgWidth / 2) - (wmWidth / 2), imgHeight - (wmHeight + margin)); break; case TargetSpot.BottomRight: point = new PointF(imgWidth - (wmWidth + margin), imgHeight - (wmHeight + margin)); break; case TargetSpot.MiddleLeft: point = new PointF(margin, (imgHeight / 2) - (wmHeight / 2)); break; case TargetSpot.Center: point = new PointF((imgWidth / 2) - (wmWidth / 2), (imgHeight / 2) - (wmHeight / 2)); break; case TargetSpot.MiddleRight: point = new PointF(imgWidth - (wmWidth + margin), (imgHeight / 2) - (wmHeight / 2)); break; case TargetSpot.TopLeft: point = new PointF(margin, margin); break; case TargetSpot.TopMiddle: point = new PointF((imgWidth / 2) - (wmWidth / 2), margin); break; case TargetSpot.TopRight: default: point = new PointF(imgWidth - (margin + wmWidth), margin); break; } return(point); }
/// <summary> /// Directly crop original image without scaling it. /// Final image aspect ratio is equal to given new width/height /// </summary> /// <param name="img"></param> /// <param name="newWidth"></param> /// <param name="newHeight"></param> /// <param name="spot">target spot to crop and save</param> /// <param name="ops">Graphic options <see cref="GraphicOptions"/></param> /// <returns></returns> public static Image Crop(this Image img, int newWidth, int newHeight, GraphicOptions ops, TargetSpot spot = TargetSpot.Center) { var resize = new Crop(img.Size, new Size(newWidth, newHeight), spot); return(Resize(img, resize.SourceRect, resize.TargetRect, ops)); }
/// <summary> /// Scale target image till shortest border are equal to target value, /// then crop the additonal pixels from the longest border. /// Final image aspect ratio is equal to the given new width/height /// </summary> /// <param name="img"></param> /// <param name="newWidth"></param> /// <param name="newHeight"></param> /// <param name="spot"></param> /// <returns></returns> public static Image ScaleAndCrop(this Image img, int newWidth, int newHeight, TargetSpot spot = TargetSpot.Center) { var resize = new ScaleAndCrop(img.Size, new Size(newWidth, newHeight), spot); return(Resize(img, resize.SourceRect, resize.TargetRect)); }