验证图片

 

1、画图片(子程序中的代码)CheckCode.aspx.cs
private void Page_Load(object sender, System.EventArgs e)
  {
   this.CreateCheckCodeImage(GenerateCheckCode());
  }

  private string GenerateCheckCode()
  {
   int number;
   char code;
   string checkCode = String.Empty;

   System.Random random = new Random();

   for(int i=0; i<5; i++)
   {
    number = random.Next();

    if(number % 2 == 0)
     code = (char)('0' + (char)(number % 10));
    else
     code = (char)('A' + (char)(number % 26));

    checkCode += code.ToString();
   }

   Response.Cookies.Add(new HttpCookie("CheckCode", checkCode));

   return checkCode;
  }

  private void CreateCheckCodeImage(string checkCode)
  {
   if(checkCode == null || checkCode.Trim() == String.Empty)
    return;

   System.Drawing.Bitmap image = new System.Drawing.Bitmap((int)Math.Ceiling((checkCode.Length * 12.5)), 22);
   Graphics g = Graphics.FromImage(image);

   try
   {
    //生成随机生成器
    Random random = new Random();

    //清空图片背景色
    g.Clear(Color.White);

    //画图片的背景噪音线
    for(int i=0; i<25; i++)
    {
     int x1 = random.Next(image.Width);
     int x2 = random.Next(image.Width);
     int y1 = random.Next(image.Height);
     int y2 = random.Next(image.Height);

     g.DrawLine(new Pen(Color.Silver), x1, y1, x2, y2);
    }

    Font font = new System.Drawing.Font("Arial", 12, (System.Drawing.FontStyle.Bold | System.Drawing.FontStyle.Italic));
    System.Drawing.Drawing2D.LinearGradientBrush brush = new System.Drawing.Drawing2D.LinearGradientBrush(new Rectangle(0, 0, image.Width, image.Height), Color.Blue, Color.DarkRed, 1.2f, true);
    g.DrawString(checkCode, font, brush, 2, 2);

    //画图片的前景噪音点
    for(int i=0; i<100; i++)
    {
     int x = random.Next(image.Width);
     int y = random.Next(image.Height);

     image.SetPixel(x, y, Color.FromArgb(random.Next()));
    }

    //画图片的边框线
    g.DrawRectangle(new Pen(Color.Silver), 0, 0, image.Width - 1, image.Height - 1);

    System.IO.MemoryStream ms = new System.IO.MemoryStream();
    image.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
    Response.ClearContent();
    Response.ContentType = "image/Gif";
    Response.BinaryWrite(ms.ToArray());
   }
   finally
   {
    g.Dispose();
    image.Dispose();
   }
  }
2、主程序中的代码(页面:<IMG src="CheckCode.aspx"><asp:Label ID="lblMessage" runat="server" Visible="false" Style="color: #cc0000;"></asp:Label>)
后台:
protected void Button1_Click(object sender, EventArgs e)
    {
        if (Request.Cookies["CheckCode"] == null)
        {
            lblMessage.Text = "您的浏览器设置已被禁用 Cookies,您必须设置浏览器允许使用 Cookies 选项后才能使用本系统。";
            lblMessage.Visible = true;
            return;
        }

        if (String.Compare(Request.Cookies["CheckCode"].Value, txtrand.Text, true) != 0)
        {
            lblMessage.Text = "认证码错误";
            lblMessage.Visible = true;
            return;
        }
        else
        {
            AddInfo();
            Response.Write("<script language=javascript> alert('举报成功,等待审批!');window.location='../../../index.htm'</script>");
        }
    }

Python可以使用第三方库Pillow来还原和验证图片。Pillow是一个Python图像处理库,它提供了许多函数和方法来处理图像。要还原和验证图片,可以按照以下步骤进行操作: 1. 导入Pillow库:首先,我们需要在Python脚本中导入Pillow库。可以使用以下代码将其导入: ```python from PIL import Image ``` 2. 打开图片:使用Pillow库中的open函数来打开要处理的图片文件。例如,可以使用以下代码来打开一张图片: ```python image = Image.open('image.jpg') ``` 3. 还原图片:利用Pillow库的函数和方法,可以对打开的图片进行还原处理。具体的还原方法取决于图片的损坏情况和需要还原的内容。例如,可以使用Pillow库的修复函数来修复损坏的图片: ```python repaired_image = image.repair() ``` 4. 验证图片:还原后的图片可以与原始图片进行比较,以验证还原的效果。可以使用Pillow库的compare函数来比较两张图片的差异。例如,可以使用以下代码来比较两张图片的差别: ```python diff = ImageChops.difference(original_image, repaired_image) ``` 如果差异很小,说明还原的效果较好;如果差异较大,说明还原的效果不理想。 5. 保存图片:最后,可以使用Pillow库的save函数将还原后的图片保存到新的文件中。例如,可以使用以下代码将还原后的图片保存为新的文件: ```python repaired_image.save('repaired_image.jpg') ``` 综上所述,可以使用Python和Pillow库来还原和验证图片。根据图片的损坏情况和需要还原的内容,可以调用Pillow库中的函数和方法进行相应的处理,然后将还原后的图片与原始图片进行比较以验证还原效果的好坏。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值