上传图片(带水印)
1、获取要上传的图片
2、加水印
3、保存下来
using System.Drawing; --绘画类命名空间
图片最后要用绝对路径保存
Server.MapPath(相对路径)
-- 将相对路径映射成绝对路径
案例分析
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
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" %>
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) { //准备颜色集合 Listclist = new List (); 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 图片格式 }}
例
protected void Page_Load(object sender, EventArgs e) { Random r = new Random(); string aaa = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"; //生成画布 Bitmap img = new Bitmap(80, 30); //画布背景色泛性组合 ListClist = new List (); 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(); }