在图像上书写时在图像中添加新行

本文关键字:图像 添加 新行 书写 | 更新日期: 2023-09-27 18:14:50

我试图在图像上写文字,但我的文本有这么多单词,它不适合单行。所以我需要在我使用的代码下面的图像上添加新行(类似换行的东西)。

string strFileName = Server.MapPath("~") + "''Certificate''" + CertificateName.ToString();
Bitmap bitMapImage = new Bitmap(strFileName);
Graphics graphicImage = Graphics.FromImage(bitMapImage);
graphicImage.SmoothingMode = SmoothingMode.AntiAlias;
graphicImage.DrawString(strCourseName, new System.Drawing.Font("Arial", 22, FontStyle.Bold), SystemBrushes.GrayText, new Point(280, 325));
string strDesImgName = Server.MapPath("~") + "''Certi''certificate.jpg";
bitMapImage.Save(strDesImgName, ImageFormat.Jpeg);
graphicImage.Dispose();
bitMapImage.Dispose();
有谁能告诉我如何在下一行插入额外的文本吗?

在图像上书写时在图像中添加新行

你基本上要实现你自己的版本的文本行换行调用你的DrawString函数两次。一次写第一行(部分截断),一次写剩下的文本。你传递给DrawString函数的point参数必须随着你使用的字体的高度而增加。参见字体高度。如果您不使用固定宽度字体,则很难知道字符串的宽度以及在何处分割它。这是因为非固定宽度字体中的每个字符都有不同的宽度。对于这个例子,我在第10个字符上分割字符串,您将不得不尝试对您的应用程序最有意义的方法。

graphicImage.DrawString(strCourseName, new System.Drawing.Font("Arial", 22, FontStyle.Bold), SystemBrushes.GrayText, new Point(280, 325));

graphicImage.DrawString(strCourseName.Substring(0,10), new System.Drawing.Font("Arial", 22, FontStyle.Bold), SystemBrushes.GrayText, new Point(280, 325));
graphicImage.DrawString(strCourseName.Substring(10), , new System.Drawing.Font("Arial", 22, FontStyle.Bold), SystemBrushes.GrayText, new Point(280, 347));