WebForm 【上传图片】【图片验证码】
上传图片(带水印)
1、获取要上传的图片
2、加水印
3、保存下来
using System.Drawing; --绘画类命名空间
图片最后要用绝对路径保存
Server.MapPath(相对路径)
-- 将相对路径映射成绝对路径
案例分析
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:FileUpload ID="FileUpload1" runat="server" />
<asp:Button ID="Button1" runat="server" Text="上传" /><br />
<asp:Image ID="Image1" runat="server" />
</div>
</form>
</body>
</html>
前台
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Drawing; public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Button1.Click += Button1_Click;
} void Button1_Click(object sender, EventArgs e)
{
//获得要上传的图片 System.Drawing.Image img = System.Drawing.Image.FromStream(FileUpload1.FileContent);
// 在这里 image 具有二义性,是个重名的类,声明空间以区分
// FromStream 方法(创建图片对象),需要一个《流》类型
//FileContent 返回一个流类型 //加上水印 Graphics g = Graphics.FromImage(img);
//Graphics 类(绘制类)
//FromImage 往.....画 string s = "WWW.ITNBA.COM";
//水印 Font f = new Font("微软雅黑", );
//字体 字体大小 Brush b = new SolidBrush(Color.Red);
//画刷(颜色)
//SolidBrush 实线画刷 PointF pf = new PointF(, );
//画水印的坐标 g.DrawString(s, f, b, pf);
//DrawString 画字符串类型水印
//(可用绘制对象点出各种绘制水印的样式(把图像做水印)) //保存下来 string path = "Uploads/" + DateTime.Now.ToString("yyyyMMddhhmmssms") + FileUpload1.FileName;
//相对路径
//FileUpload1.FileName 原名保存 img.Save(Server.MapPath(path)); //Save 保存方法,需要绝对路径
// Server.MapPath(相对路径) 将相对路径映射成绝对路径 Image1.ImageUrl = path;
// 展示,Image控件展示时 用相对路径
}
}
后台代码
用图片显示验证码
验证码的作用是在于防止某些别有用心的用户用暴力破解等方法猜测密码,是一项非常有效的防止黑客技术。
Image代表图像,是个抽象体,
Bitmap派生于Image,是具体的一个对象,即代表图像中的位图(而不是矢量图),像bmp、jpg、gif、png、tif等都是位图,
Bitmap 是用于处理由像素数据定义的图像的对象
案例分析
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<br />
<br />
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox> <img id="yzm1" src="YZM.aspx" /><br /> <%--图片指向 YZM.aspx 这个网页,验证码在此显示 --%> <asp:Button ID="Button1" runat="server" Text="验证" /> <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label><br /> <asp:Label ID="Label2" runat="server" Text="Label"></asp:Label>
</div>
</form>
</body>
</html>
<script type="text/javascript">
var a = ;
document.getElementById("yzm1").onclick = function () {
this.src = "yzm.aspx?a=" + a;
a++;
} //点击验证码图片,刷新,
//a 无用,传a 使每次的值都不一样,才能刷新 </script>
主页前台
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls; public partial class Default2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Button1.Click += Button1_Click;
} void Button1_Click(object sender, EventArgs e)
{
Label2.Text = Session["YZM"].ToString();
if (TextBox1.Text == Session["YZM"].ToString())
Label1.Text = "正确!!!";
else
Label1.Text = "错误!!!!!!!";
}
}
主页后台
YZM前台无操作
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Drawing; public partial class YZM : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//准备颜色集合
List<Color> clist = new List<Color>();
clist.Add(Color.Red);
clist.Add(Color.Firebrick);
clist.Add(Color.LawnGreen);
clist.Add(Color.Goldenrod);
clist.Add(Color.Cyan);
clist.Add(Color.DarkSlateBlue);
clist.Add(Color.Indigo); //制作随机验证码 Random r = new Random(); string s = ""; string all = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmeopqrstuvwxyz0123456789"; for (int i = ; i < ; i++) //循环4遍,获得4位的随机数
{
s += all.Substring(r.Next(, all.Length), );//从随机一个位置截取,截一位
} Session["YZM"] = s; //将验证码传倒服务器(用于判断用户是否填正确) //生产背景图
Bitmap img = new Bitmap(, ); //填充背景色
Graphics g2 = Graphics.FromImage(img); Brush b2 = new SolidBrush(clist[r.Next(, clist.Count)]);
//随机背景颜色 g2.FillRectangle(b2, , , , );
// 填充 (颜色 ,x, y, 宽, 高) //生产绘制类
Graphics g = Graphics.FromImage(img); Font f = new Font("微软雅黑", ); //字体 Brush b = new SolidBrush(Color.Red);//画刷 g.DrawString(s, f, b, new PointF(, ));//画字符串 //画干扰线 for (int i = ; i < ; i++) //
{
Graphics g3 = Graphics.FromImage(img); Pen p3 = new Pen(new SolidBrush(clist[r.Next(, clist.Count)]), r.Next(, ));
// ( 随机干扰线的颜色, 随机干扰线的粗细) g3.DrawLine(p3, new Point(r.Next(, ), r.Next(, )), new Point(r.Next(, ), r.Next(, )));
// ( p3, 随机起点坐标 ,随机终点坐标 ) //DrawLine 画线段(要有两个坐标点)
} //保存到流里 img.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Png);
// 保存到 。流里面 , ImageFormat 图片格式 }
}
YZM 后台
例
<form id="form1" runat="server">
<div>
用户名:<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<br />
密码:<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
<br />
验证码:<asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
<asp:Image ID="Image1" runat="server" ImageUrl="YZM.aspx" />
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
<br />
<asp:Button ID="Button1" runat="server" Text="Button" />
</div>
</form>
</body>
</html>
<script type="text/javascript">
var aaa = ;
document.getElementById("Image1").onclick = function () {
this.setAttribute("src", "YZM.aspx?id=" + aaa);
aaa++;
};
</script>
前台
protected void Page_Load(object sender, EventArgs e)
{
Random r = new Random();
string aaa = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
//生成画布
Bitmap img = new Bitmap(, );
//画布背景色泛性组合
List<Color> Clist = new List<Color>();
Clist.Add(Color.Yellow);
Clist.Add(Color.Green);
Clist.Add(Color.Blue);
Clist.Add(Color.Aqua);
Clist.Add(Color.Orange);
Clist.Add(Color.Pink); Graphics g = Graphics.FromImage(img); g.FillRectangle(new SolidBrush(Clist[r.Next(, Clist.Count)]), , , , );
//随机生成显示的验证码组合
string str = "";
for (int i = ; i < ; i++)
{
str += aaa.Substring(r.Next(, aaa.Length), );
} Session["YZM"] = str;
Font f = new Font("黑体", );
Brush b = new SolidBrush(Color.Red);
//生成
g.DrawString(str, f, b, , );
//添加干扰线
for (int i = ; i < r.Next(, ); i++)
{
Brush bb = new SolidBrush(Clist[r.Next(, Clist.Count)]);
Pen p = new Pen(bb, );
g.DrawLine(p, r.Next(, ), r.Next(, ), r.Next(, ), r.Next(, ));
}
//保存完成
img.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);
Response.End();
}
后台

WebForm 【上传图片】【图片验证码】的更多相关文章
- Webform 文件上传、 C#加图片水印 、 图片验证码
文件上传:要使用控件 - FileUpload 1.如何判断是否选中文件? FileUpload.FileName - 选中文件的文件名,如果长度不大于0,那么说明没选中任何文件 js - f.val ...
- webform:图片水印、验证码制作
一.图片水印 1:引命名空间System.Drawing; 前端代码 <div> <asp:FileUpload ID="FileUpload1" runat=& ...
- webform(十)——图片水印和图片验证码
两者都需要引入命名空间:using System.Drawing; 一.图片水印 前台Photoshuiyin.aspx代码: <div> <asp:FileUpload ID=&q ...
- 原生XMLHTTPResponse,jQuery-Ajax 上传文件;iframe上传图片&预览;图片验证码小案例
原生AJAX Ajax主要就是使用 [XmlHttpRequest]对象来完成请求的操作,该对象在主流浏览器中均存在(除早起的IE),Ajax首次出现IE5.5中存在(ActiveX控件) 1.Xml ...
- Django(九)下:Ajax操作、图片验证码、KindEditor使用
三.Ajax操作 ajax操作基于浏览器的xmlHttpRequest对象,IE低版本是另外一个对象,jQuery 1 版本对那两个对象做了封装,兼容性最好,2 .3版本不再支持IE低版本了. Aja ...
- ASP.NET -- WebForm -- 给图片添加水印标记
ASP.NET -- WebForm: 给图片添加水印标记 ASP.NET:使用 WebForm(C#) 制作一个简单的为图片添加水印的页面. 1. Test2.aspx文件 <%@ Page ...
- Day24-Ajax操作、图片验证码、KindEditor使用-转
参考源:http://blog.csdn.net/fgf00/article/details/54917439 三.Ajax操作 ajax操作基于浏览器的xmlHttpRequest对象,IE低版本是 ...
- Python开发【Django】:图片验证码、KindEditor
图片验证码 生成图片验证码需要以下: session check_code.py(依赖:Pillow,字体文件) 模块安装 pip install Pillow src属性后面加? 在utils下拷贝 ...
- int.TryParse非预期执行引发的思考 ASP.NET -- WebForm -- 给图片添加水印标记 Windows -- 使用批处理文件.bat删除旧文件
int.TryParse非预期执行引发的思考 问题出现 这天在写一个页面,想谨慎些就用了int.TryParse,结果出问题了. 代码如下: Copy int id = 1000; //Reque ...
- ASP.NET中图片验证码与js获取验证码的值
现在的程序中,为了防止用户恶意点击,我们一般都会加上验证,现在比较普遍的是加上图片验证码或者手机短信验证.验证码一般都是防机器不防人,有效的防止了恶意点击. 那么在webform中如何生成动态的图片验 ...
随机推荐
- modal 移除遮盖层
弹框关闭时 移除遮盖层 $("#modal").bind('hide.bs.modal',function(){ $(".modal-backdrop").re ...
- vs.code调试node.js的C++扩展
其实也很简单 点击“Add Configration..”后,会在launch.json增加一个节点,稍调整两个位置 以上完了后,就能在cpp源码里加上自己的断点,执行debug调试我们的C++源代码 ...
- 【repost】H5总结
1.新增的语义化标签: <nav>: 导航 <header>: 页眉 <footer>: 页脚 <section>:区块 <article> ...
- 《python语言程序设计》_第一章编程题
题目1.1 :显示"welcome to python " 答案:print('welcome to python') 题目1.2:显示"welcome to pytho ...
- B - Dropping tests
In a certain course, you take n tests. If you get ai out of bi questions correct on test i, your cum ...
- 2.第一个ASP.NET MVC 5.0应用程序
大家好,上一篇对ASP.NET MVC 有了一个基本的认识之后,这一篇,我们来看下怎么从头到尾创建一个ASP.NET MVC 应用程序吧.[PS:返回上一篇文章:1.开始学习ASP.NET MVC] ...
- Linux pwn入门教程——格式化字符串漏洞
本文作者:Tangerine@SAINTSEC 原文来自:https://bbs.ichunqiu.com/thread-42943-1-1.html 0×00 printf函数中的漏洞printf函 ...
- a标签嵌套a标签在实际项目开发中遇到的坑
大家都知道HTML的嵌套规范,其中一个规范是块元素嵌套行内元素,块元素嵌套块元素,行内元素嵌套行内元素,行内元素不能嵌套块元素. 其中需要注意的是行内元素嵌套行内元素,a标签虽然是行内元素,但是a标签 ...
- 列表list切片
list1 = [1, 2, 3, 4, 5, 6] list1[::-1] >>>[6, 5, 4, 3, 2, 1] list1[:3:-1] >>>[6, 5 ...
- jfixed使固定行列可编辑表格
功能: 固定行列 可以在表格直接编辑 使用ajax对数据操作 使用tab键在可编辑列切换简单介绍一下jfixed 表格插件, jfixed /jfixed.rar