public VerifyImageInfo GenerateImage(string code, int width, int height, Color bgcolor, int textcolor)
        {
            VerifyImageInfo verifyimage = new VerifyImageInfo();
            verifyimage.ImageFormat = ImageFormat.Jpeg;
            verifyimage.ContentType = "image/pjpeg";            Bitmap bitmap = new Bitmap(width, height, PixelFormat.Format32bppArgb);            Graphics g = Graphics.FromImage(bitmap);
            Rectangle rect = new Rectangle(0, 0, width, height);
            g.SmoothingMode = SmoothingMode.AntiAlias;            g.Clear(bgcolor);            //int emSize = Next(3) + 15;//(int)((width - 20) * 2 / text.Length);
            //FontFamily family = fonts[Next(fonts.Length - 1)];
            //Font font = new Font(family, emSize, FontStyle.Bold);            //SizeF measured = new SizeF(0, 0);
            //SizeF workingSize = new SizeF(width, height);
            //while (emSize > 2 && (measured = g.MeasureString(code, font)).Width > workingSize.Width || measured.Height > workingSize.Height)
            //{
            //    font.Dispose();
            //    font = new Font(family, emSize -= 2);
            //}
            int fixedNumber = textcolor == 2 ? 60 : 0;            SolidBrush drawBrush = new SolidBrush(Color.FromArgb(Next(100), Next(100), Next(100)));
            for (int x = 0; x < 3; x++)
            {
                Pen linePen = new Pen(Color.FromArgb(Next(150) + fixedNumber, Next(150) + fixedNumber, Next(150) + fixedNumber),1);
                g.DrawLine(linePen, new PointF(0.0F + Next(20), 0.0F + Next(height)), new PointF(0.0F + Next(width), 0.0F + Next(height)));
            }
            Matrix m = new Matrix();
            for (int x = 0; x < code.Length; x++)
            {
                m.Reset();
                m.RotateAt(Next(30) - 15, new PointF(Convert.ToInt64(width * (0.10 * x)), Convert.ToInt64(height * 0.5)));
                g.Transform = m;
                drawBrush.Color = Color.FromArgb(Next(150) + fixedNumber + 20, Next(150) + fixedNumber + 20, Next(150) + fixedNumber + 20);
                PointF drawPoint = new PointF(0.0F + Next(4) + x * 20, 3.0F + Next(3));
                g.DrawString(Next(1) == 1 ? code[x].ToString() : code[x].ToString().ToUpper(), fonts[Next(fonts.Length - 1)], drawBrush, drawPoint);
                g.ResetTransform();
            }            double distort = Next(5, 10) * (Next(10) == 1 ? 1 : -1);            using (Bitmap copy = (Bitmap)bitmap.Clone())
            {
                for (int y = 0; y < height; y++)
                {
                    for (int x = 0; x < width; x++)
                    {
                        int newX = (int)(x + (distort * Math.Sin(Math.PI * y / 90)));
                        int newY = (int)(y + (distort * Math.Cos(Math.PI * x / 80)));
                        if (newX < 0 || newX >= width)
                            newX = 0;
                        if (newY < 0 || newY >= height)
                            newY = 0;
                        bitmap.SetPixel(x, y, copy.GetPixel(newX, newY));
                    }
                }
            }
            //g.DrawRectangle(new Pen(Color.Silver), 0, 0, bitmap.Width - 1, bitmap.Height - 1);                       drawBrush.Dispose();
            g.Dispose();            verifyimage.Image = bitmap;            return verifyimage;
        }

解决方案 »

  1.   

    double distort = Next(5, 10) * (Next(10) == 1 ? 1 : -1);            using (Bitmap copy = (Bitmap)bitmap.Clone())
                {
                    for (int y = 0; y < height; y++)
                    {
                        for (int x = 0; x < width; x++)
                        {
                            int newX = (int)(x + (distort * Math.Sin(Math.PI * y / 90)));
                            int newY = (int)(y + (distort * Math.Cos(Math.PI * x / 80)));
                            if (newX < 0 || newX >= width)
                                newX = 0;
                            if (newY < 0 || newY >= height)
                                newY = 0;
                            bitmap.SetPixel(x, y, copy.GetPixel(newX, newY));
                        }
                    }
                }
    这里在处理扭曲,distort是扭曲系数,newX,newY是扭曲以后的坐标