c# - 添加到图像的文本在边缘被切断

标签 c# image text

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

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

这是我目前的代码:

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

这是它创建的图像 Image with text overlay

如何让文字换行?

最佳答案

在矩形中绘制文本,它会换行:

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

关于c# - 添加到图像的文本在边缘被切断,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60555424/

相关文章:

安卓图像大小限制

css - wordpress woocommerce 图像大小 - 容器正在拉伸(stretch)图像

sql - 在 Stackoverflow 上进行类似于 "Related Questions"的搜索使用的 SQL 是什么

c# - 带有功能区的 Visual Studio 图像 (XML)

c# - 仅从 Azure Web Scheduler 调用 Asp.Net MVC Web API

jquery - 有没有办法根据名称定位目录中的图像?

c - 从文本文件读取到字符串数组

c# - 文本区域和超链接?

c# - 复杂对象不能被同一个实体多次引用

c# - 如何通过沙箱从托管代码调用外部非托管应用程序?