如何设置背景 None 颜色 ASP.NET C#
How To Set Background None Color ASP.NET C#
从文本生成图像
public 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;
}
和回调
Font font = new Font("Arial", 20, FontStyle.Bold);
Color color = Color.FromArgb(255, 0, 0); //Red
Color background = Color.Transparent; //None Color
Image img = DrawText("Hoàng Long", font, color, background);
img.Save(context.Response.OutputStream, ImageFormat.Jpeg);
尝试过:How to generate an image from text on fly at runtime
请帮帮我
免责声明:此代码将创建一个白色背景。不是透明的。要使其透明,您需要更改此
var image = DrawTextImage(TextForImage, font, Color.Black, Color.Transparent);
public class TextToImage
{
// main method you need to call.
public byte[] GetImageFromText(string TextForImage)
{
FontFamily fontFamily = new FontFamily("Arial");
Font font = new Font(fontFamily, 40.0f);
var image = DrawTextImage(TextForImage, font, Color.Black, Color.Transparent);
var imgData = ImageToByteArray(image);
return imgData;
}
// convert image to byte array
private byte[] ImageToByteArray(System.Drawing.Image imageIn)
{
MemoryStream ms = new MemoryStream();
imageIn.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
return ms.ToArray();
}
private Image DrawTextImage(String text, Font font, Color textColor, Color backColor)
{
return DrawTextImage(text, font, textColor, backColor, new Size(500, 500));
}
// create image
private Image DrawTextImage(String text, 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(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);
Image retImg = new Bitmap(500, 500);
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.TextRenderingHint = TextRenderingHint.AntiAlias;
RectangleF rectF1 = new RectangleF(10, 10, 490, 490);
drawing.DrawString(text, font, textBrush, rectF1);
drawing.Save();
}
}
return retImg;
}
}
我认为上面的代码是不言自明的。您只需创建此助手的一个实例 class 并传递文本参数。
TextToImage textToImage = new TextToImage();
byte[] byteArr = textToImage.GetImageFromText("some text to create image out of it");
希望这对您有所帮助。 :)
从文本生成图像
public 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;
}
和回调
Font font = new Font("Arial", 20, FontStyle.Bold);
Color color = Color.FromArgb(255, 0, 0); //Red
Color background = Color.Transparent; //None Color
Image img = DrawText("Hoàng Long", font, color, background);
img.Save(context.Response.OutputStream, ImageFormat.Jpeg);
尝试过:How to generate an image from text on fly at runtime
请帮帮我
免责声明:此代码将创建一个白色背景。不是透明的。要使其透明,您需要更改此
var image = DrawTextImage(TextForImage, font, Color.Black, Color.Transparent);
public class TextToImage
{
// main method you need to call.
public byte[] GetImageFromText(string TextForImage)
{
FontFamily fontFamily = new FontFamily("Arial");
Font font = new Font(fontFamily, 40.0f);
var image = DrawTextImage(TextForImage, font, Color.Black, Color.Transparent);
var imgData = ImageToByteArray(image);
return imgData;
}
// convert image to byte array
private byte[] ImageToByteArray(System.Drawing.Image imageIn)
{
MemoryStream ms = new MemoryStream();
imageIn.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
return ms.ToArray();
}
private Image DrawTextImage(String text, Font font, Color textColor, Color backColor)
{
return DrawTextImage(text, font, textColor, backColor, new Size(500, 500));
}
// create image
private Image DrawTextImage(String text, 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(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);
Image retImg = new Bitmap(500, 500);
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.TextRenderingHint = TextRenderingHint.AntiAlias;
RectangleF rectF1 = new RectangleF(10, 10, 490, 490);
drawing.DrawString(text, font, textBrush, rectF1);
drawing.Save();
}
}
return retImg;
}
}
我认为上面的代码是不言自明的。您只需创建此助手的一个实例 class 并传递文本参数。
TextToImage textToImage = new TextToImage();
byte[] byteArr = textToImage.GetImageFromText("some text to create image out of it");
希望这对您有所帮助。 :)