C#旋转的的DrawString从右角

问题描述 投票:1回答:3

我试图借鉴朝左下角右上角的图像上的文字。我的代码弄乱拉绳的定位。到目前为止,该代码绘制这样的,但我需要帮助来绘制这些红线之间的文本。

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");
    }
c# graphics rotation drawstring rotatetransform
3个回答
1
投票

我会写更多像这样。有很多的变化不大在那里......仔细一看!

        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);
        }

2
投票

你需要它首先转化为移动原点:

就像是:

 gr.TranslateTransform(x, y);
 gr.RotateTransform(45);
 gr.TranslateTransform(-x, -y);

你可以先衡量字符串的长度来确定文本的中心。

伪..

 var size = gr.MeasureString(txt, font);

 var halfWidth = size.X / 2;
 var halfHeight = size.X / 2;

 gr.TranslateTransform(halfWidth , halfHeight);
 gr.RotateTransform(45);
 gr.TranslateTransform(-halfWidth , -halfHeight);

未测试...


1
投票

你的问题似乎有至少两个子问题:

  • 沿对角线画:您的样本似乎表明,要每行文本写垂直于次对角线,所以我将与去。
  • 我认为我们正在谈论矩形,所以45度的部分仅仅是愚蠢的,添加代码来计算实际角度
  • 带,如果你的问题是定位或保持边界内的字符串宽度不知内绘制。

下面的代码解决了沿对角线的一部分绘制,以获得定位部分的权利。在我看来,一个“带”配件的事情(通过与g.MeasureString直到事情装配或换行,我不知道确切的要求,您必须循环)是比较容易解决的问题。让我知道,如果你需要进一步澄清。

enter image description here

我一直在努力,包括在代码中的注释,但让我知道如果有什么需要清理。祝格式化代码将是对SO容易...

public class DiagonalLines
{
    private readonly Font font;
    private readonly Brush brush = new SolidBrush(Color.Black);
    private readonly Image image;
    private readonly float width;
    private readonly float height;

    private readonly float diagonalAngle;

    private readonly string savePath;

    public DiagonalLines(string path, string savePath)
    {
       this.image = Image.FromFile(path);

       width = image.Width;
            height = image.Height;

       //this could be optimized
       //you want to write perpendicular to the secondary diagonal, if I understood correctly
       //Math.Atan(height / width) => angle, in radians of the first diagonal
       //after applying "-" we obtain the angle, in radians, of the secondary diagonal
       //the rest of the first term is converting radians to degrees
       diagonalAngle = -(float)(Math.Atan(height / width) * 180 / Math.PI) + /* perpendicular*/ 90;

       this.font = new Font("Arial", (float)image.Width / 80); //write about 80 characters for a full horizontal text line
       this.savePath = savePath;
}

public void DrawLines(params string[] lines)
{
   using (Graphics g = Graphics.FromImage(image))
   {
       //M should be the largest character in most "western" fonts
       var lineHeight = g.MeasureString("M", font).Height;
       var halfTheLines =  (float)lines.Length / 2; //about half the lines should be "above" the midpoint of the secondary diagonal
       var offsetY = -(halfTheLines * lineHeight); //we scale the position against the line height
                                                   //same effect could probably be achieved with ScaleTransform

       g.DrawLine(Pens.Red, 0, height, width, 0); //draw the secondary diagonal

       foreach (var val in lines)
       {
            var size = g.MeasureString(val, font);

            g.ResetTransform();

            g.TranslateTransform(width / 2, height / 2); //go to center of image
            g.RotateTransform(diagonalAngle);

            //translate, to center the text and apply our offset
            g.TranslateTransform(-size.Width / 2, -size.Height / 2 + offsetY); 

            g.DrawString(val, font, brush, 0, 0);

            offsetY += lineHeight;
         }
     }

     image.Save(savePath);
}
}

static void Main(string[] args)
{
   var lines = new DiagonalLines("c:\\temp\\img\\poza.png", "c:\\temp\\img\\watermarked.jpg");
   lines.DrawLines("this", "that", "the other", "and another");
   Process.Start("c:\\temp\\img\\watermarked.jpg");
}
© www.soinside.com 2019 - 2024. All rights reserved.