图片加水印 图片验证码

图片加水印

1   获取这张图片

System.Drawing.Image img = System.Drawing.Image.FromStream(FileUpload1.FileContent);

2 给图片加水印

 Graphics g = Graphics.FromImage(img);        

string s = "WWW.ITNBA.COM";         s 给图片加的水印文字

Font f = new Font("微软雅黑", 30);        

Brush b = new SolidBrush(Color.Red);        

PointF pf = new PointF(50, 50);

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

3 保存这张图

string path = "Uploads/" + DateTime.Now.ToString("yyyyMMddhhmmssms") + FileUpload1.FileName;

       

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

Image1.ImageUrl = path;

网页保存这张图回事aspx格式   将后缀改成png格式即可查看图片

小知识 :  图片转换成txt格式 会出现乱码  在乱码下方添加 文字 再转换成jpg个是不会影响图片

图片格式的验证码

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

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


        img.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Png);
图片后台

前台验验证 验证码
var a = 0;
    document.getElementById("yzm1").onclick = function () {
        this.src = "yzm.aspx?a=" + a;
        a++;
    }

点击图片  切换验证码    方正验证码 图片重复  打开的验证码图片 传个值  这个值用不到

就可以实现  图片 无限刷新

toUpperCase  实现验证码大小写转换

原文地址:https://www.cnblogs.com/v587yy/p/6971356.html