標籤:
浮水印:一個fileupload一個button
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Drawing;
using System.IO;
public partial class 浮水印 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void shangchuan_Click(object sender, EventArgs e)
{
//一、從上傳資料中,轉化成圖片對象。
Stream s = FileUpload1.FileContent;
System.Drawing.Image img = System.Drawing.Image.FromStream(s);//重點
//二、對圖片對象進行畫浮水印
//1.造筆
SolidBrush brush=new SolidBrush (Color.Yellow);
//2.造字型
Font f = new Font("Buxton Sketch", 12);
//3.找到映像繪圖區域
Graphics gs = Graphics.FromImage(img);
//4.確定開始畫位置
float x = 0, y = 0;
SizeF size = gs.MeasureString("happy new year!", f);//確定書寫的浮水印(即要寫的字)的大小
x = img.Width-size.Width;
y = img.Height-size.Height;
//5.開始畫
gs.DrawString("happy new year!", f, brush, x, y);
//三、圖片對象另存到硬碟上去
string app = FileUpload1.FileName;
string path = Server.MapPath("/upload/")+app;
img.Save(path);
}
}
驗證碼:
顯示頁面(出現驗證碼,cs代碼中夾雜驗證碼的驗證):
aspx頁面(script中是點擊驗證碼後改變原來的隨機數):
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Denglu.aspx.cs" Inherits="Denglu" %>
<!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>
<script language="javascript">
function Bianhua()
{
var img = document.getElementById("Image1");
img.setAttribute("src", "驗證碼.aspx?id=" + Math.random());
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:Image ID="Image1" runat="server" ImageUrl="驗證碼.aspx" onclick="Bianhua()"/>
<br />
<asp:Button ID="Deng" runat="server" Text="登入" />
</div>
</form>
</body>
</html>
驗證碼的產生圖片頁面:
aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Drawing;
using System.IO;
public partial class 驗證碼 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Bitmap img = new Bitmap(70, 30);//建立一個新的位元影像
string suijishu = "qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM1234567890";
//所有隨機產生的字元的集合字串
Random ran = new Random();
string yzm = "";
for (int i = 0; i < 4; i++)
{
int a = ran.Next(suijishu.Length);
yzm += suijishu.Substring(a,1);
}
//以上是產生4個隨機的字元組成一個擁有4個字元的字串
Session["code"] = yzm;
//將隨機產生的字串記錄下來,供使用者填寫時驗證
//、、、以下是將產生的字串浮水印到產生的位元影像上,(由於位元影像預設背景顏色為黑色)
SolidBrush brush = new SolidBrush(Color.Green);//勾勒畫筆顏色
Font fo = new Font("SketchFlow Print", 18);//描述字型以及大小
Graphics gs = Graphics.FromImage(img);//描述要浮水印的圖片,即新產生的位元影像
gs.DrawString(yzm, fo, brush, 0, 0);//勾畫浮水印
//把圖片儲存到輸出資料流中去
img.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);
Response.End();
}
}
或者驗證碼的背景圖片為白色:
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)
{
//造一個圖片
Bitmap img = new Bitmap(60, 30);
//產生個隨機數
string str = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
Random rand = new Random();
string code = "";
for (int i = 0; i < 4; i++)
{
int start = rand.Next(str.Length);
code += str.Substring(start, 1);
}
Session["yzm"] = code;
//把隨機數畫到圖片上
SolidBrush brush = new SolidBrush(Color.White);
Font font = new Font("Lucida Sans Unicode", 14);
Graphics g = Graphics.FromImage(img);
//a.把圖片背景塗白
g.FillRectangle(brush, 0, 0, 60, 30);
//b.給畫筆換個顏色
brush.Color = Color.Red;
g.DrawString(code, font, brush, 0, 0);
//把圖片儲存到輸出資料流中去
img.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);
Response.End();
}
}
C#--web浮水印+++++驗證碼