博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
生成验证码语法(旧版)
阅读量:4687 次
发布时间:2019-06-09

本文共 1668 字,大约阅读时间需要 5 分钟。

一.通过“一般处理程序”返回一张图片:

1.一般处理程序需要先设置ContentType =“image/jpeg

2.一般处理程序需要把图片保存到Response.OutputStream中。

 


代码:

  • 自己创建一张图片

 

注:File.OpenWrite(@"d:\2.jpg")  表示将创建的图片保存的位置!

 

  • 将本地图片复制到浏览器上
1 context.Response.ContentType = "image/jpeg";2             string path = context.Server.MapPath("~/lanse22.jpg");3             using (Stream allfile = File.OpenRead(path))4             {5                 allfile.CopyTo(context.Response.OutputStream);6             }
  • 对图片内容进行添加或修饰
1 context.Response.ContentType = "image/jpeg";2             string path = context.Server.MapPath("~/lanse22.jpg");3             using (Image img = Bitmap.FromFile(path))4             using (Graphics g = Graphics.FromImage(img))5                 {6                     g.DrawString("水印", new Font(FontFamily.GenericSansSerif, 50), Brushes.Red, 10, 10);7                     img.Save(context.Response.OutputStream, ImageFormat.Jpeg);8                 }
  •  生成简单的数字验证码
1 context.Response.ContentType = "image/jpeg"; 2             Random rand = new Random(); 3            int number= rand.Next(1000, 10000); 4            using (Bitmap bm = new Bitmap(55, 25)) 5            using (Graphics g =Graphics.FromImage(bm)) 6            using(Font font =new Font (FontFamily.GenericSansSerif,15)) 7            { 8                g.DrawString(number.ToString(), font, Brushes.Red, 0, 0); 9                using (Stream stream = File.OpenWrite("d:\\sy.jpg"))10                {11                    bm.Save(context.Response.OutputStream, ImageFormat.Jpeg);12                }13            }

 

关于生成的验证码,通过系统产生的随机数存入Session中。在到Sesson中取数据和用户输入的数据进行比对!

具体的操作步骤在后面写Cookie和session中详细介绍!

转载于:https://www.cnblogs.com/fengxuehuanlin/p/4885111.html

你可能感兴趣的文章
day07
查看>>
【Android开发:自定义控件系列二】关于PopupWindow的注意点
查看>>
HTML——使用表格进行页面布局
查看>>
字符串统计 连续的某个字符的数量 1.1.4
查看>>
JMS
查看>>
gulpfile 压缩模板
查看>>
JAVA知多少
查看>>
Kruskal算法(转)
查看>>
CSS3 Media Queries实现响应式布局
查看>>
【34.14%】【BZOJ 3110】 [Zjoi2013]K大数查询
查看>>
【 henuacm2016级暑期训练-动态规划专题 A 】Cards
查看>>
第五篇:白话tornado源码之褪去模板的外衣
查看>>
设备常用框架framework
查看>>
bootstrap模态框和select2合用时input无法获取焦点(转)
查看>>
快速转移数据的要领
查看>>
windows情况下的oracle效力
查看>>
*nix-style:定制 bash 提示符
查看>>
Informix IDS 11系统解决(918查验)认证指南,第 7 部分: IDS复制(7)
查看>>
解决Charles Response 中文乱码
查看>>
Spring Boot 分布式Session状态保存Redis
查看>>