我想在对角线的中心放置水印。我的代码做得很好,但水印没有出现在每个图像的中心。我认为水印文本的位置存在问题,我硬编码值。我怎么写水印文本定位的通用公式。
private MemoryStream ApplyWaterMark(MemoryStream stream)
{
System.Drawing.Image Im = System.Drawing.Image.FromStream(stream); //
Graphics g = Graphics.FromImage(Im);
// Create a solid brush to write the watermark text on the image
Brush myBrush = new SolidBrush(System.Drawing.Color.FromArgb(25,
System.Drawing.Color.LightSteelBlue));
var f = new System.Drawing.Font(FontFamily.GenericSerif, 30);
// Calculate the size of the text
SizeF sz = g.MeasureString("TEST WATER MARK", f);
int X, Y;
X = ((int)(Im.Width - sz.Width) / 2)-1162;
Y = ((int)(Im.Height + sz.Height) / 2)-127;
g.RotateTransform(-45f);
g.DrawString("TEST WATER MARK", f, myBrush,new System.Drawing.Point(X, Y));
g.RotateTransform(45f);
Im.Save(OutPutStream, ImageFormat.Png);//save image with dynamic watermark
return OutPutStream;
}
答案 0 :(得分:6)
您可以通过这种方式对角设置水印
Bitmap newBitmap = new Bitmap(bitmap);
Graphics g = Graphics.FromImage(newBitmap);
// Trigonometry: Tangent = Opposite / Adjacent
double tangent = (double)newBitmap.Height /
(double)newBitmap.Width;
// convert arctangent to degrees
double angle = Math.Atan(tangent) * (180/Math.PI);
// a^2 = b^2 + c^2 ; a = sqrt(b^2 + c^2)
double halfHypotenuse =(Math.Sqrt((newBitmap.Height
* newBitmap.Height) +
(newBitmap.Width *
newBitmap.Width))) / 2;
// Horizontally and vertically aligned the string
// This makes the placement Point the physical
// center of the string instead of top-left.
StringFormat stringFormat = new StringFormat();
stringFormat.Alignment = StringAlignment.Center;
stringFormat.LineAlignment=StringAlignment.Center;
g.RotateTransform((float)angle);
g.DrawString(waterMarkText, font, new SolidBrush(color),
new Point((int)halfHypotenuse, 0),
stringFormat);
答案 1 :(得分:0)
尝试以这种方式为图像中心计算坐标(X,Y):
int X, Y;
X = (int)(Im.Width / 2 - sz.Width / 2);
Y = (int)(Im.Height / 2 - sz.Height / 2);
答案 2 :(得分:0)
底部有多种颜色的水印
public void AddWatermark(string watermarkText, string image, string TColor)
{
System.Drawing.Image bitmap = (System.Drawing.Image)Bitmap.FromFile(Proot + image);
Font font = new Font("Arial", 12, FontStyle.Bold, GraphicsUnit.Pixel);
RectangleF rectf = new RectangleF(70, 90, 90, 50);
Color color = Color.FromArgb(255, 255, 0, 0);
try
{
if (TColor.ToUpper() == "RED")
{
color = Color.FromArgb(255, 255, 0, 0);
}
else if (TColor.ToUpper() == "WHITE")
{
color = Color.FromArgb(255, 255, 255, 255);
}
else if (TColor.ToUpper() == "BLACK")
{
color = Color.FromArgb(155, 0, 0, 0);
}
else if (TColor.ToUpper() == "GREEN")
{
color = Color.FromArgb(255, 0, 255, 0);
}
}
catch { }
// WHITE (255, 255, 255, 255)
// RED (255, 255, 000, 000)
// GREEN (255, 000, 255, 000)
// BLUE (255, 000, 000, 255)
// BLACK (150, 000, 000, 000)
// PURPLE(255, 125, 000, 255)
// GREY (255, 128, 128, 128)
// YELLOW(255, 255, 255, 000)
// ORANGE(255, 255, 125, 000)
// Point atpoint = new Point(bitmap.Width / 2, bitmap.Height / 2);
Point atpoint = new Point(bitmap.Width / 2, bitmap.Height - 10);
SolidBrush brush = new SolidBrush(color);
Graphics graphics = Graphics.FromImage(bitmap);
StringFormat sf = new StringFormat();
sf.Alignment = StringAlignment.Center;
sf.LineAlignment = StringAlignment.Center;
graphics.DrawString(watermarkText, font, brush, atpoint, sf);
graphics.Dispose();
MemoryStream m = new MemoryStream();
bitmap.Save(m, System.Drawing.Imaging.ImageFormat.Jpeg);
// Response.WriteFile("images/DefaultLogo.png", true);
m.WriteTo(Response.OutputStream);
m.Dispose();
base.Dispose();
}