225 lines
6.9 KiB
C#
225 lines
6.9 KiB
C#
using System;
|
|
using System.Drawing;
|
|
using System.Drawing.Imaging;
|
|
using System.Windows.Forms;
|
|
|
|
namespace BeWoPlanerMobil.Util
|
|
{
|
|
public enum WatermarkPosition
|
|
{
|
|
Absolute,
|
|
TopLeft,
|
|
TopRight,
|
|
TopMiddle,
|
|
BottomLeft,
|
|
BottomRight,
|
|
BottomMiddle,
|
|
MiddleLeft,
|
|
MiddleRight,
|
|
Center
|
|
}
|
|
|
|
public class Watermarker
|
|
{
|
|
private Image _OriginalImage;
|
|
private Image _Watermark;
|
|
private Padding _Margin = new Padding(0);
|
|
|
|
public Image Image { get; private set; }
|
|
|
|
public WatermarkPosition Position { get; set; } = WatermarkPosition.Absolute;
|
|
|
|
public int PositionX { get; set; } = 0;
|
|
|
|
public int PositionY { get; set; } = 0;
|
|
|
|
public float Opacity { get; set; } = 1f;
|
|
|
|
public Color TransparentColor { get; set; } = Color.Empty;
|
|
|
|
public RotateFlipType RotateFlipType { get; set; } = RotateFlipType.RotateNoneFlipNone;
|
|
public Padding Margin { get; set; } = new Padding(0);
|
|
|
|
public Font Font { get; set; } = new Font(FontFamily.GenericSansSerif, 10);
|
|
|
|
public Color FontColor { get; set; } = Color.Black;
|
|
|
|
public float ScaleRatio { get; set; } = 1f;
|
|
|
|
public Watermarker(Image image)
|
|
{
|
|
LoadImage(image);
|
|
}
|
|
|
|
public void ResetImage()
|
|
{
|
|
Image = new Bitmap(_OriginalImage);
|
|
}
|
|
|
|
public void DrawImage(string filename)
|
|
{
|
|
Image = new Bitmap(_OriginalImage);
|
|
}
|
|
|
|
public void DrawImage(Image watermark)
|
|
{
|
|
if (watermark == null)
|
|
{
|
|
throw new ArgumentOutOfRangeException("Watermark");
|
|
}
|
|
|
|
if (Opacity < 0 || Opacity > 1)
|
|
{
|
|
throw new ArgumentOutOfRangeException("Opacity");
|
|
}
|
|
|
|
if (ScaleRatio <= 0)
|
|
{
|
|
throw new ArgumentOutOfRangeException("ScaleRatio");
|
|
}
|
|
|
|
_Watermark = GetWatermarkImage(watermark);
|
|
|
|
_Watermark.RotateFlip(RotateFlipType);
|
|
|
|
var watermarkPosition = GetWatermarkPosition();
|
|
|
|
var destinationRectangle = new Rectangle(watermarkPosition.X, watermarkPosition.Y, _Watermark.Width, _Watermark.Height);
|
|
|
|
var colorMatrix = new ColorMatrix(
|
|
new[]
|
|
{
|
|
new[] {1f, 0f, 0f, 0f, 0f},
|
|
new[] {0f, 1f, 0f, 0f, 0f},
|
|
new[] {0f, 0f, 1f, 0f, 0f},
|
|
new[] {0f, 0f, 0f, Opacity, 0f},
|
|
new[] {0f, 0f, 0f, 0f, 1f}
|
|
});
|
|
|
|
var attributes = new ImageAttributes();
|
|
|
|
attributes.SetColorMatrix(colorMatrix);
|
|
|
|
if (TransparentColor != Color.Empty)
|
|
{
|
|
attributes.SetColorKey(TransparentColor, TransparentColor);
|
|
}
|
|
|
|
using (var graphics = Graphics.FromImage(Image))
|
|
{
|
|
graphics.DrawImage(_Watermark, destinationRectangle, 0, 0, _Watermark.Width, _Watermark.Height, GraphicsUnit.Pixel, attributes);
|
|
}
|
|
}
|
|
|
|
public void DrawText(string text)
|
|
{
|
|
var textWatermark = GetTextWatermark(text);
|
|
|
|
DrawImage(textWatermark);
|
|
}
|
|
|
|
private void LoadImage(Image image)
|
|
{
|
|
_OriginalImage = image;
|
|
ResetImage();
|
|
}
|
|
|
|
private Image GetTextWatermark(string text)
|
|
{
|
|
var brush = new SolidBrush(FontColor);
|
|
SizeF size;
|
|
|
|
using (var graphics = Graphics.FromImage(Image))
|
|
{
|
|
size = graphics.MeasureString(text, Font);
|
|
}
|
|
|
|
var bitmap = new Bitmap((int) size.Width, (int) size.Height);
|
|
bitmap.SetResolution(Image.HorizontalResolution, Image.VerticalResolution);
|
|
|
|
using (var graphics = Graphics.FromImage(bitmap))
|
|
{
|
|
graphics.DrawString(text, Font, brush, 0, 0);
|
|
}
|
|
|
|
return bitmap;
|
|
}
|
|
|
|
private Image GetWatermarkImage(Image watermark)
|
|
{
|
|
if (_Margin.All == 0 && ScaleRatio == 1.0f)
|
|
{
|
|
return watermark;
|
|
}
|
|
|
|
var newWidth = Convert.ToInt32(watermark.Width * ScaleRatio);
|
|
var newHeight = Convert.ToInt32(watermark.Height * ScaleRatio);
|
|
|
|
var sourceRectangle = new Rectangle(_Margin.Left, _Margin.Top, newWidth, newHeight);
|
|
var destinationRectangle = new Rectangle(0, 0, watermark.Width, watermark.Height);
|
|
|
|
var bitmap = new Bitmap(newWidth + _Margin.Left + _Margin.Right, newHeight + _Margin.Top + _Margin.Bottom);
|
|
bitmap.SetResolution(watermark.HorizontalResolution, watermark.VerticalResolution);
|
|
|
|
using (var graphics = Graphics.FromImage(bitmap))
|
|
{
|
|
graphics.DrawImage(watermark, sourceRectangle, destinationRectangle, GraphicsUnit.Pixel);
|
|
}
|
|
|
|
return bitmap;
|
|
}
|
|
|
|
private Point GetWatermarkPosition()
|
|
{
|
|
int x, y;
|
|
|
|
switch (Position)
|
|
{
|
|
case WatermarkPosition.Absolute:
|
|
x = PositionX;
|
|
y = PositionY;
|
|
break;
|
|
case WatermarkPosition.TopLeft:
|
|
x = 0;
|
|
y = 0;
|
|
break;
|
|
case WatermarkPosition.TopRight:
|
|
x = Image.Width;
|
|
y = 0;
|
|
break;
|
|
case WatermarkPosition.TopMiddle:
|
|
x = (Image.Width - _Watermark.Width) / 2;
|
|
y = 0;
|
|
break;
|
|
case WatermarkPosition.BottomLeft:
|
|
x = 0;
|
|
y = Image.Height - _Watermark.Height;
|
|
break;
|
|
case WatermarkPosition.BottomRight:
|
|
x = Image.Width - _Watermark.Width;
|
|
y = Image.Height - _Watermark.Height;
|
|
break;
|
|
case WatermarkPosition.BottomMiddle:
|
|
x = (Image.Width - _Watermark.Width) / 2;
|
|
y = Image.Height - _Watermark.Height;
|
|
break;
|
|
case WatermarkPosition.MiddleLeft:
|
|
x = 0;
|
|
y = (Image.Height - _Watermark.Height) / 2;
|
|
break;
|
|
case WatermarkPosition.MiddleRight:
|
|
x = Image.Width - _Watermark.Width;
|
|
y = (Image.Height - _Watermark.Height) / 2;
|
|
break;
|
|
case WatermarkPosition.Center:
|
|
x = (Image.Width - _Watermark.Width) / 2;
|
|
y = (Image.Height - _Watermark.Height) / 2;
|
|
break;
|
|
default:
|
|
throw new ArgumentOutOfRangeException();
|
|
}
|
|
|
|
return new Point(x, y);
|
|
}
|
|
}
|
|
} |