gpt4 book ai didi

c# - 如何对齐图像中的水印

转载 作者:行者123 更新时间:2023-12-04 07:20:37 25 4
gpt4 key购买 nike

我正在尝试向图像添加多个水印,中间有一些间隙。不过,我已经能够通过两个小警告实现这一点。
我想要的是:

  • 水印应垂直居中对齐。
  • 当图像中没有足够的宽度和/或高度时,程序需要停止添加水印(以便没有剪切文本)。

  • 我的代码是:
    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);
    }
    }
    这就是我得到的:
    Resu
    所需的输出。
    enter image description here
    任何帮助深表感谢。
    谢谢你!
    更新 1
    Update-1

    最佳答案

    您所需的输出在 x 轴上没有任何偏移,因此您根本不应增加 x 偏移。

                        offsetY += increment;
    index++;
    }
    while (offsetY * 1.25 < image.Height);
    image.Save(path + destFileName);

    关于c# - 如何对齐图像中的水印,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68525845/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com