标签:
水印:一个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();
    }
}
标签:
原文地址:http://www.cnblogs.com/xianshui/p/4541977.html