【发布时间】:2021-10-02 03:43:37
【问题描述】:
我正在尝试将多个水印添加到图像中,中间有一些间隙。不过,我已经能够通过两个小警告来实现这一目标。
我想要的是:
- 水印应垂直居中对齐。
- 当图像中没有足够的宽度和/或高度时,程序需要停止添加水印(以便没有切割文本)。
我的代码是:
static void WatermarkedImage(string path, string fileName, string message, string destFileName)
{
using (Image image = Image.FromFile(path + fileName))
{
Graphics graphics = Graphics.FromImage(image);
Font font = new Font("Arial", 20, FontStyle.Bold);
SolidBrush brush = new SolidBrush(Color.FromArgb(150, 255, 0, 0));
StringFormat format = new StringFormat();
format.Alignment = StringAlignment.Center;
format.FormatFlags = StringFormatFlags.MeasureTrailingSpaces;
int index = 0;
float offsetX = image.Width / 3;
float offsetY = image.Height / 3;
int increment = Convert.ToInt32(image.Height * 0.15);
while (offsetY * 1.25 < image.Height)
{
Matrix matrix = new Matrix();
matrix.Translate(offsetX, offsetY);
matrix.Rotate(-45.0f);
graphics.Transform = matrix;
graphics.DrawString(message, font, brush, 0, 50, format);
offsetX += increment;
offsetY += increment;
index++;
}
image.Save(path + destFileName);
}
}
这就是我得到的结果:
所需的输出。
非常感谢任何帮助。 谢谢!
Update-1
【问题讨论】:
-
我已经删除了你的 ASP.NET 标签,因为这个问题是关于图像处理而不是关于 ASP.NET(网络框架)的。
-
当您说“应该在末尾对齐”时,您的意思是垂直对齐,对吧?
-
@Cleptus 是的,我的意思是垂直对齐的文本。请在最后查看所需的输出。谢谢。
-
在您的循环中添加
offsetX += increment;。这导致向右移动。尝试评论该行。
标签: c# image image-processing