C#生成验证码程序
做网站都必须有生成验证吗的地方,下面简单贴一段生成验证吗的程序

static void Main(string[] args) { try { Bitmap image = CreateCheckCodeImage(GetCheckCodeStr(4)); image.Save(@"img.jpg", System.Drawing.Imaging.ImageFormat.Jpeg); } catch (Exception ex) { Console.WriteLine(ex.ToString()); Console.ReadLine(); } } /// <summary> /// 生成一个随机的验证码 /// </summary> /// <param name="codeLength">需要生成的验证码长度</param> /// <returns>生成的验证码字符串</returns> private static string GetCheckCodeStr(int codeLength) { StringBuilder sb = new StringBuilder(); System.Random random = new Random(); //循环生成验证码 for (int i = 0; i < codeLength; i++) { if (random.Next() % 2 == 0) { sb.Append((char)('0' + (char)(random.Next() % 10))); } else { sb.Append((char)('A' + (char)(random.Next() % 26))); } } return sb.ToString(); } /// <summary> /// 创建一个验证码图片,并保存图片 /// </summary> /// <param name="checkCode">验证码字符串</param> private static Bitmap CreateCheckCodeImage(string checkCode) { //如果生成checkCode出现错误,返回异常 if (string.IsNullOrEmpty(checkCode)) throw new Exception("checkCode IsNullOrEmpty!"); System.Drawing.Bitmap image = new System.Drawing.Bitmap((int)Math.Ceiling((checkCode.Length * 26.0)), 40); Graphics g = Graphics.FromImage(image); try { //生成随机生成器 Random random = new Random(); //清空图片背景色 g.Clear(Color.White); //画图片的背景噪音线 for (int i = 0; i < 10; 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); } for (int index = 0; index < checkCode.Length; ++index) { Font font = new System.Drawing.Font(FontFamily.GenericSansSerif, 16 + random.Next(10), (System.Drawing.FontStyle.Regular | System.Drawing.FontStyle.Italic)); System.Drawing.Drawing2D.LinearGradientBrush brush = new System.Drawing.Drawing2D.LinearGradientBrush(new Rectangle(0, 0, image.Width, image.Height), Color.ForestGreen, Color.DeepSkyBlue, 1.2f, true); g.DrawString(checkCode[index].ToString(), font, brush, image.Width / checkCode.Length * index + random.Next(6) - 3, 0); } //画图片的前景噪音点 for (int i = 0; i < 30; i++) { int x = random.Next(image.Width); int y = random.Next(image.Height); image.SetPixel(x, y, Color.FromArgb(random.Next())); } //画图片的前景噪音线 for (int i = 0; i < 2; 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.White), x1, y1, x2, y2); } //画图片的边框线 g.DrawRectangle(new Pen(Color.Silver), 0, 0, image.Width - 1, image.Height - 1); return image; } catch (Exception ex) { throw new Exception("checkCode : " + checkCode + Environment.NewLine + ex.ToString()); } finally { g.Dispose(); } } }
插入一个片段,改变图片大小的程序段

try { while (true) { string fileName = Console.ReadLine(); Image image = Image.FromFile(fileName); double size = double.Parse(Console.ReadLine()); string result = Regex.Replace(fileName, @"(?=\.\w+$)", "1"); Image saveImg = new Bitmap(image, new Size((int)(image.Width * size), (int)(image.Height * size))); saveImg.Save(result,System.Drawing.Imaging.ImageFormat.Jpeg); } } catch (Exception ex) { Console.WriteLine(ex.ToString()); Console.ReadLine(); }
可以看到Image saveImg = new Bitmap(image, new Size((int)(image.Width * size), (int)(image.Height * size)));
这里从新new了一个bitmap对象,并且传入了一个大小信息,在这里改变了图片的大小!
还有就是改变图片格式的方法,在保存的时候选择图片格式即可:
saveImg.Save(result,System.Drawing.Imaging.ImageFormat.Jpeg);
这里选择的就是jpg的格式,其他的格式还有很多,另行发挥吧到时候!
这个只是一个简单的改变图片大小的方法,高深的等研究过再说……
记在这里,以后学习!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架