以一定角度在图像上平铺文本

Tiling Text Over Image at an Angle

我使用以下代码在图像的中心绘制文本角度

Bitmap bmp = new Bitmap(pictureBox1.Image);
using (Graphics g = Graphics.FromImage(bmp)) {
  g.TranslateTransform(bmp.Width / 2, bmp.Height / 2);
  g.RotateTransform(30);
  SizeF textSize = g.MeasureString("hi", font);
  g.DrawString("hi", font, Brushes.Red, -(textSize.Width / 2), -(textSize.Height / 2));
}

我需要像这样在整个图像上平铺文字

我知道我可以增加坐标并使用 Loop.I have

Bitmap bmp = new Bitmap(pictureBox1.Image);
            for (int i = 0; i < bmp.Width; i += 20)
            {
                for (int y = 0; y < bmp.Height; y += 20)
                {
                    using (Graphics g = Graphics.FromImage(bmp))
                    {
                        g.TranslateTransform(bmp.Width / 2, bmp.Height / 2);                      
                        g.RotateTransform(30);
                        SizeF textSize = g.MeasureString("my test image", DefaultFont);
                        g.DrawString("my test image", DefaultFont, Brushes.Yellow, i, y);
                       
                    }
                }
            }
            pictureBox1.Image = bmp;

这会产生以下结果

如何通过测量绘制区域来正确放置文本properly.May是一种更好更快的方法。

您正在页面中央插入文本。
这意味着您的图片 0,0 坐标位于另一张图片的 50%、50%

如果你想得到你想要的结果,我建议你将图像宽度分成 25% 的块,以获得建议的 16 个块。然后在每个块的中心添加其中一个文本图像。

请记住,当您添加图像并且您希望图像从原点而不是原点 0,0(这是您的情况发生的情况)开始旋转时,您需要明确说明它,认为命令是 rotateorigan 或该行中的内容,

调用额外的 TranslateTransform 将文本移动到您想要的位置,然后在 (0, 0) 坐标处使用 DrawString 绘制文本。这将围绕其自身的中心旋转每个文本,而不是围绕段落的中心旋转文本。

Bitmap bmp = new Bitmap(pictureBox1.Image);
Graphics g = Graphics.FromImage(bmp);
String text = "TextTile";

Font font = new Font(DefaultFont.Name, 20);
SizeF size = g.MeasureString(text, font);
int textwidth = size.ToSize().Width;
int textheight = size.ToSize().Height;

int y_offset = (int)(textwidth * Math.Sin(45 * Math.PI / 180.0));

//the sin of the angle may return zero or negative value, 
//it won't work with this formula
if (y_offset >= 0)
{
    for (int x = 0; x < bmp.Width; x += textwidth)
    {
        for (int y = 0; y < bmp.Height; y += y_offset)
        {
            //move to this position
            g.TranslateTransform(x, y);

            //draw text rotated around its center
            g.TranslateTransform(textwidth, textheight);
            g.RotateTransform(-45);
            g.TranslateTransform(-textwidth, -textheight);
            g.DrawString(text, font, Brushes.Yellow, 0, 0);

            //reset
            g.ResetTransform();
        }
    }
}

pictureBox1.Image = bmp;

以上示例使用了 20 号的较大字体。您可以将其设置回使用 DefaultFont.size。它使用45度角。