询问者
Window Form 中制作验证码

问题
全部回复
-
这段代码会生成一个图片,将图片bmpBytes放到picture box中显示出来
Bitmap bmpOut = new Bitmap(200, 50);
Graphics g = Graphics.FromImage(bmpOut);
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
g.FillRectangle(Brushes.Black, 0, 0, 200, 50);
g.DrawString(context.Session["Captcha"].ToString(), new Font("Verdana", 18), new SolidBrush(Color.White), 0, 0);
MemoryStream ms = new MemoryStream();
bmpOut.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
byte[] bmpBytes = ms.GetBuffer();
bmpOut.Dispose();
ms.Close();
其它的参考例子
https://www.c-sharpcorner.com/UploadFile/6f0898/simple-logic-to-generate-human-verification-captcha-code-in/
Random random = new Random (); // get a random instance
value = random.Next(10000, 99999); // get a random value between any range
var image = new Bitmap(this.pictureBox1.Width, this.pictureBox1.Height); // Get a bitmap
var font = new Font("TimesNewRoman", 25, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Pixel); // Get a font
var graphics = Graphics.FromImage(image); // Get a graphics with the bitmap image
graphics.DrawString(value.ToString (), font, Brushes.Red, new PointF(0, 0)); // Add the value in the graphics
Pen p = new Pen(Brushes.Orange, 2.0f); // get pen width
graphics.DrawLine(p, new PointF(0,this.pictureBox1 .Height), new Point(this.pictureBox1.Width,0)); // draw a diagonal line
graphics.DrawLine(p, new PointF(0,0), new Point(this.pictureBox1.Width , this.pictureBox1.Height)); // draw another diagonal line
p.Dispose(); // dispose the pen to avoid memory leak
graphics.SmoothingMode = SmoothingMode.AntiAlias; // Smoothing the pixel
graphics.TextRenderingHint = TextRenderingHint.AntiAlias; // Smoothing the text rendering because stem width may differ
this.pictureBox1.Image = image; // load the image in the picturebox
https://www.codeproject.com/Articles/99148/Simple-CAPTCHA-Create-your-own-in-C
专注于.NET ERP/CRM开发框架,C/S架构,SQL Server + ORM(LLBL Gen Pro) + Infragistics WinForms
-
请问在c#窗体中如何制作验证码呢?
Hi,
下面是一个简单的例子,你可以参考下:
public partial class FrmVerificationCode : Form { public FrmVerificationCode() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { CodeImage(CheckCode()); } private string CheckCode() { #region//生成随机数 int number; char code; string checkcode = string.Empty;//声明变量存储随机生成的4位英文或数字 Random random = new Random();//生成随机数 for (int i = 0; i < 4; i++) { number = random.Next();//返回非负随机数 if (number % 2 == 0)//判断数字是否为偶数 code = (char)('0' + (char)(number % 10)); else //如果不是偶数 code = (char)('A' + (char)(number % 26)); checkcode += "" + code.ToString(); //累加字符串 } return checkcode;//返回生成的字符串 #endregion } private void CodeImage(string checkcode) { #region//生成背景图 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);//创建Graphics对象 try { Random random = new Random();//生成随机生成器 g.Clear(Color.White);//清空图片背景色 for (int i = 0; i < 5; 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.Black), x1, y1, x2, y2);//把两点坐标用线连接起来 } Font font = new Font("Arial", 12, (System.Drawing.FontStyle.Bold));//指定字体大小和样式 g.DrawString(checkcode, font, new SolidBrush(Color.Red), 2, 2);//绘制文本字符串 for (int i = 0; i < 150; 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);//绘制矩形 this.pictureBox1.Width = image.Width;//设置pictureBox1的宽度 this.pictureBox1.Height = image.Height;//设置pictureBox1的高度 this.pictureBox1.BackgroundImage = image;//设置pictureBox1的背景图像 } catch (Exception ex) { MessageBox.Show(ex.Message); } #endregion } private void button1_Click(object sender, EventArgs e) { CodeImage(CheckCode()); } }
参考:http://blog.sina.com.cn/s/blog_6a7eff27010102fh.html
Regards,
Frankie
Note: This response contains a reference to a third party World Wide Web site. Microsoft is providing this information as a convenience to you. Microsoft does not control these sites and has not tested any software or information found on these sites; Therefore, Microsoft cannot make any representations regarding the quality, safety, or suitability of any software or information found there. There are inherent dangers in the use of any software found on the Internet, and Microsoft cautions you to make sure that you completely understand the risk before retrieving any software from the Internet.
MSDN Community Support
Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.