欢迎光临
我们一直在努力

使用 Visual C# .NET 向 Excel 工作簿传输数据-.NET教程,Asp.Net开发

建站超值云服务器,限时71元/月

/* copyright all(c) 2005 zhongfeng, http://blog.csdn.net/sw515 */
 public class validatecode : system.web.ui.page
 {
  private void page_load(object sender, system.eventargs e)
  {
   this.createcheckcodeimage(generatecheckcode());
  }

  #region web 窗体设计器生成的代码
  override protected void oninit(eventargs e)
  {
   //
   // codegen: 该调用是 asp.net web 窗体设计器所必需的。
   //
   initializecomponent();
   base.oninit(e);
  }
  
  /// <summary>
  /// 设计器支持所需的方法 – 不要使用代码编辑器修改
  /// 此方法的内容。
  /// </summary>
  private void initializecomponent()
  {   
   this.load += new system.eventhandler(this.page_load);
  }
  #endregion

  private string generatecheckcode()
  {
   int number;
   char code;
   string checkcode = string.empty;

   system.random random = new random();

   for(int i=0; i<5; i++)
   {
    number = random.next();

    if(number % 2 == 0)
     code = (char)(0 + (char)(number % 10));
    else
     code = (char)(a + (char)(number % 26));

    checkcode += code.tostring();
   }

   response.cookies.add(new httpcookie(“checkcode”, checkcode));

   return checkcode;
  }

  private void createcheckcodeimage(string checkcode)
  {
   if(checkcode == null || checkcode.trim() == string.empty)
    return;

   system.drawing.bitmap image = new system.drawing.bitmap((int)math.ceiling((checkcode.length * 12.5)), 22);
   graphics g = graphics.fromimage(image);

   try
   {
    //生成随机生成器
    random random = new random();

    //清空图片背景色
    g.clear(color.white);

    //画图片的背景噪音线
    for(int i=0; i<25; i++)
    {
     int x1 = random.next(image.width);
     int x2 = random.next(image.width);
     int y1 = random.next(image.height);
     int y2 = random.next(image.height);

     g.drawline(new pen(color.silver), x1, y1, x2, y2);
    }

    font font = new system.drawing.font(“arial”, 12, (system.drawing.fontstyle.bold | system.drawing.fontstyle.italic));
    system.drawing.drawing2d.lineargradientbrush brush = new system.drawing.drawing2d.lineargradientbrush(new rectangle(0, 0, image.width, image.height), color.blue, color.darkred, 1.2f, true);
    g.drawstring(checkcode, font, brush, 2, 2);

    //画图片的前景噪音点
    for(int i=0; i<100; i++)
    {
     int x = random.next(image.width);
     int y = random.next(image.height);

     image.setpixel(x, y, color.fromargb(random.next()));
    }

    //画图片的边框线
    g.drawrectangle(new pen(color.silver), 0, 0, image.width – 1, image.height – 1);

    system.io.memorystream ms = new system.io.memorystream();
    image.save(ms, system.drawing.imaging.imageformat.gif);
    response.clearcontent();
    response.contenttype = “image/gif”;
    response.binarywrite(ms.toarray());
   }
   finally
   {
    g.dispose();
    image.dispose();
   }
  }
 }

赞(0)
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » 使用 Visual C# .NET 向 Excel 工作簿传输数据-.NET教程,Asp.Net开发
分享到: 更多 (0)