新增图片
public static byte[] CreateImage(string strRandom)
{
Bitmap newBitmap = new Bitmap(strRandom.Length * 20, 38);
Graphics g = Graphics.FromImage(newBitmap);
g.Clear(Color.White);
在图片上绘制文字
SolidBrush solidBrush = new SolidBrush(Color.Red);
g.DrawString(strRandom, new Font("Aril", 18), solidBrush, 12, 4);
在图片上绘制干扰线
Random random = new Random();
产生一条线,并绘制到画布。 起始点(x,y) 总结点
for (int i = 0; i < 10; i++)
{
int x1 = random.Next(newBitmap.Width);
int y1 = random.Next(newBitmap.Height);
int x2 = random.Next(newBitmap.Width);
int y2 = random.Next(newBitmap.Height);
g.DrawLine(new Pen(Color.DarkGray), x1, y1, x2, y2);
}
绘制图片的前景干扰点
for (int i = 0; i < 100; i++)
{
int x = random.Next(newBitmap.Width);
int y = random.Next(newBitmap.Height);
newBitmap.SetPixel(x, y, Color.FromArgb(random.Next()));
}
在最外边绘制边框
g.DrawRectangle(new Pen(Color.Blue), 0, 0, newBitmap.Width, newBitmap.Height);
g.DrawRectangle(new Pen(Color.Blue), -1, -1, newBitmap.Width, newBitmap.Height);
将图转保存到内存流中
MemoryStream ms = new MemoryStream();
newBitmap.Save(ms, ImageFormat.Jpeg);
return ms.ToArray();
将流内容写入byte数组返回
}