如何使用图像处理程序不受图像大小的限制
我正在将文本转换为图像。有些文本的长度比其他文本长。
如何确保没有任何文本被截断?
下面的代码将我的位图限制为 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用
TextRenderer.MeasureText
来获取文本的大小(以像素为单位)。编辑
我发现这篇关于如何编写HTTP的文章处理程序将执行您想要的操作,它甚至可以将文本换行以适合。
You can use
TextRenderer.MeasureText
to get the size in pixels of the text.EDIT
I found this article on how to write an HTTP Handler that will do what you want, it even wraps text to fit.
试试这个:http://msdn.microsoft。 com/en-us/library/system.drawing.graphics.measurestring.aspx 位于 System.Drawing.Graphics 上。
Try this one: http://msdn.microsoft.com/en-us/library/system.drawing.graphics.measurestring.aspx which is on System.Drawing.Graphics.