public static void AddWaterMark(MemoryStream ms, string watermarkText, MemoryStream outputStream)
{
System.Drawing.Image img = System.Drawing.Image.FromStream(ms);
Graphics gr = Graphics.FromImage(img);
Font font = new Font("Tahoma", (float)40);
Color color = Color.FromArgb(50, 241, 235, 105);
double tangent = (double)img.Height / (double)img.Width;
double angle = Math.Atan(tangent) * (180 / Math.PI);
double halfHypotenuse = Math.Sqrt((img.Height * img.Height) + (img.Width * img.Width)) / 2;
double sin, cos, opp1, adj1, opp2, adj2;
for (int i = 100; i > 0; i--)
{
font = new Font("Tahoma", i, FontStyle.Bold);
SizeF sizef = gr.MeasureString(watermarkText, font, int.MaxValue);
sin = Math.Sin(angle * (Math.PI / 180));
cos = Math.Cos(angle * (Math.PI / 180));
opp1 = sin * sizef.Width;
adj1 = cos * sizef.Height;
opp2 = sin * sizef.Height;
adj2 = cos * sizef.Width;
if (opp1 + adj1 < img.Height && opp2 + adj2 < img.Width)
break;
//
}
StringFormat stringFormat = new StringFormat();
stringFormat.Alignment = StringAlignment.Center;
stringFormat.LineAlignment = StringAlignment.Center;
gr.SmoothingMode = SmoothingMode.AntiAlias;
gr.RotateTransform((float)angle);
gr.DrawString(watermarkText, font, new SolidBrush(color), new Point((int)halfHypotenuse, 0), stringFormat);
img.Save(outputStream, ImageFormat.Jpeg);
}
in the same place where you call gr.DrawString, you can also do gr.DrawImage(position, size, overlayImage). It helps if your image-to-overlay is loaded from a PNG file (with transparency) to produce the best quality.
One of the best way to stamp an image with time and location.
Ref of the above example is from http://stackoverflow.com/questions/1224653/place-watermark-image-on-other-images-c-asp-net
{
System.Drawing.Image img = System.Drawing.Image.FromStream(ms);
Graphics gr = Graphics.FromImage(img);
Font font = new Font("Tahoma", (float)40);
Color color = Color.FromArgb(50, 241, 235, 105);
double tangent = (double)img.Height / (double)img.Width;
double angle = Math.Atan(tangent) * (180 / Math.PI);
double halfHypotenuse = Math.Sqrt((img.Height * img.Height) + (img.Width * img.Width)) / 2;
double sin, cos, opp1, adj1, opp2, adj2;
for (int i = 100; i > 0; i--)
{
font = new Font("Tahoma", i, FontStyle.Bold);
SizeF sizef = gr.MeasureString(watermarkText, font, int.MaxValue);
sin = Math.Sin(angle * (Math.PI / 180));
cos = Math.Cos(angle * (Math.PI / 180));
opp1 = sin * sizef.Width;
adj1 = cos * sizef.Height;
opp2 = sin * sizef.Height;
adj2 = cos * sizef.Width;
if (opp1 + adj1 < img.Height && opp2 + adj2 < img.Width)
break;
//
}
StringFormat stringFormat = new StringFormat();
stringFormat.Alignment = StringAlignment.Center;
stringFormat.LineAlignment = StringAlignment.Center;
gr.SmoothingMode = SmoothingMode.AntiAlias;
gr.RotateTransform((float)angle);
gr.DrawString(watermarkText, font, new SolidBrush(color), new Point((int)halfHypotenuse, 0), stringFormat);
img.Save(outputStream, ImageFormat.Jpeg);
}
in the same place where you call gr.DrawString, you can also do gr.DrawImage(position, size, overlayImage). It helps if your image-to-overlay is loaded from a PNG file (with transparency) to produce the best quality.
One of the best way to stamp an image with time and location.
Ref of the above example is from http://stackoverflow.com/questions/1224653/place-watermark-image-on-other-images-c-asp-net
No comments:
Post a Comment