- ///<summary>
- /// 画图片的背景图,干扰
- /// </summary>
- /// <param name="checkCode"></param>
- /// <returns></returns>
- private Bitmap CreateImages(string checkCode,string type)
- {
- int step=0;
- if(type=="ch")
- {
- step=5;//中文字符比较大,所以字距要比较大
- }
- int iwidth = (int)(checkCode.Length * (13 + stepw));
- System.Drawing.Bitmap image = new System.Drawing.Bitmap(iwidth, 22);
- Graphics g = Graphics.FromImage(image);
- g.Clear(Color.White);//清除背景色
- Color[] c = { Color.Black, Color.Red, Color.DarkBlue, Color.Green, Color.Orange, Color.Brown, Color.DarkCyan, Color.Purple };//定义随机颜色
- string[] font = { "Verdana", "Microsoft Sans Serif", "Comic Sans MS", "Arial", "宋体" };
- Random rand = new Random();
- for (int i = 0; i < 50; i++)
- {
- int x1 = rand.Next(image.Width);
- int x2 = rand.Next(image.Width);
- int y1 = rand.Next(image.Height);
- int y2 = rand.Next(image.Height);
- g.DrawLine(new Pen(Color.LightGray,1), x1,y1,x2,y2);//根据坐标画线
- }
- for (int i = 0; i < checkCode.Length; i++)
- {
- int cindex = rand.Next(7);
- int findex = rand.Next(5);
- Font f = new System.Drawing.Font(font[findex], 10, System.Drawing.FontStyle.Bold);
- Brush b = new System.Drawing.SolidBrush(c[cindex]);
- int ii = 4;
- if ((i + 1) % 2 == 0)
- {
- ii = 2;
- }
- g.DrawString(checkCode.Substring(i, 1), f, b, 3 + (i * (12 + stepw)), ii);
- }
- g.DrawRectangle(new Pen(Color.Black, 0), 0, 0, image.Width - 1, image.Height - 1);
- System.IO.MemoryStream ms = new System.IO.MemoryStream();
- return image;
- }
- //该片段来自于http://www.codesnippet.cn/detail/0304201512095.html
来源: http://www.codesnippet.cn/detail/0304201512095.html