using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Configuration;
using System.Drawing.Drawing2D;
namespace 验证码
{
    public partial class Form1 : Form
    {
        Image pbImg = null;
        Rectangle rect = new Rectangle(50, 50, 100, 100);        public Form1()
        {
            InitializeComponent();
            pbImg = new Bitmap(pictureBox1.Width, pictureBox1.Height);
            pictureBox1.Image = pbImg;        }
        
       /* private void Page_Load(object sender, System.EventArgs e)
        {
           this.CreateImage(GenerateCheckCode());
             
            
        }*/
        private string GenerateCheckCode()
            {
                int number;
                char code;
                string checkCode = String.Empty;                System.Random random = new Random();
                for (int i = 0; i < 4; i++)
                {
                    number = random.Next();
                    code = (char)('0' + (char)(number % 10));
                    if (number % 2 == 0)
                        code = (char)('0' + (char)(number % 10));
                    else
                        code = (char)('A' + (char)(number % 26));//数字加英文字
                    checkCode += code.ToString();
                }
                return checkCode;
            }
            private void CreateImage(string checkCode)
            {
                //int iwidth = (int)(checkCode.Length * 13);
                //System.Drawing.Bitmap image = new System.Drawing.Bitmap(iwidth, 25);
                Graphics g = Graphics.FromImage(pbImg);
                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 < 2; i++)
                {
                    int x = rand.Next(pbImg.Width);
                    int y = rand.Next(pbImg.Height);
                    g.DrawRectangle(new Pen(Color.LightGray, 0), x, y, 1, 1);
                }            //输出不同字体和颜色的验证码字符
                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), ii);
                  }
            //画一个边框
                g.DrawRectangle(new Pen(Color.Black, 0), 0, 0, pbImg.Width - 2, pbImg.Height - 2);
               
                g.Dispose();
            }                       private void pictureBox1_Click(object sender, EventArgs e)
            {
               this.CreateImage(GenerateCheckCode());
               this.pictureBox1.Refresh();
                
            }         
        }
}
我的代码如上,希望大神们可以帮我改一下,能直接把图形运行出来

解决方案 »

  1.   

    最简单就是下面这样了加红色的2行,不过,我认为在Form1.Designer.cs写上一个构造函数或者将代码加入InitializeComponent()函数也行,呵呵public Form1()
             {
                 InitializeComponent();
                 pbImg = new Bitmap(pictureBox1.Width, pictureBox1.Height);
                 pictureBox1.Image = pbImg;
               this.CreateImage(GenerateCheckCode());
               this.pictureBox1.Refresh();

            }