• 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("微软雅黑", 30);  
                         //字体  字体大小
    
            Brush b = new SolidBrush(Color.Red); 
                                   //画刷(颜色)
                                   //SolidBrush  实线画刷  
    
            PointF pf = new PointF(50, 50);
                              //画水印的坐标
    
            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 = 0;
        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 = 0; i < 4; i++)  //循环4遍,获得4位的随机数
            {
                s += all.Substring(r.Next(0, all.Length), 1);//从随机一个位置截取,截一位
            }
    
            Session["YZM"] = s;  //将验证码传倒服务器(用于判断用户是否填正确)
    
    
            //生产背景图
            Bitmap img = new Bitmap(120, 60);
    
             
    
            //填充背景色
            Graphics g2 = Graphics.FromImage(img);
    
            Brush b2 = new SolidBrush(clist[r.Next(0, clist.Count)]); 
                                          //随机背景颜色
    
            g2.FillRectangle(b2, 0, 0, 120, 60);
             //   填充     (颜色 ,x, y, 宽, 高)
    
    
    
            //生产绘制类
            Graphics g = Graphics.FromImage(img);
    
            Font f = new Font("微软雅黑", 30); //字体
    
            Brush b = new SolidBrush(Color.Red);//画刷
    
            g.DrawString(s, f, b, new PointF(0, 0));//画字符串
    
    
    
    
            //画干扰线
    
            for (int i = 0; i < 8; i++) //
            {
                Graphics g3 = Graphics.FromImage(img);
    
    
                Pen p3 = new Pen(new SolidBrush(clist[r.Next(0, clist.Count)]), r.Next(2, 5));
                             //  ( 随机干扰线的颜色, 随机干扰线的粗细)
    
                g3.DrawLine(p3, new Point(r.Next(0, 120), r.Next(0, 60)), new Point(r.Next(0, 120), r.Next(0, 60)));
                           //  ( 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 = 1;
        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(80, 30);
            //画布背景色泛性组合
            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(0, Clist.Count)]), 0, 0, 80, 30);
            //随机生成显示的验证码组合
            string str = "";
            for (int i = 0; i < 4; i++)
            {
                str += aaa.Substring(r.Next(0, aaa.Length), 1);
            }
    
            Session["YZM"] = str;
            Font f = new Font("黑体", 20);
            Brush b = new SolidBrush(Color.Red);
            //生成
            g.DrawString(str, f, b, 10, 0);
            //添加干扰线
            for (int i = 0; i < r.Next(6, 20); i++)
            {
                Brush bb = new SolidBrush(Clist[r.Next(0, Clist.Count)]);
                Pen p = new Pen(bb, 1);
                g.DrawLine(p, r.Next(0, 80), r.Next(0, 30), r.Next(0, 80), r.Next(0, 30));
            }
            //保存完成
            img.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);
            Response.End();
        }
    后台

  • 相关阅读:
    C#中调用Outlook API 发起会议
    Log4Net配置
    web端调用Webapi获取Excel表格
    表格导出之Aspose.Cells
    验证输入框
    把新建的对象所有属性变成默认值
    省市区三级联动
    全局异常处理
    HttpHttpServletRequest / Reponse
    热部署
  • 原文地址:https://www.cnblogs.com/Tanghongchang/p/6977770.html
Copyright © 2020-2023  润新知