C# 异步获取验证码

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using CodeClassLibrary;
using System.IO;
using System.Net;

namespace ValidatorCodeDemo
{
    public partial class frmValidatorCode : Form
    {
        public frmValidatorCode()
        {
            InitializeComponent();
            this.GetCode();
        }

        private void btnRefresh_Click(object sender, EventArgs e)
        {
            this.GetCode();
        }

        /// <summary>
        /// 产生新的验证码
        /// </summary>
        private void GetCode()
        {
            try
            {
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://checkcode.taobao.com/auction/checkcode?sessionID=eb3f99fbe975258ea87bc24c235ab24f&r=1237173457015");
                Stream responseStream = ((HttpWebResponse)request.GetResponse()).GetResponseStream();
                Image original = Image.FromStream(responseStream);
                Bitmap bitMap = new Bitmap(original);
                this.pBoxCode.Image = bitMap;           //注意:替换为你的PictureBox控件名字
                responseStream.Close();

            }
            catch (Exception exception)
            {
                MessageBox.Show("ERROR:" + exception.Message);
            }
        }

    }
}

posted on 2009-11-20 11:15  云中深海  阅读(520)  评论(0编辑  收藏  举报