EntityFramework图片水印与验证码

EntityFramework图片水印

基本步骤:

  1. 使用Visual Studio 新建 ASP.NET 应用程序(.NET Framework)项目;
  2. 添加一个Web窗体和一个存放图片的images文件夹;
  3. 创建两个类分别用来编写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中实现验证码的步骤比较麻烦,一旦错了一步后面的代码就不好继续编写了

  1. 使用Visual Studio 新建 ASP.NET 应用程序(.NET Framework)项目;
  2. 添加一个Web窗体;
  3. 创建一个类用来编写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编写实现验证码代码

步骤:

  1. 生产随机数
  2. 定义字符串
  3. 循环生成五个字符
  4. 定宽度
  5. 生成图片
  6. 背景颜色
  7. 定义随机数
  8. 随机输出噪点
  9. 存储到内存中
  10. 保存Jpeg文件
  11. 清除缓存中所有输出

详细代码如下:

 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)
            //{
            //    //执行操作
            //}

以上就是在EF中给图片添加水印、在EF中实现验证码的编写过程

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值