我想在图像中心对角放置水印。我的代码做得很好,但水印没有出现在每个图像的中心。我认为在我硬编码值的位置放置水印文本存在问题。我该如何编写通用公式水印文本的定位。
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;
}