将文字转换为文字图片实现方法
//把文字转换成图片
if (!string.IsNullOrEmpty(present_no)) //如果文字不为空
{
try
{
if (!Directory.Exists(@sinpath))
{
Directory.CreateDirectory(@sinpath); //用于存在图片的路径不存在,则先生成。
}
//图片对象 初始化图片的宽度与高度
System.Drawing.Bitmap image = new System.Drawing.Bitmap(150, 30);
//画板
Graphics g = Graphics.FromImage(image);
//文本样式
//Font font = new System.Drawing.Font(“Arial”, 12, (System.Drawing.FontStyle.Bold | System.Drawing.FontStyle.Italic));
Font font = new System.Drawing.Font(“Arial”, 10, (System.Drawing.FontStyle.Regular));
//文本颜色与纹理
//System.Drawing.Drawing2D.LinearGradientBrush brush = new System.Drawing.Drawing2D.LinearGradientBrush(new Rectangle(0,0, image.Width, image.Height), Color.Red, Color.Red,0, true);
SolidBrush drawBrush = new SolidBrush(Color.Red); //文字颜色
g.FillRectangle(new SolidBrush(Color.White), 0, 0, image.Width, image.Height);
//g.DrawString(wordstring要绘制的文本, font, brush, 2, 2);
//image.Save(filename保存路径)
g.DrawString(pnName, font, drawBrush, 5, 5);
string path = sinpath + “\\” + present_no + “.jpg”;
image.Save(path, System.Drawing.Imaging.ImageFormat.Jpeg);
}
catch (Exception ex)
{
throw ex;
}
}