Random 產生 4-6 碼英數字元,存入 Session["checkcode"]。Bitmap、Graphics,用 MeasureString 量測文字寬高,加上邊距決定最終尺寸。Bitmap、Graphics,DrawString 繪製文字 (設定 StringFormat 置中/靠左)。MemoryStream → Response.OutputStream,ContentType=image/png。public static bool CheckCode(string text)
{
string saved = HttpContext.Current.Session["checkcode"] as string;
return text == saved;
}
其他頁面 (如 login.aspx) 呼叫 checkimage.CheckCode(txtCode.Text) 即可驗證。
