C# 如何在运行时从文本动态生成图像
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2070365/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to generate an image from text on fly at runtime
提问by Ravia
Can anyone guide how to generate image from input text. Image might have any extension doesn't matter.
任何人都可以指导如何从输入文本生成图像。图像可能有任何扩展名都没有关系。
采纳答案by Alistair Evans
Ok, assuming you want to draw a string on an image in C#, you are going to need to use the System.Drawing namespace here:
好的,假设您想在 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;
}
This code will measure the string first, and then create an image of the correct size.
此代码将首先测量字符串,然后创建正确大小的图像。
If you want to save the return of this function, just call the Save method of the returned image.
如果要保存这个函数的返回值,只需调用返回图像的Save方法即可。
回答by Toad
use imagemagickfor rendering text on images (on the server)
使用imagemagick在图像上渲染文本(在服务器上)
Since you are in C# you could also use the .Net classes for bitmap and font manipulation directly (with the classes like: System.Drawing.Bitmap
and System.Drawing.Graphics
)
由于您使用的是 C#,您还可以直接使用 .Net 类进行位图和字体操作(使用类,例如:System.Drawing.Bitmap
和System.Drawing.Graphics
)
回答by Freddy
I just translated this method mentioned in this answerto a VB.NET method. Maybe this helps someone.
我只是将这个答案中提到的这个方法翻译成一个 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
Edit: Fixed typo.
编辑:修正错字。
回答by Dzmitry Lahoda
F#
version:
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
回答by Panayiotis Panayiotou
Thanks Kazar. A slight improvement of the previous answer to use USING for the dispose of the image/graphic objects after used and the introduction of min size param
谢谢卡扎尔。对使用 USING 处理使用后的图像/图形对象的先前答案略有改进,并引入了最小尺寸参数
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;
}
回答by Andrew Taylor
Here is Panayiotis' version of Kazar's answer with optional parameters and documentation, suitable for adding to a library class.
这是 Panayiotis 版本的 Kazar 答案,带有可选参数和文档,适合添加到库类中。
/// <summary>
/// Creates an image containing the given text.
/// NOTE: the image should be disposed after use.
/// </summary>
/// <param name="text">Text to draw</param>
/// <param name="fontOptional">Font to use, defaults to Control.DefaultFont</param>
/// <param name="textColorOptional">Text color, defaults to Black</param>
/// <param name="backColorOptional">Background color, defaults to white</param>
/// <param name="minSizeOptional">Minimum image size, defaults the size required to display the text</param>
/// <returns>The image containing the text, which should be disposed after use</returns>
public static Image DrawText(string text, Font fontOptional=null, Color? textColorOptional=null, Color? backColorOptional=null, Size? minSizeOptional=null)
{
Font font = Control.DefaultFont;
if (fontOptional != null)
font = fontOptional;
Color textColor = Color.Black;
if (textColorOptional != null)
textColor = (Color)textColorOptional;
Color backColor = Color.White;
if (backColorOptional != null)
backColor = (Color)backColorOptional;
Size minSize = Size.Empty;
if (minSizeOptional != null)
minSize = (Size)minSizeOptional;
//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(text, 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(text, font, textBrush, 0, 0);
drawing.Save();
}
}
return retImg;
}