图片上传,图片加水印,验证码制作
Posted 张鑫4477
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了图片上传,图片加水印,验证码制作相关的知识,希望对你有一定的参考价值。
文件上传:
所用控件:FileUpload
使用时的思路:
1、判断用户是否选中了文件
FileUpload.FileName获取选中的文件名,判断长度,如果长度大于零就代表已经选择了文件
JS端:通过ID获取控件,然后控件的value获取选中的文件名
2、将文件保存到服务器上
FileUpload.SaveAs("绝对路径");
3、获得绝对路径
先编写相对路径:比如"UpLoads/abc.txt"
再把路径映射成绝对路径:Server.MapPath("UpLoads/abc.txt");
4、保留原文件的类型和文件名
相对路径改为"UpLoads/" + FileUpload1.FileName;
5、防止文件重名覆盖的问题
"UpLoads/" + Request.Cookies["user"].Value + DateTime.Now.ToString("yyyyMMddhhmmssms") + FileUpload1.FileName;
也就是在文件名前面拼接上登陆的用户名以及操作的时间,精确到毫秒
6、限制用户可选的文件类型
给控件添加属性accept=".jpg,.png,.jpeg,.txt"
但在打开的对话框中,仍然可以选择全部文件,只能通过JS端来限制:
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; } };
图片加水印:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="shuiyin.aspx.cs" Inherits="shuiyin" %> <!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="Button" /> <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 shuiyin : 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); //加上水印 //把获得图片变成一个GDI画图图面,开始绘制 Graphics g = Graphics.FromImage(img); //分别对应(水印内容,水印字体,水印颜色,水印位置) g.DrawString("哈哈哈", new Font("微软雅黑", 20), new SolidBrush(Color.Red), new PointF(0, 0)); //保存图片 //img指文件夹名称,后面给要加水印的文件重新命名 string ul = "img/" + DateTime.Now.ToString("yyyyMMddHHmmssms") + FileUpload1.FileName; //保存并生成绝对路径 img.Save(Server.MapPath(ul)); //把效果展示出来看一下 Image1.ImageUrl = ul; } }
效果:
验证码制作:
Bitmap bit = new Bitmap(90, 40);//画布大小 Graphics g = Graphics.FromImage(bit);//创建绘制对象,告诉它往哪张图片上绘制 Random r = new Random(); string s = ""; Color color1 = Color.FromArgb(r.Next(155, 255), r.Next(155, 255), r.Next(155, 255)); g.FillRectangle(new SolidBrush(color1), 0, 0, 90, 40);//画布颜色随机 for (int i = 0; i < 10; i++)//随机画干扰线 { Color color3 = Color.FromArgb(r.Next(170, 255), r.Next(190, 255), r.Next(170, 255)); Pen pp = new Pen(new SolidBrush(color3), r.Next(0, 5)); g.DrawLine(pp, r.Next(0, 90), r.Next(0, 40), r.Next(0, 90), r.Next(0, 40)); } string yan = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";//设置随机的验证码,从里面抽取 int wei = 0; for (int i = 0; i < 4; i++)//取四位验证码,颜色随机 { string m = yan.Substring(r.Next(0, yan.Length), 1); s += m; Color color2 = Color.FromArgb(r.Next(0, 155), r.Next(0, 155), r.Next(0, 155)); int w = r.Next(20, 25); Font f = new Font("黑体", w); SolidBrush b = new SolidBrush(color2); g.DrawString(m, f, b, wei, r.Next(0, 10)); wei += w; } Session["YZM"] = s;//把验证码保存到session中 for (int i = 0; i < 251; i++) { Color color3 = Color.FromArgb(r.Next(0, 255), r.Next(0, 255), r.Next(0, 255)); Pen pp = new Pen(new SolidBrush(color3), r.Next(0, 5)); int a = r.Next(0, 90); int b = r.Next(0, 40); g.DrawLine(pp, a, b, (a + 1), (b + 1)); } bit.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg); Response.End();
验证码:<asp:TextBox ID="TextBox3" runat="server"></asp:TextBox> <asp:Image ID="Image1" runat="server" ImageUrl="Default2.aspx" />//这里图片路径不是一张图片的话会默认打开
以上是关于图片上传,图片加水印,验证码制作的主要内容,如果未能解决你的问题,请参考以下文章