任何人都可以指导如何从输入文本生成图像。图片可能有任何扩展名并不重要。在影像上呈现文本如何在运行时从文本生成图像
47
A
回答
123
好吧,假设你想绘制在C#中的图像上的绳子,你将需要在这里使用System.Drawing命名空间:
private Image DrawText(String text, Font font, Color textColor, Color backColor)
{
//first, create a dummy bitmap just to get a graphics object
Image img = new Bitmap(1, 1);
Graphics drawing = Graphics.FromImage(img);
//measure the string to see how big the image needs to be
SizeF textSize = drawing.MeasureString(text, font);
//free up the dummy image and old graphics object
img.Dispose();
drawing.Dispose();
//create a new image of the right size
img = new Bitmap((int) textSize.Width, (int)textSize.Height);
drawing = Graphics.FromImage(img);
//paint the background
drawing.Clear(backColor);
//create a brush for the text
Brush textBrush = new SolidBrush(textColor);
drawing.DrawString(text, font, textBrush, 0, 0);
drawing.Save();
textBrush.Dispose();
drawing.Dispose();
return img;
}
此代码将先测量串,然后创建一个正确大小的图像。
如果你想保存这个函数的返回值,只需调用返回图像的Save方法即可。
3
使用imagemagick(在服务器上)
既然你是在C#中,你也可以使用直接位图和字体操纵的.NET类(使用类,如:System.Drawing.Bitmap
和System.Drawing.Graphics
)
3
我刚刚将此answer中提到的这种方法转换为VB.NET方法。也许这有助于某人。
Public Function DrawText(ByVal text As String, ByRef font As Font, ByRef textColor As Color, ByRef backColor As Color) As Image
' first, create a dummy bitmap just to get a graphics object
Dim img As Image = New Bitmap(1, 1)
Dim drawing As Graphics = Graphics.FromImage(img)
' measure the string to see how big the image needs to be
Dim textSize As SizeF = drawing.MeasureString(Text, Font)
' free up the dummy image and old graphics object
img.Dispose()
drawing.Dispose()
' create a new image of the right size
img = New Bitmap(CType(textSize.Width, Integer), CType(textSize.Height, Integer))
drawing = Graphics.FromImage(img)
' paint the background
drawing.Clear(BackColor)
' create a brush for the text
Dim textBrush As Brush = New SolidBrush(textColor)
drawing.DrawString(text, font, textBrush, 0, 0)
drawing.Save()
textBrush.Dispose()
drawing.Dispose()
Return img
End Function
编辑:固定错字。
+0
谢谢弗雷迪,你为我节省了很多精力。 – BedfordNYGuy 2016-10-21 16:26:50
1
F#
版本:
open System.Drawing
let drawText text font textColor backColor =
let size =
use dummyImg = new Bitmap(1, 1)
use drawing = Graphics.FromImage(dummyImg)
drawing.MeasureString(text, font)
let img = new Bitmap((int size.Width), (int size.Height))
use drawing = Graphics.FromImage(img)
use textBrush = new SolidBrush(textColor)
do
drawing.Clear(backColor)
drawing.DrawString(text, font, textBrush, PointF())
drawing.Save() |> ignore
img
3
感谢Kazar。以前的答案的略有改善使用使用使用后的图像/图形对象的
private Image DrawTextImage(String currencyCode, Font font, Color textColor, Color backColor) {
return DrawTextImage(currencyCode, font, textColor, backColor, Size.Empty);
}
private Image DrawTextImage(String currencyCode, Font font, Color textColor, Color backColor, Size minSize) {
//first, create a dummy bitmap just to get a graphics object
SizeF textSize;
using (Image img = new Bitmap(1, 1)) {
using (Graphics drawing = Graphics.FromImage(img)) {
//measure the string to see how big the image needs to be
textSize = drawing.MeasureString(currencyCode, font);
if (!minSize.IsEmpty) {
textSize.Width = textSize.Width > minSize.Width ? textSize.Width : minSize.Width;
textSize.Height = textSize.Height > minSize.Height ? textSize.Height : minSize.Height;
}
}
}
//create a new image of the right size
Image retImg = new Bitmap((int)textSize.Width, (int)textSize.Height);
using (var drawing = Graphics.FromImage(retImg)) {
//paint the background
drawing.Clear(backColor);
//create a brush for the text
using (Brush textBrush = new SolidBrush(textColor)) {
drawing.DrawString(currencyCode, font, textBrush, 0, 0);
drawing.Save();
}
}
return retImg;
}
相关问题
- 1. 何时在运行时生成图标?
- 2. 如何在运行时生成图像查看
- 3. 如何在运行时生成的QML中加载图像(jpg)
- 4. 选择图像时生成UILabel文本
- 5. 如何在每次运行autotools时运行脚本(用于版本号生成)
- 6. 如何在列表视图中生成文本为图像ItemTemplate
- 7. 如何生成包含图标的.res文件运行时
- 8. 在运行时从wsdl生成JAVA类
- 9. 我如何在运行时生成SQL脚本在VB6.0
- 10. Rails:在运行时生成.js文件
- 11. java在运行时生成类文件
- 12. 显示在ASP.NET MVC运行时生成的图像:ASHX VS ImageResult
- 13. 在MVC中显示运行时生成的图像ASP.NET
- 14. 如何在图像上运行脚本?
- 15. 请求时在运行时生成缩略图图像,或在硬盘中预先生成缩略图?
- 16. 在运行时生成
- 17. 如何在运行时在HADOOP中生成多个文件名?
- 18. 使用Cocos2D在运行时生成图形 - 如何显示?
- 19. 如何在运行时加载图像?
- 20. 如何在运行时更改图像?
- 21. 如何在运行时从代码生成datagrid?
- 22. 如何插入图像到WPF RichTextBox在文本之间的运行时间,使文本漂浮在图像
- 23. 在运行时从Jenkins Workflow脚本获取生成日志?
- 24. 如何从php中生成QR图像
- 25. 如何从jdl生成uml图像
- 26. 如何在生成图像后生成图像(Python成像,Plone 3)
- 27. 如何在运行时生成操作文档?
- 28. 如何在C#中运行时生成.exe文件
- 29. 如何运行由JDK 9 jlink生成的图像?
- 30. 如果.designer.cs在运行时生成?
你的意思是像你这样的形象会从屏幕捕捉得到处置和引进分钟大小参数的?当然,*一些*格式/扩展会比其他格式更好。 – pavium 2010-01-15 08:52:49
你的意思是什么样的文本输入? – 2010-01-15 08:53:06
不,它不是屏幕截图,我们有输入文本框,我们正在使用C# – Ravia 2010-01-15 09:06:45