EntityFramework图片水印
基本步骤:
- 使用Visual Studio 新建 ASP.NET 应用程序(.NET Framework)项目;
- 添加一个Web窗体和一个存放图片的images文件夹;
- 创建两个类分别用来编写HttpModule和HttpHandler代码
代码编辑
Web窗体中编写一个
<img src="images/img_1.jpg" />
HttpModule中编写以下代码
public class MyModule : IHttpModule
{
public void Dispose()
{
}
public void Init(HttpApplication context)
{
context.BeginRequest += Context_BeginRequest;
context.EndRequest += Context_EndRequest;
}
private void Context_EndRequest(object sender, EventArgs e)
{
HttpApplication application = sender as HttpApplication;
}
private void Context_BeginRequest(object sender, EventArgs e)
{
HttpApplication application = sender as HttpApplication;
}
}
HttpHandler中编写添加水印的过程步骤
public class MyHandler : IHttpHandler
{
public bool IsReusable => false;
public void ProcessRequest(HttpContext context)
{
string uri = context.Request.PhysicalPath;
Bitmap bitmap = new Bitmap(uri);
Graphics graphics = Graphics.FromImage(bitmap);
graphics.DrawString("这是水印", new Font("微软雅黑", 20, FontStyle.Bold), Brushes.Red, new Point(0, 0));
graphics.Flush();
bitmap.Save(context.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);
}
}
Web.config中编写
编写在configuration里面!
<system.webServer>
<handlers>
<add name="test" verb="*" path="images/*" type="WebApplication1.MyHandler"/>
</handlers>
</system.webServer>
在EF中怎么在图片中添加水印到这里就能实现了
EntityFramework实现验证码
在EF中实现验证码的步骤比较麻烦,一旦错了一步后面的代码就不好继续编写了
- 使用Visual Studio 新建 ASP.NET 应用程序(.NET Framework)项目;
- 添加一个Web窗体;
- 创建一个类用来编写HttpHandler代码;
在Web窗体中添加用来存放验证码
<h1>实现验证码</h1>
<asp:Label ID="Label1" runat="server" Text="验证码"></asp:Label>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<img src="Validatecodehandler.ashx?id=1" onclick="this.src=this.src+1" />
在HttpHandler编写实现验证码代码
步骤:
- 生产随机数
- 定义字符串
- 循环生成五个字符
- 定宽度
- 生成图片
- 背景颜色
- 定义随机数
- 随机输出噪点
- 存储到内存中
- 保存Jpeg文件
- 清除缓存中所有输出
详细代码如下:
public class MyHandler : IHttpHandler,IRequiresSessionState
{
public bool IsReusable => false;
//生产随机数
private Random random = new Random();
public void ProcessRequest(HttpContext context)
{
//定义字符串
string num = "ABCDEFGHIGKLMNOPQRSTUVWXYZabcdefghigklmnopqrstuvwxyz1234567890";
string numstr = num;
//循环生成五个字符
for (int i = 0; i < 5; i++)
{
numstr += num[random.Next(0, num.Length)];
}
//将验证码存入session中
context.Session["Code"] = numstr.ToLower();
CreateImages(context, numstr);
}
private void CreateImages(HttpContext context, string checkCode)
{
//定宽度
int iwidth = (int)(checkCode.Length * 13);
System.Drawing.Bitmap image = new System.Drawing.Bitmap(iwidth, 22);
//生成图片
Graphics g = Graphics.FromImage(image);
//背景颜色
g.Clear(Color.White);
//定义颜色
Color[] c = { Color.Black, Color.DarkBlue, Color.Orange, Color.Purple, Color.Red, Color.Green, Color.Gray, Color.Yellow };
//定义字体
string[] font = { "Verdana", "Microsoft Sans Serif", "宋体", "Arial", "黑体" };
//定义随机数
Random rand = new Random();
//随机输出噪点
for (int i = 0; i < 50; i++)
{
int x = rand.Next(image.Width);
int y = rand.Next(image.Height);
g.DrawRectangle(new Pen(Color.LightGray, 0), x, y, 1, 1);
}
for (int i = 0; i < checkCode.Length; i++)
{
//随机颜色
int cindex = rand.Next(7);
//随机字体
int findex = rand.Next(5);
//刷字体
Font f = new Font(font[findex], 10, System.Drawing.FontStyle.Bold);
//颜色
Brush b = new System.Drawing.SolidBrush(c[cindex]);
int ii = 4;
if ((i+1)%2==0)
{
ii = 2;
}
//绘制图片
g.DrawString(checkCode.Substring(i, 1), f, b, 2 + (i * 12), ii);
}
//绘制边框
g.DrawRectangle(new Pen(ColorTranslator.FromHtml("#CCCCCC"), 0), 0, 0, image.Width - 1, image.Height - 1);
//存储到内存中
MemoryStream ms = new MemoryStream();
//保存Jpeg文件
image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
//清除缓存中所有输出
context.Response.ClearContent();
context.Response.ContentType = "image/gif";
context.Response.BinaryWrite(ms.ToArray());
//释放内存
g.Dispose();
image.Dispose();
}
}
后端取出session与文本框判断即可
//string yanzheng = TextBox1.Text;
//string yanz = Session["Code"].ToString();
//if (yanz == yanzheng)
//{
// //执行操作
//}