C#:使用抽绳注释图像可以工作,但无法获取左下角的文本,只能获取右下角的文本
我正在为自己和工作中的其他人制作一个批量水印工具,并使用以下代码允许我在图像右下角注释文本,但如果不手动调整图像,我无法使其在左下角注释对于任何给定图像而言不同的坐标。另外,将 StringAlignment.Far 更改为 StringAlignment.Near 等不会执行任何操作,但可能会在图像外部未显示的地方注释文本。
MSDN 有一些解释,但它对我没有帮助。任何帮助都会很棒,我已经为此奋斗了一段时间了。
private void button1_Click(object sender, EventArgs e)
{
foreach (string images in Directory.GetFiles(textBox1.Text))
{
System.Drawing.Image img = System.Drawing.Image.FromFile(images);
Graphics gr = Graphics.FromImage(img);
Font font = new Font("Times New Roman", (float)25,
System.Drawing.FontStyle.Regular);
System.Drawing.Color color = System.Drawing.Color.Red;
StringFormat stringFormat = new StringFormat();
stringFormat.Alignment = StringAlignment.Far;
stringFormat.LineAlignment = StringAlignment.Far;
gr.SmoothingMode = SmoothingMode.AntiAlias;
gr.DrawString("WATERMARK GOES HERE"+ images, font,
new System.Drawing.SolidBrush(color),
new System.Drawing.Point(img.Width - 0, img.Height - 0),
stringFormat);
MemoryStream outputStream = new MemoryStream();
img.Save(images+"Stamped.jpg");
}
MessageBox.Show("done");
}
I'm making a batch watermarking tool for myself and some others at work and using the following code allows me to annotate text on the bottom right of the image but I'm not able to make it annotate on the bottom left without manually adjusting the coordinates which differs for any given image. Also changing StringAlignment.Far to StringAlignment.Near etc doesn't do anything but possibly annotate the text outside the image somewhere that doesn't show up.
MSDN has some explanation but it is not helping me. Any help would be great I've been fighting this for some time now.
private void button1_Click(object sender, EventArgs e)
{
foreach (string images in Directory.GetFiles(textBox1.Text))
{
System.Drawing.Image img = System.Drawing.Image.FromFile(images);
Graphics gr = Graphics.FromImage(img);
Font font = new Font("Times New Roman", (float)25,
System.Drawing.FontStyle.Regular);
System.Drawing.Color color = System.Drawing.Color.Red;
StringFormat stringFormat = new StringFormat();
stringFormat.Alignment = StringAlignment.Far;
stringFormat.LineAlignment = StringAlignment.Far;
gr.SmoothingMode = SmoothingMode.AntiAlias;
gr.DrawString("WATERMARK GOES HERE"+ images, font,
new System.Drawing.SolidBrush(color),
new System.Drawing.Point(img.Width - 0, img.Height - 0),
stringFormat);
MemoryStream outputStream = new MemoryStream();
img.Save(images+"Stamped.jpg");
}
MessageBox.Show("done");
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
至于水印本身,您应该决定是否希望它随图像大小缩放,或者保持一致的大小。或者您可以设置最大/最小尺寸。那是你的偏好。
try/catch 是一种跳过非图像文件的惰性方法。由于 Directory.GetFiles 返回目录中的所有文件,因此非图像文件将导致异常。这可以以更简洁的方式完成,但由于这不是你问题的本质,所以我保持简单。
As for the watermarking itself, you should decide if you want it to scale with image size, or to be a consistent size. Or you can have it have a maximum/minimum size. That's your preference.
The try/catch is a lazy way of skipping files which aren't images. Since Directory.GetFiles returns all files in the directory, a non-image file would cause an exception. This could be done in a much neater fashion, but since that was not the nature of your question I kept it simple.