public byte[] ChangeStringToImage(string pic)
{
try
{
//判断字符串不等于空和null
if (pic == null || pic.Trim() == String.Empty)
{
return null;
}
//创建一个位图对象
Bitmap image = new Bitmap((int)Math.Ceiling((pic.Length * 20.0)), 20);
//创建Graphics
Graphics g = Graphics.FromImage(image);
try
{
//清空图片背景颜色
g.Clear(Color.White);
Font font = new Font("Arial", 8f, (FontStyle.Regular));
System.Drawing.Drawing2D.LinearGradientBrush brush = new System.Drawing.Drawing2D.LinearGradientBrush(new Rectangle(0, 0, image.Width, image.Height), Color.Black, Color.DarkRed, 1.2f, true);
g.DrawString(pic, font, brush, 2, 2);
//画图片的边框线
// g.DrawRectangle(new Pen(Color.Silver), 0, 0, image.Width - 1, image.Height - 1);
//image.Save("d:/001.jpg");
ImageFormat format = image.RawFormat;
using (MemoryStream ms = new MemoryStream())
{
image.Save(ms, ImageFormat.Png);
byte[] buffer = new byte[ms.Length];
//Image.Save()会改变MemoryStream的Position,需要重新Seek到Begin
ms.Seek(0, SeekOrigin.Begin);
ms.Read(buffer, 0, buffer.Length);
return buffer;
}
}
finally
{
g.Dispose();
image.Dispose();
}
}
catch (Exception)
{
return null;
}
}
convert Text To Image
猜你喜欢
转载自blog.csdn.net/lz37025/article/details/79897861
今日推荐
周排行