Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/272.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# C从右角旋转的抽绳_C#_Graphics_Rotation_Drawstring_Rotatetransform - Fatal编程技术网

C# C从右角旋转的抽绳

C# C从右角旋转的抽绳,c#,graphics,rotation,drawstring,rotatetransform,C#,Graphics,Rotation,Drawstring,Rotatetransform,我试图从右上角到左下角在图像上绘制文本。我的代码搞乱了抽绳的位置。到目前为止,这个代码是这样绘制的,但我需要帮助在这些红线之间绘制文本 要移动原点,您需要先对其进行平移: 比如: gr.TranslateTransform(x, y); gr.RotateTransform(45); gr.TranslateTransform(-x, -y); 可以先测量字符串的长度以确定文本的中心 伪 var size = gr.MeasureString(txt, font); var hal

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


要移动原点,您需要先对其进行平移:

比如:

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

未测试…

您需要先对其进行转换以移动原点:

比如:

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

未测试…

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

沿着对角线绘制:您的示例似乎表明您希望将每一行文本垂直于第二条对角线,因此我将使用它。 我假设我们谈论的是矩形,所以45度的部分很愚蠢,添加了计算实际角度的代码 在一个范围内画我不知道你的问题是定位还是保持字符串宽度在范围内。 下面的代码说明了如何沿对角线部分绘制,以使定位部分正确。在我看来,把东西放在一个带上更容易解决,你必须用g来循环。在东西放在一起或把文字包起来之前,我不知道确切的要求。如果你需要更多的澄清,请告诉我

我已经尝试在代码中包含注释,但是如果有什么需要清除,请告诉我。 我希望格式化代码会更容易,所以

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

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

沿着对角线绘制:您的示例似乎表明您希望将每一行文本垂直于第二条对角线,因此我将使用它。 我假设我们谈论的是矩形,所以45度的部分很愚蠢,添加了计算实际角度的代码 在一个范围内画我不知道你的问题是定位还是保持字符串宽度在范围内。 下面的代码说明了如何沿对角线部分绘制,以使定位部分正确。在我看来,把东西放在一个带上更容易解决,你必须用g来循环。在东西放在一起或把文字包起来之前,我不知道确切的要求。如果你需要更多的澄清,请告诉我

我已经尝试在代码中包含注释,但是如果有什么需要清除,请告诉我。 我希望格式化代码会更容易,所以

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

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

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

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

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

RotateTransform几乎总是需要合适的TranslateTransform RotateTransform几乎总是需要合适的TranslateTransform包围谢谢你们抽出时间来帮助我。再次感谢你们抽出时间来帮助我。再次感谢