/// <summary> /// 图片水印 /// </summary> /// <param name="destBitmap"></param> /// <param name="watermarkBitmap"></param> /// <param name="currentSettings"></param> private void PlaceImageWatermark(Bitmap destBitmap, Bitmap watermarkBitmap, WatermarkSettings currentSettings) { currentSettings = currentSettings ?? new WatermarkSettings(); using (Graphics g = Graphics.FromImage(destBitmap)) { Bitmap rotateImage = null; if (currentSettings.ImageRotatedDegree > 0) { rotateImage = GetRotateImage(watermarkBitmap, currentSettings.ImageRotatedDegree); } if (rotateImage != null) { watermarkBitmap = rotateImage; } double watermarkSizeInPercent = (double)currentSettings.PictureSettings.Size / 100; Size boundingBoxSize = new Size((int)(destBitmap.Width * watermarkSizeInPercent), (int)(destBitmap.Height * watermarkSizeInPercent)); Size calculatedWatermarkSize = ScaleRectangleToFitBounds(boundingBoxSize, watermarkBitmap.Size); if (calculatedWatermarkSize.Width == 0 || calculatedWatermarkSize.Height == 0) { return; } Bitmap scaledWatermarkBitmap = new Bitmap(calculatedWatermarkSize.Width, calculatedWatermarkSize.Height); using (Graphics watermarkGraphics = Graphics.FromImage(scaledWatermarkBitmap)) { ColorMatrix opacityMatrix = new ColorMatrix { Matrix33 = (float)currentSettings.PictureSettings.Opacity }; ImageAttributes attrs = new ImageAttributes(); attrs.SetColorMatrix(opacityMatrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap); watermarkGraphics.DrawImage(watermarkBitmap, new Rectangle(0, 0, scaledWatermarkBitmap.Width, scaledWatermarkBitmap.Height), 0, 0, watermarkBitmap.Width, watermarkBitmap.Height, GraphicsUnit.Pixel, attrs); attrs.Dispose(); } foreach (var position in currentSettings.PictureSettings.PositionList) { Point watermarkPosition = CalculateWatermarkPosition(position, destBitmap.Size, calculatedWatermarkSize); g.DrawImage(scaledWatermarkBitmap, new Rectangle(watermarkPosition, calculatedWatermarkSize), 0, 0, calculatedWatermarkSize.Width, calculatedWatermarkSize.Height, GraphicsUnit.Pixel); } scaledWatermarkBitmap.Dispose(); } }
/// <summary> /// 文字水印 /// </summary> /// <param name="sourceBitmap"></param> /// <param name="currentSettings"></param> private void PlaceTextWatermark(Bitmap sourceBitmap, WatermarkSettings currentSettings) { using (Graphics g = Graphics.FromImage(sourceBitmap)) { g.SmoothingMode = SmoothingMode.HighQuality; g.CompositingQuality = CompositingQuality.HighQuality; g.InterpolationMode = InterpolationMode.Default; g.SmoothingMode = SmoothingMode.AntiAlias; g.TextRenderingHint = TextRenderingHint.AntiAlias; string text = currentSettings.WatermarkText; int textAngle = currentSettings.TextRotatedDegree; double sizeFactor = (double)currentSettings.TextSettings.Size / 100; Size maxTextSize = new Size( (int)(sourceBitmap.Width * sizeFactor), (int)(sourceBitmap.Height * sizeFactor)); int fontSize = ComputeMaxFontSize(text, textAngle, currentSettings.WatermarkFont, maxTextSize, g); Font font = new Font(currentSettings.WatermarkFont, (float)fontSize, FontStyle.Bold); SizeF originalTextSize = g.MeasureString(text, font); SizeF rotatedTextSize = CalculateRotatedRectSize(originalTextSize, textAngle); Bitmap textBitmap = new Bitmap((int)rotatedTextSize.Width, (int)rotatedTextSize.Height, PixelFormat.Format32bppArgb); using (Graphics textG = Graphics.FromImage(textBitmap)) { Color color = Color.FromArgb((int)(currentSettings.TextSettings.Opacity * 255), currentSettings.TextColor); SolidBrush brush = new SolidBrush(color); textG.TranslateTransform(rotatedTextSize.Width / 2, rotatedTextSize.Height / 2); textG.RotateTransform((float)textAngle); textG.DrawString(text, font, brush, -originalTextSize.Width / 2, -originalTextSize.Height / 2); textG.ResetTransform(); brush.Dispose(); } foreach (var position in currentSettings.TextSettings.PositionList) { Point textPosition = CalculateWatermarkPosition(position, sourceBitmap.Size, rotatedTextSize.ToSize()); g.DrawImage(textBitmap, textPosition); } textBitmap.Dispose(); font.Dispose(); } }
public Image MakeImageWatermark(Image sourceImage, WatermarkSettings currentSettings) { if ((sourceImage.Height > currentSettings.MinimumImageHeightForWatermark) || (sourceImage.Width > currentSettings.MinimumImageWidthForWatermark)) { Bitmap destBitmap = CreateBitmap(sourceImage); if (currentSettings.WatermarkTextEnable && !String.IsNullOrEmpty(currentSettings.WatermarkText)) { PlaceTextWatermark(destBitmap, currentSettings); } if (currentSettings.WatermarkPictureEnable && watermarkBitmap.Value != null) { PlaceImageWatermark(destBitmap, watermarkBitmap.Value, currentSettings); } sourceImage?.Dispose(); return(destBitmap); } return(sourceImage); }