1、上传图片加水印:
<%@ 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); //加上水印 Graphics g = Graphics.FromImage(img); string s="hello word"; Font f=new Font("微软雅黑",30); Brush b=new SolidBrush(Color.Red); PointF pf=new PointF(50,50); g.DrawString(s,f,b,pf); //保存下来 string path = "upload/" +DateTime.Now.ToString("yyyyMMddhhmmssms")+ FileUpload1.FileName; img.Save(Server.MapPath(path)); Image1.ImageUrl = path; }}
2、验证码制作
<%@ 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;using System.Drawing;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) { Label1.Text = Session["Default2"].ToString(); if (TextBox1.Text == Session["Default2"].ToString()) { Label2.Text = "正确!!!"; } else { Label2.Text = "错误!!"; } }}
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="yzm.aspx.cs" Inherits="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.Purple); clist.Add(Color.Pink); clist.Add(Color.Blue); clist.Add(Color.Orange); clist.Add(Color.Black); clist.Add(Color.White); clist.Add(Color.Violet); Random r = new Random(); string s = ""; string all = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"; for (int i = 0; i < 4; i++) { s += all.Substring(r.Next(0, all.Length), 1); } Session["Default2"] = s; Bitmap img = new Bitmap(100, 50); Graphics g = Graphics.FromImage(img); Brush b = new SolidBrush(clist[r.Next(0, clist.Count)]); g.FillRectangle(b, 0, 0, 100, 50); Graphics g2 = Graphics.FromImage(img); Font f = new Font("微软雅黑", 30); Brush b2 = new SolidBrush(Color.Red); PointF pf = new PointF(5, 5); g2.DrawString(s, f, b2, pf); for (int i = 0; i < 7; 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, 100), r.Next(0, 50)), new Point(r.Next(0, 100), r.Next(0, 50))); } img.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Png); }}