C#--web水印+++++验证码

水印:一个fileupload一个button

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Drawing;
using System.IO;
public partial class 水印 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}
protected void shangchuan_Click(object sender, EventArgs e)
{
//一、从上传数据中,转化成图片对象。
Stream s = FileUpload1.FileContent;
System.Drawing.Image img = System.Drawing.Image.FromStream(s);//重点
//二、对图片对象进行画水印
//1.造笔
SolidBrush brush=new SolidBrush (Color.Yellow);
//2.造字体
Font f = new Font("Buxton Sketch", 12);

//3.找到图像绘图区域
Graphics gs = Graphics.FromImage(img);

//4.确定开始画位置
float x = 0, y = 0;

SizeF size = gs.MeasureString("happy new year!", f);//确定书写的水印(即要写的字)的大小
x = img.Width-size.Width;
y = img.Height-size.Height;
//5.开始画
gs.DrawString("happy new year!", f, brush, x, y);
//三、图片对象另存到硬盘上去
string app = FileUpload1.FileName;
string path = Server.MapPath("/upload/")+app;
img.Save(path);

}
}

验证码:

显示页面(出现验证码,cs代码中夹杂验证码的验证):

aspx页面(script中是点击验证码后改变原来的随机数):

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Denglu.aspx.cs" Inherits="Denglu" %>

<!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>
<script language="javascript">
function Bianhua()
{
var img = document.getElementById("Image1");
img.setAttribute("src", "验证码.aspx?id=" + Math.random());
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>

<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:Image ID="Image1" runat="server" ImageUrl="验证码.aspx" onclick="Bianhua()"/>
<br />
<asp:Button ID="Deng" runat="server" Text="登录" />

</div>
</form>
</body>
</html>

验证码的生成图片页面:

aspx.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Drawing;
using System.IO;
public partial class 验证码 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Bitmap img = new Bitmap(70, 30);//创建一个新的位图
string suijishu = "qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM1234567890";
//所有随机产生的字符的集合字符串
Random ran = new Random();
string yzm = "";
for (int i = 0; i < 4; i++)
{
int a = ran.Next(suijishu.Length);
yzm += suijishu.Substring(a,1);
}

//以上是产生4个随机的字符组成一个拥有4个字符的字符串

Session["code"] = yzm;
//将随机产生的字符串记录下来,供用户填写时验证

//、、、以下是将产生的字符串水印到产生的位图上,(由于位图默认背景颜色为黑色)
SolidBrush brush = new SolidBrush(Color.Green);//勾勒画笔颜色
Font fo = new Font("SketchFlow Print", 18);//描述字体以及大小

Graphics gs = Graphics.FromImage(img);//描述要水印的图片,即新产生的位图

gs.DrawString(yzm, fo, brush, 0, 0);//勾画水印

//把图片保存到输出流中去
img.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);

Response.End();
}
}

或者验证码的背景图片为白色:

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)
{
//造一个图片
Bitmap img = new Bitmap(60, 30);
//生成个随机数
string str = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
Random rand = new Random();
string code = "";
for (int i = 0; i < 4; i++)
{
int start = rand.Next(str.Length);
code += str.Substring(start, 1);
}
Session["yzm"] = code;
//把随机数画到图片上
SolidBrush brush = new SolidBrush(Color.White);
Font font = new Font("Lucida Sans Unicode", 14);
Graphics g = Graphics.FromImage(img);

//a.把图片背景涂白
g.FillRectangle(brush, 0, 0, 60, 30);
//b.给画笔换个颜色
brush.Color = Color.Red;

g.DrawString(code, font, brush, 0, 0);

//把图片保存到输出流中去
img.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);

Response.End();
}
}

时间: 2024-09-20 21:24:18

C#--web水印+++++验证码的相关文章

.NET加水印/验证码的NuGet包

.NET加水印/验证码的NuGet包 我的在前两篇文章(水印,验证码)中,我介绍了使用Direct2D给图片加水印/验证码,今天我将其进行了封装,发布了一个NuGet包Sdcb.Imaging: <PackageReference Include="Sdcb.Imaging" Version="1.1.0" /> 或者在这个链接中下载: https://www.nuget.org/packages/Sdcb.Imaging .NET中给图片加水印 usi

web项目 验证码 小园dd

1. jsp代码 : 1 <Script> 2 function changeImg(){ 3 document.getElementById("certImg").src ="makeCertPic.jsp?it="+Math.random(); /* +Math.random() */ 4 } 5 </Script> 6 7 8 9 <table width="100%" border="0"

图片上传,图片加水印,验证码制作

文件上传: 所用控件:FileUpload 使用时的思路: 1.判断用户是否选中了文件 FileUpload.FileName获取选中的文件名,判断长度,如果长度大于零就代表已经选择了文件 JS端:通过ID获取控件,然后控件的value获取选中的文件名 2.将文件保存到服务器上 FileUpload.SaveAs("绝对路径"); 3.获得绝对路径 先编写相对路径:比如"UpLoads/abc.txt" 再把路径映射成绝对路径:Server.MapPath(&quo

java web 实现验证码

验证码的作用:通常的登录或者注册系统时,都会要求用户输入验证码,以此区别用户行为和计算机程序行为,目的是有人防止恶意注册.暴力破解密码等. 实现验证码的思路:用 server 实现随机生成数字和字母组成图片的功能,用 jsp 页面实现显示验证码和用户输入验证码的功能,再用 server 类分别获取图片和用户输入的数据,判断两个数据是否一致. 代码实现 1.编写数字.英文随机生成的 server 类,源码: 1 package com; 2 3 import java.awt.Color; 4 i

web 图片验证码 验证

做一个图片验证码 界面中的代码 1 验证码:<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox><asp:Image ID="Image1" ImageUrl="YZM.aspx" runat="server" /> 2 <asp:Button ID="Button1" runat=&q

web程序验证码刷不出来原因

关键字:Could not initialize class sun.awt.X11GraphicsEnvironment --linux系统下应用项目的验证码不能够正常显示 说明没是加载java.awt.headless,于是修改${TOMCAT_HOME}/bin/catalina.sh (windows修改catalina.bat).查找到-Djava.io.tmpdir="$CATALINA_TMPDIR"这行,并在这一行下加入:-Djava.awt.headless=true

SpringMVC+Apache Shiro+JPA(hibernate)案例教学(三)给Shiro登录验证加上验证码

序: 给Shiro加入验证码,有多种方式,当然你也可以通过继承修改FormAuthenticationFilter类,通过Shiro去验证验证码.具体实现请百度: 应用Shiro到Web Application(验证码实现) 而今天我要说的,既然使用的SpringMVC,为什么不直接在Controller中就处理验证码验证,让事情变的更简单一点呢? 一.新建ValidateCode.java验证码工具类 package org.shiro.demo.util; import java.util.

Java如何获取图片验证码保存

举例网站:https://my.1hai.cn/Login/?url=http://www.1hai.cn/ 一.场景:出于安全性考虑,越来越多的Web平台登录都会增加图形验证码(图片),或者短信验证码.由于是图片脚本selenium是无法识别的,这是时候我们解析图片验证码. 解决思路:1.通过selenium定位到图片,把图片保存到本地. 2 通过ORC技术将图片验证码转化为文字. 其他解决方法:A:去掉验证码    B:设置万能码 二.Web图片验证码的实现源码: 1 package uti

CAS之改造

cas改造 关键字: sso域名:passport.xiw.com 登陆地址(spring web flow):http://www.xiw.com/site/login 登陆地址(直接):https://passport.xiw.com/login?locale=zh_CN 退出地址:https://passport.xiw.com/logout 一.涉及模块 svn地址: cas-client-core http://svnxiw.xiw.com/Userend/passport/cas_c