gpt4 book ai didi

C# 从右角开始旋转 DrawString

转载 作者:太空宇宙 更新时间:2023-11-03 20:51:38 24 4
gpt4 key购买 nike

我试图在图像上从右上角向左下角绘制文本。我的代码弄乱了拉绳的定位。到目前为止,这段代码是这样绘制的,但我需要帮助在这些红线之间绘制文本。

enter image description here

string img_src = "F:\\blank_imge.jpg";
System.Drawing.Image selected_img = System.Drawing.Image.FromFile(img_src);
using(Graphics gr = Graphics.FromImage(selected_img))
{
Font font = new Font("Arial", 2.0f);
int x = selected_img.Width;
int y = 0;
for (int b = 1; b <= 5; b++)
{
GraphicsState state = gr.Save();
gr.ResetTransform();
int opacity = 35;
string txt = "watermark";
using (Brush brush = new SolidBrush(Color.FromArgb(opacity, 255, 255, 255)))
{
SizeF textSize = gr.MeasureString("watermark", font);
gr.RotateTransform(45);
gr.SmoothingMode = SmoothingMode.HighQuality;
gr.TextRenderingHint = TextRenderingHint.AntiAliasGridFit;
gr.DrawString(txt, font, brush, new PointF(x - textSize.Width, y));
}

y = y + 25;
gr.Restore(state);
}
selected_img.Save("F:\\watermarked.jpg");
}

最佳答案

我会更像这样写。那里有很多小变化……仔细看!

        using (Graphics gr = Graphics.FromImage(selected_img))
{
int y = -50;
int opacity = 127; // 0 to 255
string txt = "watermark";
int x = selected_img.Width;
GraphicsState state = gr.Save();
gr.ResetTransform();
gr.TranslateTransform(selected_img.Width / 2, selected_img.Height / 2);
gr.RotateTransform(45);
gr.SmoothingMode = SmoothingMode.HighQuality;
gr.TextRenderingHint = TextRenderingHint.AntiAliasGridFit;
using (Font font = new Font("Arial", 14.0f))
{
SizeF textSize = gr.MeasureString(txt, font);
using (Brush brush = new SolidBrush(Color.FromArgb(opacity, Color.DarkGray)))
{
for (int b = 1; b <= 5; b++, y += 25)
{
gr.DrawString(txt, font, brush, new PointF(-(textSize.Width / 2), y));
}
}
}
gr.Restore(state);
}

关于C# 从右角开始旋转 DrawString,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54594396/

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