I hope that I will receive your help, thanks.
my code as below:
<asp:Image ID="imgCapcha" runat="server" Height="51px" Width="212px" />
<asp:UpdatePanel ID="UpdatePanel1" runat="server" >
<ContentTemplate >
<asp:ImageButton ID="imbReLoad" runat="server" Height="31px"
ImageUrl="~/hinhtrangtri/refresh.png" OnClick="imbReLoad_Click"
Width="33px" />
</ContentTemplate>
</asp:UpdatePanel>
and code at class codebehind:
public void TaoAnhCapcha()
{
string[] Fonts = { "Arial Black", "Arial" };
byte bLenght = 6;
string chars = "12345678ABCDEFGHIJKLMNOPQRSTUVWXYZabdeqptmnghr"; ;
Bitmap bmp = new Bitmap(125, 30);
Graphics gr = Graphics.FromImage(bmp);
HatchBrush brush = new HatchBrush(HatchStyle.Wave, Color.White, Color.Wheat);
gr.FillRegion(brush, gr.Clip);
StringBuilder strCapCha = new StringBuilder();
Random Rand = new Random();
int x = 1;
for (int i = 0; i < bLenght; i++)
{
string schar = chars[Rand.Next(chars.Length)].ToString();
strCapCha.Append(schar);
Font font = new Font(Fonts[Rand.Next(Fonts.Length)], 12, FontStyle.Bold | FontStyle.Italic);
SizeF size = gr.MeasureString(schar, font);
gr.DrawString(schar, font, Brushes.Blue, x * 2, Rand.Next(2, 10));
font.Dispose();
x += 10;
}
Session["captcha"] = strCapCha.ToString();
string path = "hinhtrangtri/" + Request.UserHostAddress + ".gif";
bmp.Save(Server.MapPath("") + "/" + path, System.Drawing.Imaging.ImageFormat.Gif);
imgCapcha.ImageUrl = path;
}
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
TaoAnhCapcha();
}
}
protected void imbReLoad_Click(object sender, ImageClickEventArgs e)
{
TaoAnhCapcha();
}