日期:2014-05-18  浏览次数:20803 次

求高手帮忙看下下面的方法是干嘛的,顺便每一行都标上注释,50分辛苦费双手奉上
public void CreateImage(string str_ValidateCode)
  {
  int bmpWidth = str_ValidateCode.Length * 9;
  int bmpHight = 0x12;
  Random newRandom = new Random();
  Bitmap theBitmap = new Bitmap(bmpWidth, bmpHight);
  Graphics theGraphics = Graphics.FromImage(theBitmap);
  theGraphics.Clear(Color.White);
  Font theFont = new Font("Verdana", 9f);
  for (int int_index = 0; int_index < str_ValidateCode.Length; int_index++)
  {
  string str_char = str_ValidateCode.Substring(int_index, 1);
  Brush newBrush = new SolidBrush(this.GetRandomColor());
  Point thePos = new Point(int_index * 8, 2);
  theGraphics.DrawString(str_char, theFont, newBrush, (PointF) thePos);
  }
  MemoryStream ms = new MemoryStream();
  theBitmap.Save(ms, ImageFormat.Png);
  base.Response.ClearContent();
  base.Response.ContentType = "image/Png";
  base.Response.BinaryWrite(ms.ToArray());
  theGraphics.Dispose();
  theBitmap.Dispose();
  base.Response.End();
  }

------解决方案--------------------
C# code

        public void CreateImage(string str_ValidateCode)
        {
            int bmpWidth = str_ValidateCode.Length * 9;//str_ValidateCode的长度乘9
            int bmpHight = 0x12;
            Random newRandom = new Random();//随机数
            Bitmap theBitmap = new Bitmap(bmpWidth, bmpHight);//新建一个bitmap对象
            Graphics theGraphics = Graphics.FromImage(theBitmap);//获得上面对象的graphic
            theGraphics.Clear(Color.White);//填充白底色
            Font theFont = new Font("Verdana", 9f);//字体
            //下面是在theGraphics上面画文字
            for (int int_index = 0; int_index < str_ValidateCode.Length; int_index++)
            {
                string str_char = str_ValidateCode.Substring(int_index, 1);
                Brush newBrush = new SolidBrush(this.GetRandomColor());
                Point thePos = new Point(int_index * 8, 2);
                theGraphics.DrawString(str_char, theFont, newBrush, (PointF)thePos);
            }
            MemoryStream ms = new MemoryStream();//创建内存流
            theBitmap.Save(ms, ImageFormat.Png);//位图保存至流

            //下面调用基类的方法,自己查去
            base.Response.ClearContent();
            base.Response.ContentType = "image/Png";
            base.Response.BinaryWrite(ms.ToArray());
            theGraphics.Dispose();//释放
            theBitmap.Dispose();//释放
            base.Response.End();
        }