public IActionResult CreateIntCode() { string code = new Random().Next(999999).ToString("000000"); using (Bitmap image = new Bitmap(76, 32)) { Random random = new Random(); int min = 70, max = 170; Color bg; using (Graphics g = Graphics.FromImage(image)) { int bad = random.Next(3);//总有一个颜色分量可能偏深色 bg = Color.FromArgb(random.Next(bad == 0 ? 120 : 200, 255), random.Next(bad == 1 ? 120 : 200, 255), random.Next(bad == 2 ? 120 : 200, 255)); g.Clear(bg); Font font = new Font("Arial", 18, FontStyle.Bold); Color c1 = Color.FromArgb(random.Next(min, max), random.Next(min, max), random.Next(min, max)); Color c2 = Color.FromArgb(random.Next(min, max), random.Next(min, max), random.Next(min, max)); //字体由上到下颜色渐变 LinearGradientBrush brush = new LinearGradientBrush(new Rectangle(0, 0, 10, 22), c1, c2, 90, true); int x = random.Next(-8, -1); for (int i = 0; i < code.Length; i++) { Matrix transform = g.Transform; //上下波动 transform.Shear(0, Convert.ToSingle((random.NextDouble() - 0.5) / 5)); g.Transform = transform; x += random.Next(9, 11); g.DrawString(code.Substring(i, 1), font, brush, x, 3); g.ResetTransform(); } } using (Bitmap destBmp = new Bitmap(image.Width, image.Height)) { using (Graphics g = Graphics.FromImage(destBmp)) { // 填充位图背景 g.FillRectangle(new SolidBrush(bg), 0, 0, destBmp.Width, destBmp.Height); double dBaseAxisLen = (double)destBmp.Height; double dPhase = 0; for (int i = 0; i < destBmp.Width; i++) { //干扰线 //if ((i % 15) == 1) dPhase = random.NextDouble() * Math.PI * 2; for (int j = 0; j < destBmp.Height; j++) { double dx = Math.PI * j * 2 / dBaseAxisLen + dPhase; double dy = Math.Sin(dx); // 取得当前点的颜色 int nOldX = i + (int)(dy * 3); int nOldY = j; Color color = image.GetPixel(i, j); if (nOldX >= 0 && nOldX < destBmp.Width && nOldY >= 0 && nOldY < destBmp.Height) { destBmp.SetPixel(nOldX, nOldY, color); } } } g.SmoothingMode = SmoothingMode.HighSpeed; }; MemoryStream ms = new MemoryStream(); destBmp.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp); byte[] b = new byte[ms.Length]; ms.Position = 0; ms.Read(b, 0, Convert.ToInt32(ms.Length)); ms.Close(); return File(b, "image/jpeg"); } } }
code也可以用上一个的GetRandomString方法获取随机字符串后,做字符验证码,不过,转字符流的时候,用Image即可,destBmp有点难以识别。