I use class from this article
Using Trigonometry and Pythagoras to WaterMark an Image[
^]
All work good, but after applying watermark size of image increase greatly. If input file have size 144 kb - in ouptut it increase to 640kb. Why it so?
public class WaterMark {
private string waterMarkText;
private string fontName;
private FontStyle fontStyle;
private Color color;
private int maxFontSize;
public WaterMark(string waterMarkText,
string fontName, int maxFontSize,
FontStyle fontStyle, Color color,
byte alpha) {
this.waterMarkText = waterMarkText;
this.fontName = fontName;
this.fontStyle = fontStyle;
this.color = Color.FromArgb(alpha, color);
this.maxFontSize = maxFontSize;
}
public Bitmap Apply(string url) {
Bitmap bmp;
using(Bitmap newBitmap = new Bitmap(url))
using(Graphics g = Graphics.FromImage(newBitmap)) {
double tangent = (double) newBitmap.Height /
(double) newBitmap.Width;
double angle = Math.Atan(tangent) * (180 / Math.PI);
double halfHypotenuse = Math.Sqrt((newBitmap.Height * newBitmap.Height) +
(newBitmap.Width *
newBitmap.Width)) / 2;
StringFormat stringFormat = new StringFormat();
stringFormat.Alignment = StringAlignment.Center;
stringFormat.LineAlignment = StringAlignment.Center;
Font font = new Font(fontName, maxFontSize,
fontStyle);
for (int i = maxFontSize; i > 0; i--) {
font = new Font(fontName, i, fontStyle);
SizeF sizef = g.MeasureString(waterMarkText,
font, int.MaxValue);
double sin = Math.Sin(angle * (Math.PI / 180));
double cos = Math.Cos(angle * (Math.PI / 180));
double opp1 = sin * sizef.Width;
double adj1 = cos * sizef.Height;
double opp2 = sin * sizef.Height;
double adj2 = cos * sizef.Width;
if (opp1 + adj1 < newBitmap.Height &&
opp2 + adj2 < newBitmap.Width) {
break;
}
}
g.SmoothingMode = SmoothingMode.None;
g.RotateTransform((float) angle);
g.DrawString(waterMarkText, font,
new SolidBrush(color),
new Point((int) halfHypotenuse, 0),
stringFormat);
bmp = new Bitmap(newBitmap);
newBitmap.Dispose();
}
return bmp;
}
}
What I have tried:
I try use this code but its not help to me.