/// <summary>
/// 根據指定參數返回BitMap對象
/// 引用如下:
/// using System.Drawing;
/// 調用例子如下:
/// eg1、儲存為圖象檔案為
/// Bitmap srBmp = srBitmap(srs);
/// srBmp.Save(Directory.GetCurrentDirectory() + "\\srs.gif", System.Drawing.Imaging.ImageFormat.Gif);
/// srBmp.Dispose();
/// eg2。網頁中調用方式如下
/// Bitmap srBmp = srBitmap(srs);
/// System.IO.MemoryStream srMS = new System.IO.MemoryStream();
/// srBmp.Save(srMS,System.Drawing.Imaging.ImageFormat.Gif);
/// Response.ClearContent();
/// Response.ContentType = "image/gif";
/// Response.BinaryWrite(srMS.ToArray());
/// srBmp.Dispose();
/// </summary>
/// <param name="srs"></param>
/// <returns></returns>
public static Bitmap srBitmap(string srs)
{
//定義圖片彎曲的角度
int srseedangle = 45;
//定義圖象
Bitmap srBmp = new Bitmap(srs.Length*20,30);
//畫圖
Graphics srGraph = Graphics.FromImage(srBmp);
//清空圖象
srGraph.Clear(Color.AliceBlue);
//給圖象畫邊框
srGraph.DrawRectangle(new Pen(Color.Black,0),0,0,srBmp.Width-1,srBmp.Height-1);
//定義隨即數
Random srRandom = new Random();
//定義畫筆
Pen srPen = new Pen(Color.LightGray,0);
//畫噪點
for (int i = 0; i < 100; i++)
{
srGraph.DrawRectangle(srPen,srRandom.Next(1,srBmp.Width-2),srRandom.Next(1,srBmp.Height-2),1,1);
}
//將字串轉化為字元數組
char[] srchars = srs.ToCharArray();
//封狀文本
StringFormat srFormat = new StringFormat(StringFormatFlags.NoClip);
//設定文本垂直置中
srFormat.Alignment = StringAlignment.Center;
//設定文本水平置中
srFormat.LineAlignment = StringAlignment.Center;
//定義字型顏色
Color[] srColors ={
Color.Black,Color.Red,Color.DarkBlue,Color.Blue,Color.Orange,Color.Brown,Color.DarkCyan,Color.Purple};
//定義字型
string[] srFonts ={ "Verdana", "Microsoft Sans Serif", "Comic Sans MS", "Arial", "宋體" };
//迴圈畫出每個字元
for (int i = 0, j = srchars.Length; i < j; i++)
{
//定義字型 參數分別為字型樣式 字型大小 字型字形
Font srFont = new Font(srFonts[srRandom.Next(5)], srRandom.Next(12,20), FontStyle.Regular);
//填充圖形
Brush srBrush = new SolidBrush(srColors[srRandom.Next(7)]);
//定義座標
Point srPoint = new Point(16, 16);
//定義傾斜角度
float srangle = srRandom.Next(-srseedangle, srseedangle);
//傾斜
srGraph.TranslateTransform(srPoint.X, srPoint.Y);
srGraph.RotateTransform(srangle);
//填充字元
srGraph.DrawString(srchars[i].ToString(),srFont,srBrush,1,1,srFormat);
//迴歸正常
srGraph.RotateTransform(-srangle);
srGraph.TranslateTransform(2,-srPoint.Y);
}
srGraph.Dispose();
return srBmp;
}