netcore get a verification code

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))
                {
                    intRandom.Next = Bad ( . 3 ); // always have a dark color component may be biased 
                    BG = Color.FromArgb (Random.Next (Bad == 0 ? 120 : 200 is , 255 ), Random.Next (Bad == . 1 ? 120 : 200 is , 255 ), Random.Next (Bad == 2 ? 120 : 200 is , 255 )); 
                    g.Clear (BG); 
                    the font font = new new the font ( " Arial " , 18 is  , 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 ++ ) 
                        { 
                            // interference line
                             // IF ((I 15%). 1 ==) dPhase = random.NextDouble () * * 2 Math.PI; 
                            for ( int J = 0 ; J <destBmp.Height; J ++ ) 
                            { 
                                Double DX Math.PI * J * = 2 / + dBaseAxisLen dPhase;
                                 Double Dy = Math.Sin (DX); 

                                // obtain the current color point 
                                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 also can be used a method of GetRandomString after obtaining random string, character codes do, however, when the character stream turn, can use Image, destBmp somewhat difficult to identify.

Guess you like

Origin www.cnblogs.com/huanyun/p/11265093.html