C#:使用抽绳注释图像可以工作,但无法获取左下角的文本,只能获取右下角的文本

发布于 2024-12-23 02:07:17 字数 1342 浏览 2 评论 0原文

我正在为自己和工作中的其他人制作一个批量水印工具,并使用以下代码允许我在图像右下角注释文本,但如果不手动调整图像,我无法使其在左下角注释对于任何给定图像而言不同的坐标。另外,将 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

旧伤慢歌 2024-12-30 02:07:17
  • 为您的控件命名。不要使用“button1”、“textbox1”等。
  • 使用“using”语句。编写“System.Drawing.Point”和其他完全限定名称只会增加代码的大小并使其更难以阅读。
  • 您正在为要加水印的每个图像创建 SolidBrush 类的新实例。您应该在循环之前创建画笔并在循环中使用它,然后再将其丢弃。
  • 您对 MemoryStream 的声明不执行任何操作,也不会在任何地方使用。

至于水印本身,您应该决定是否希望它随图像大小缩放,或者保持一致的大小。或者您可以设置最大/最小尺寸。那是你的偏好。

private void watermark_btn_Click(object sender, EventArgs e)
{
    string watermarkText = "ShowThisWatermark";

    using (Font font = new Font("Times New Roman", (float)25, FontStyle.Regular))
    using (SolidBrush brush = new SolidBrush(Color.Red))
    foreach (string file in Directory.GetFiles(directory_txt.Text))
    {
        try
        {
            Bitmap b = new Bitmap(file);

            using (Graphics g = Graphics.FromImage(b))
            {
                g.SmoothingMode = SmoothingMode.AntiAlias;

                SizeF measuredSize = g.MeasureString(watermarkText, font);

                // Use this to watermark the bottom-left corner
                g.DrawString(watermarkText, font, brush, 0, b.Height - measuredSize.Height);

                // Use this to watermark the bottom-right corner
                g.DrawString(watermarkText, font, brush, b.Width - measuredSize.Width, b.Height - measuredSize.Height);
            }

            b.Save(Path.GetFileNameWithoutExtension(file) + "_stamped" + Path.GetExtension(file));
        }
        catch
        {
            continue;
        }
    }
}

try/catch 是一种跳过非图像文件的惰性方法。由于 Directory.GetFiles 返回目录中的所有文件,因此非图像文件将导致异常。这可以以更简洁的方式完成,但由于这不是你问题的本质,所以我保持简单。

  • Name your controls. Don't use "button1", "textbox1", etc.
  • Use the "using" statement. Writing "System.Drawing.Point" and other fully qualified names just increases the size of your code and makes it harder to read.
  • You are creating a new instance of a SolidBrush class for each image you are watermarking. You should create the brush before the loop and just use it in the loop, then dispose of it afterwards.
  • Your declaration of a MemoryStream does nothing and is used nowhere.

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.

private void watermark_btn_Click(object sender, EventArgs e)
{
    string watermarkText = "ShowThisWatermark";

    using (Font font = new Font("Times New Roman", (float)25, FontStyle.Regular))
    using (SolidBrush brush = new SolidBrush(Color.Red))
    foreach (string file in Directory.GetFiles(directory_txt.Text))
    {
        try
        {
            Bitmap b = new Bitmap(file);

            using (Graphics g = Graphics.FromImage(b))
            {
                g.SmoothingMode = SmoothingMode.AntiAlias;

                SizeF measuredSize = g.MeasureString(watermarkText, font);

                // Use this to watermark the bottom-left corner
                g.DrawString(watermarkText, font, brush, 0, b.Height - measuredSize.Height);

                // Use this to watermark the bottom-right corner
                g.DrawString(watermarkText, font, brush, b.Width - measuredSize.Width, b.Height - measuredSize.Height);
            }

            b.Save(Path.GetFileNameWithoutExtension(file) + "_stamped" + Path.GetExtension(file));
        }
        catch
        {
            continue;
        }
    }
}

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文