如何使用图像处理程序不受图像大小的限制

发布于 2024-08-14 02:40:46 字数 975 浏览 2 评论 0原文

我正在将文本转换为图像。有些文本的长度比其他文本长。
如何确保没有任何文本被截断?

下面的代码将我的位图限制为 250、30。

System.Drawing.Bitmap imgIn = new System.Drawing.Bitmap(250, 30);
System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(imgIn);
g.Clear(System.Drawing.Color.White);
    System.Drawing.Font font = new System.Drawing.Font("Arial", 10, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Pixel);

我正在遵循以下示例:如何转换电子邮件地址或其他文本将文本框转换为图像

更新

我发现这篇文章有助于完成我的任务:使用 C# 从文本生成图像或使用 C# 将文本转换为图像

在我能够根据文本长度调整图像大小后,我发现我需要在文本中引入换行符,否则图像会一直延伸到廷巴克图 文字是几句话。
如何在长文本中引入换行符?

I am converting text to image. Some of the text is longer in length than others.
How do I make sure that none of the text is truncated?

The code below is limiting my bitmap to 250, 30.

System.Drawing.Bitmap imgIn = new System.Drawing.Bitmap(250, 30);
System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(imgIn);
g.Clear(System.Drawing.Color.White);
    System.Drawing.Font font = new System.Drawing.Font("Arial", 10, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Pixel);

I was following this example:How to convert Email Address or another text form TextBox to image

UPDATE

I found this article that helped accomplish my task: Generate Image from text using C# OR Convert Text in to Image using C#

After I was able to resize the image according to the text length, I discovered that I need to introduce line breaks in the text otherwise the image was going all the way to Timbuktu when
the text was a couple of sentences.
How do I introduce line breaks in long texts?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

执着的年纪 2024-08-21 02:40:46

您可以使用 TextRenderer.MeasureText 来获取文本的大小(以像素为单位)。

Size size = TextRenderer.MeasureText("text", Font("Arial",10));
System.Drawing.Bitmap imgIn = new System.Drawing.Bitmap(size.Width, size.Height);

编辑

我发现这篇关于如何编写HTTP的文章处理程序将执行您想要的操作,它甚至可以将文本换行以适合。

You can use TextRenderer.MeasureText to get the size in pixels of the text.

Size size = TextRenderer.MeasureText("text", Font("Arial",10));
System.Drawing.Bitmap imgIn = new System.Drawing.Bitmap(size.Width, size.Height);

EDIT

I found this article on how to write an HTTP Handler that will do what you want, it even wraps text to fit.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文