【问题标题】:Text added to image cuts off at the edges添加到图像的文本在边缘被切断
【发布时间】:2020-03-05 23:24:41
【问题描述】:

我正在尝试在图像上添加文字,如果文字很短,效果很好。但是,如果我添加更长的文本,它将在边缘被切断。

如果文本太长,我需要换行。

这是我目前的代码:

Graphics graphicImage = Graphics.FromImage(bitMapImage);

//Smooth graphics is nice.
graphicImage.SmoothingMode = SmoothingMode.AntiAlias;
Font font = new Font("Tahoma", 6);
int x_axis = 50;
int y_axis = 120;
int distance = 50;
graphicImage.DrawString("This is a very long text and this long text might come to a new line below", font, Brushes.Yellow, new PointF(x_axis, y_axis + distance * 2));

String tempFile = folder + "output.jpeg";
bitMapImage.Save(tempFile, ImageFormat.Jpeg);

这是它创建的图像

如何让文字换行?

【问题讨论】:

    标签: c# image text


    【解决方案1】:

    在一个矩形中绘制文本,它会自动换行:

    string text1 = "Draw `enter code here`text in a rectangle by passing a RectF to the DrawString method.";
    using (Font font1 = new Font("Arial", 12, FontStyle.Bold, GraphicsUnit.Point))
    {
        RectangleF rectF1 = new RectangleF(30, 10, 100, 122);
        e.Graphics.DrawString(text1, font1, Brushes.Blue, rectF1);
        e.Graphics.DrawRectangle(Pens.Black, Rectangle.Round(rectF1));
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-02-05
      • 2021-12-30
      • 2020-12-05
      • 1970-01-01
      • 2018-11-08
      • 2017-08-14
      相关资源
      最近更新 更多