码迷,mamicode.com
首页 > Windows程序 > 详细

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

时间:2017-01-06 16:28:22      阅读:257      评论:0      收藏:0      [点我收藏+]

标签:url   保留   view   show   asp   time   否则   ppa   rgba   

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

2、如何保存到服务器上?
FileUpload.SaveAs("绝对路径");

3、如何获得绝对路径?
先编写相对路径 - "UpLoads/abc.txt"
将相对路径映射成绝对路径 - Server.MapPath("UpLoads/abc.txt");

4、现在只能上传成为txt文件,并且名字只能是abc
如何保留文件原有的名字和文件类型?
"UpLoads/" + FileUpload1.FileName;

5、如何防止重名覆盖的问题?
"UpLoads/" + Request.Cookies["user"].Value + DateTime.Now.ToString("yyyyMMddhhmmssms") + FileUpload1.FileName;

6、如何限制选中文件的类型?
限制普通人,给控件添加属性 - accept=".jpg,.png,.jpeg,.txt"
限制找事的:
document.getElementById("Button1").onclick = function () {
var fileName = document.getElementById("FileUpload1").value;

var name = fileName.substr(fileName.length - 4, 4);
var name1 = fileName.substr(fileName.length - 5, 5);
if (name != ".jpg" && name != ".png" && name != ".txt" && name1 != ".jpeg")
{
alert("请选择正确的文件!你要找事儿啊??");
return false;
}
};

7、控制上传文件的大小
扩容 - 系统默认允许最大上传长度是4MB
webconfig配置文件中写入
<system.web>
<compilation debug="false" targetFramework="4.0" />
<httpRuntime maxRequestLength="40000" />
</system.web>

百度搜,研究,C#大文件上传,断点续传。

注意!不要扩的太多,否则多人同时上传大文件会造成服务器内存不足!

限制大小
C#端限制:
if (FileUpload1.PostedFile.ContentLength>(4*1024*1024))
{
Label1.Text = "文件长度过长!!!";
return;
}

JS端限制:
var f = document.getElementById("FileUpload1");

if (f.files[0].size > (4 * 1024 * 1024)) {
alert("文件过大!!!");
return false;
}

上传图片加水印:

技术分享
<%@ 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>
    <style type="text/css">
        #Image1 {
            width: 500px;
        }
    </style>
</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>
View Code

后台:

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);

        Graphics g = Graphics.FromImage(img);

        string s = "汉企奇点网络0928";
        Font f = new Font("微软雅黑",20);
        Brush b = new SolidBrush(Color.Red);
        PointF p = new PointF(20,20);

        g.DrawString(s, f, b, p);

        string sss = "images/"+DateTime.Now.ToString("yyyyMMddhhmmssms")+FileUpload1.FileName;

        img.Save(Server.MapPath(sss));

        Image1.ImageUrl = sss;
    }
}

图片验证码:

技术分享
<%@ 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>
            <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
            <asp:Image ID="Image1" runat="server" ImageUrl="YZM.aspx" />
            <asp:Button ID="Button1" runat="server" Text="验证" />
            <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
        </div>
    </form>
</body>
</html>
<script type="text/javascript">
    var a = 0;
    document.getElementById("Image1").onclick = function () {
        this.setAttribute("src", "yzm.aspx?id=" + a);
        a++;
    }



</script>
View Code

后台:

技术分享
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)
    {
        string t1 = TextBox1.Text;
        string t2 = Session["YZM"].ToString();

        if (t1.ToUpper() == t2.ToUpper())
        {
            Label1.Text = "验证成功!";
        }
        else
        {
            Label1.Text = "验证失败!";
        }


    }
}
View Code

验证码图片:

(前台不用写)

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(80, 40);
        Graphics g = Graphics.FromImage(img);
        Random r = new Random();
        //单独加颜色
        //List<Color> clist = new List<Color>();
        //clist.Add(Color.Yellow);
        //clist.Add(Color.Green);
        //clist.Add(Color.Blue);
        //clist.Add(Color.Pink);
        //clist.Add(Color.Orange);
        //clist.Add(Color.Black);


        //g.FillRectangle(new SolidBrush(clist[r.Next(0, clist.Count)]), 0, 0, 80, 40);



        //for (int i = 0; i < 10; i++)
        //{
        //    Color ccc = clist[r.Next(0, clist.Count)];

        //    Pen ppp = new Pen(new SolidBrush(ccc), r.Next(1, 5));

        //    g.DrawLine(ppp, new PointF(r.Next(0, 80), r.Next(0, 40)), new PointF(r.Next(0, 80), r.Next(0, 40)));
        //}
        //随机数颜色rgba
        Color yanse = Color.FromArgb(r.Next(255), r.Next(255), r.Next(255), r.Next(255));       
        g.FillRectangle(new SolidBrush(yanse), 0, 0, 80, 40);
        for (int i=0;i<1000;i++)
        {
            Color yansel = Color.FromArgb(r.Next(255), r.Next(255), r.Next(255), r.Next(255));
            Pen ppp = new Pen(new SolidBrush(yansel), r.Next(1, 5));
            g.DrawLine(ppp, new PointF(r.Next(0, 80), r.Next(0, 40)), new PointF(r.Next(0, 80), r.Next(0, 40)));            
        }




        string all = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
        string s = "";

        for (int i = 0; i < 4; i++)
        {
            int a = r.Next(all.Length);
            s += all.Substring(a, 1);
        }

        Session["YZM"] = s;

        g.DrawString(s, new Font("微软雅黑", 16), new SolidBrush(Color.Red), new PointF(3, 3));

        img.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Png);
        Response.End();

    }
}

 

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

标签:url   保留   view   show   asp   time   否则   ppa   rgba   

原文地址:http://www.cnblogs.com/shadow-wolf/p/6256612.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!