上传图片(带水印) 

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 【上传图片】【图片验证码】的更多相关文章

  1. Webform 文件上传、 C#加图片水印 、 图片验证码

    文件上传:要使用控件 - FileUpload 1.如何判断是否选中文件? FileUpload.FileName - 选中文件的文件名,如果长度不大于0,那么说明没选中任何文件 js - f.val ...

  2. webform:图片水印、验证码制作

    一.图片水印 1:引命名空间System.Drawing; 前端代码 <div> <asp:FileUpload ID="FileUpload1" runat=& ...

  3. webform(十)——图片水印和图片验证码

    两者都需要引入命名空间:using System.Drawing; 一.图片水印 前台Photoshuiyin.aspx代码: <div> <asp:FileUpload ID=&q ...

  4. 原生XMLHTTPResponse,jQuery-Ajax 上传文件;iframe上传图片&预览;图片验证码小案例

    原生AJAX Ajax主要就是使用 [XmlHttpRequest]对象来完成请求的操作,该对象在主流浏览器中均存在(除早起的IE),Ajax首次出现IE5.5中存在(ActiveX控件) 1.Xml ...

  5. Django(九)下:Ajax操作、图片验证码、KindEditor使用

    三.Ajax操作 ajax操作基于浏览器的xmlHttpRequest对象,IE低版本是另外一个对象,jQuery 1 版本对那两个对象做了封装,兼容性最好,2 .3版本不再支持IE低版本了. Aja ...

  6. ASP.NET -- WebForm -- 给图片添加水印标记

    ASP.NET -- WebForm: 给图片添加水印标记 ASP.NET:使用 WebForm(C#) 制作一个简单的为图片添加水印的页面. 1. Test2.aspx文件 <%@ Page ...

  7. Day24-Ajax操作、图片验证码、KindEditor使用-转

    参考源:http://blog.csdn.net/fgf00/article/details/54917439 三.Ajax操作 ajax操作基于浏览器的xmlHttpRequest对象,IE低版本是 ...

  8. Python开发【Django】:图片验证码、KindEditor

    图片验证码 生成图片验证码需要以下: session check_code.py(依赖:Pillow,字体文件) 模块安装 pip install Pillow src属性后面加? 在utils下拷贝 ...

  9. int.TryParse非预期执行引发的思考 ASP.NET -- WebForm -- 给图片添加水印标记 Windows -- 使用批处理文件.bat删除旧文件

    int.TryParse非预期执行引发的思考   问题出现 这天在写一个页面,想谨慎些就用了int.TryParse,结果出问题了. 代码如下: Copy int id = 1000; //Reque ...

  10. ASP.NET中图片验证码与js获取验证码的值

    现在的程序中,为了防止用户恶意点击,我们一般都会加上验证,现在比较普遍的是加上图片验证码或者手机短信验证.验证码一般都是防机器不防人,有效的防止了恶意点击. 那么在webform中如何生成动态的图片验 ...

随机推荐

  1. 从中央仓库下载所想要的jar包

    中央仓库地址:https://mvnrepository.com/ 这边我搜索一个commons-logging包作为例子: 点击下面第二个绿色的comons-logging进入这个页面: 一.win ...

  2. docker gitlab安装

    mkdir -p /data/docker/volumes/gitlab chmod 777 /data/docker/volumes/gitlab cd /data/docker/volumes/g ...

  3. Solutions and Summay for Linked List Naive and Easy Questions

    1.Remove Linked List Elements package linkedlist; /* * Question: Remove all elements from a linked l ...

  4. Delphi fmx控件在手机滑动与单击的问题

    Delphi fmx控件在手机滑动与单击的问题 (2016-03-08 10:52:00) 转载▼ 标签: it delphi 分类: Delphi10 众所周知,fmx制作的app,对于象TEdit ...

  5. .Wait()与.GetAwaiter()之间有什么区别

    两者都是同步等待操作的结果差异主要在于处理异常.使用Wait,异常堆栈跟踪不会改变并表示异常时的实际堆栈,因此如果您有一段代码在线程池线程上运行,那么您将拥有类似的堆栈 ThreadPoolThrea ...

  6. 关于Runtime.getRuntime().exec()产生阻塞的2个陷阱

    本文来自网易云社区 背景 相信做java服务端开发的童鞋,经常会遇到Java应用调用外部命令启动一些新进程来执行一些操作的场景,这时候就会使用到Runtime.getRuntime().exec(), ...

  7. 关于JavaScript的操作

    一:js基础. 1. var是定义js变量的关键字. 如: var leng=5;定义一个变量为5 var length = 16; // Number 通过数字字面量赋值 var points = ...

  8. Mybatis框架一:搭建测试

    Mybatis框架不再介绍: 在JDBC中存在一些问题: 1.频繁连接和释放资源浪费内存 2.编码完成后不便于维护 于是产生了简化数据库操作的框架:Hibernate.Mybatis等等,这里介绍My ...

  9. ElasticSearch权威指南学习(结构化查询)

    请求体查询 简单查询语句(lite)是一种有效的命令行adhoc查询.但是,如果你想要善用搜索,你必须使用请求体查询(request body search)API. 空查询 我们以最简单的 sear ...

  10. IDEA快捷键整理

                                                                              IDEA快捷键整理 一.修改快捷键方法: 点击 Fi ...