仿长微薄工具,字符串转为图片
1 ///<summary>
2 /// 字符串转为图片
3 ///</summary>
4 ///<param name="str">字符</param>
5 ///<param name="width">宽度</param>
6 ///<param name="borderColor">边框颜色</param>
7 ///<param name="foreColor">字体颜色</param>
8 ///<param name="backgroundColor">背景颜色</param>
9 ///<returns>返回图片</returns>
10 public static Bitmap StringToImage(string str, int width, Color borderColor, Color foreColor, Color backgroundColor)
11 {
12 Bitmap bitmap = new Bitmap(width, 500);
13 Graphics g = Graphics.FromImage(bitmap);
14 Font f = new Font("宋体", 11);
15 str = str + "\r\n------------------------------------";
16 str = str + "\r\n本图片由老驴生成 http://www.lanmiao.com";
17 Size size = g.MeasureString(str, f, width - 30).ToSize();
18 Bitmap result = new Bitmap(width, size.Height + 30);
19 g = Graphics.FromImage(result);
20 g.FillRectangle(new SolidBrush(backgroundColor), 0, 0, width, size.Height + 30);
21 g.DrawRectangle(new Pen(borderColor), new Rectangle(15, 10, width - 30, size.Height + 10));
22
23 g.DrawString(str, f, new SolidBrush(foreColor), new Rectangle(25, 15, width - 40, size.Height));
24 return result;
25 }