制作水印:
1 //1、先把图片获取出来 2 //咱们要画画的画布 3 System.Drawing.Image img = System.Drawing.Image.FromStream(FileUpload1.FileContent); 4 5 //2、往上面画上水印 6 Graphics g = Graphics.FromImage(img);//这个是绘制方法,等号右边是往那张图片上绘制 7 string s = "http://www.itnba.com"; //绘制的字符串 8 Font f = new Font("微软雅黑",18);//绘制使用的字体 9 SolidBrush b = new SolidBrush(Color.Red);//绘制使用的刷子,设置颜色 10 11 g.DrawString(s, f, b, 0, 0); //绘制完毕 12 13 //3、保存到服务器指定路径,并且在Image控件中显示 14 string path = Server.MapPath("Uploads/" + FileUpload1.FileName); 15 16 img.Save(path);//将绘制完的图片保存,注意:千万不要把选择的图片保存 17 18 Image1.ImageUrl = "Uploads/" + FileUpload1.FileName;
图片验证码:
//1、准备画布 Bitmap img = new Bitmap(60, 30); //2、往画布上绘制验证码 string all = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"; string aaa = ""; Random r = new Random(); for (int i = 0; i < 4; i++) { aaa += all.Substring(r.Next(all.Length), 1); } Session["YZM"] = aaa; Graphics g = Graphics.FromImage(img); Font f = new Font("微软雅黑",16); SolidBrush b = new SolidBrush(Color.Green); g.DrawString(aaa, f, b, 0, 0); img.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Png);//将图像以指定格式通过流的方式输出
调用验证码直接用图片控件src指向这个界面即可
点击改变验证码:
1 var aaa = 0; 2 document.getElementById('Image1').onclick = function () { 3 this.setAttribute("src", "yzm.aspx?id="+aaa);//地址需要有变化所以随便给他一个传值,地址一样图片不会改变 4 aaa++; 5 }