首页 > 代码库 > 根据传入的文字生成图片 并保存

根据传入的文字生成图片 并保存

很简单的代码,记录下,防止忘记。

实现过程和生成验证码相似

public void SavePhoneImg(string Phone, string path)        {            System.Drawing.Bitmap image = new System.Drawing.Bitmap(135, 25);            Graphics g = Graphics.FromImage(image);            try            {                Font font = new System.Drawing.Font("楷体", 15, (System.Drawing.FontStyle.Bold));                System.Drawing.Drawing2D.LinearGradientBrush brush = new System.Drawing.Drawing2D.LinearGradientBrush(new Rectangle(5, 5, image.Width, image.Height), Color.Black, Color.Black, 1.2f, true);                g.DrawString(Phone, font, brush, 2, 0);                //画图片的边框线                 g.DrawRectangle(new Pen(Color.Silver), 0, 0, image.Width - 1, image.Height - 1);                System.IO.FileStream ms = new System.IO.FileStream(path, FileMode.Create);                image.Save(ms, System.Drawing.Imaging.ImageFormat.Png);            }            finally            {                g.Dispose();                image.Dispose();            }        }
代码