zoukankan      html  css  js  c++  java
  • 英文验证码(转)

    前几天看的别人写的英文验证码的帖子 自己就引用了  然后整理了一下

    xaml代码是:

    复制代码
    <Window x:Class="SecurityCode.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
            xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
            xmlns:local="clr-namespace:SecurityCode"
            mc:Ignorable="d"
            x:Name="usercontrol"
            Title="MainWindow" MaxHeight="100" Width="900">
        <Grid>
            <StackPanel Height="50" Orientation="Horizontal">
                <Label Content="验证码"></Label>
                <TextBox  x:Name="SecurityTxt"  Width="200">
                </TextBox>
                <Button Content="发送" Click="Button_Click"></Button>
                <Image Width="400" Stretch="None" x:Name="image"></Image>
                <Button Content="重新获取验证码" Click="MainWindow_Loaded"></Button>
            </StackPanel>
        </Grid>
    </Window>
    复制代码

    引用的类是:

    复制代码
    using System;
    using System.Collections.Generic;
    using System.Drawing;
    using System.Drawing.Imaging;
    using System.IO;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Media;
    using System.Windows.Shapes;
    
    namespace SecurityCode
    {
        public class EnglishSecurityCode
        {
    
    
            /// <summary>
            /// 英文字符验证码
            /// </summary>
            /// <param name="传出验证码"></param>
            /// <param name="验证码长度"></param>
            /// <returns></returns>
            public static byte[] GraphCode(out string code, int codeLength)
            {
                int Width = 80;//默认为100
                int Height = 30;//默认为40
                int FontSize = 20;//默认为20
                byte[] bytes = GraphCode(out code, codeLength, Width, Height, FontSize);
                return bytes;
            }
            /// <summary>
            /// 产生图形验证码
            /// </summary>
            /// <param name="传出验证码"></param>
            /// <param name="验证码字符"></param>
            /// <param name="Width"></param>
            /// <param name="Height"></param>
            /// <param name="FontSize"></param>
            /// <returns></returns>
            public static byte[] GraphCode(out string Code, int CodeLength, int Width, int Height, int FontSize)
            {
                string code = string.Empty;
                string sCode = string.Empty;
                System.Drawing.Color[] oColor ={
                System.Drawing.Color.Red,
                System.Drawing.Color.Black,
                System.Drawing.Color.Bisque,
                System.Drawing.Color.White,
                System.Drawing.Color.Blue,
                };
                string[] oFontNames = { "Times New Roman", "MS Mincho", "Book Antiqua", "Gungsuh", "PMingLiU", "Impact" };
                //验证码的字元集
                char[] oCharacter =
                {
                      'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z',
                      'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'
                  };
                Random oRnd = new Random();
                Bitmap oBmp = null;
                Graphics oGraphics = null;
                int N1 = 0;
                //生成验证码字串
                for (N1 = 0; N1 <= CodeLength - 1; N1++)
                {
                    sCode += oCharacter[oRnd.Next(oCharacter.Length)];
                }
                oBmp = new Bitmap(Width, Height);
                oGraphics = Graphics.FromImage(oBmp);
                oGraphics.Clear(System.Drawing.Color.White);
                try
                {
                    oGraphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.ClearTypeGridFit;
                    Font f = new System.Drawing.Font("宋体", FontSize, (System.Drawing.FontStyle.Bold));
                    // 前景色
                    System.Drawing.Brush b = new System.Drawing.SolidBrush(System.Drawing.Color.Black);
                    // 背景色
                    oGraphics.Clear(System.Drawing.Color.White);
                    // 填充文字
                    for (int i = 0; i < sCode.Length; i++)
                    {
                        oGraphics.DrawString(sCode[i].ToString(), f, b, new System.Drawing.Rectangle(i * (FontSize - 2), 3, (FontSize - 2), Height));
                    }
                    // 随机线条
                    System.Drawing.Pen linePen = new System.Drawing.Pen(System.Drawing.Color.Gray, 0);
                    Random rand = new Random();
                    for (int i = 0; i < 3; i++)
                    {
                        int x1 = rand.Next(oBmp.Width);
                        int y1 = rand.Next(oBmp.Height);
                        int x2 = rand.Next(oBmp.Width);
                        int y2 = rand.Next(oBmp.Height);
                        oGraphics.DrawLine(linePen, x1, y1, x2, y2);
                    }
    
                    // 随机点
                    for (int i = 0; i < 30; i++)
                    {
                        int x = rand.Next(oBmp.Width);
                        int y = rand.Next(oBmp.Height);
                        oBmp.SetPixel(x, y, System.Drawing.Color.FromArgb(rand.Next(250), rand.Next(250), rand.Next(250)));
                    }
    
                    Code = sCode;
                    //保存图片数据
                    MemoryStream stream = new MemoryStream();
                    oBmp.Save(stream, ImageFormat.Jpeg);
                    //输出图片流
                    return stream.ToArray();
                }
                finally
                {
                    oGraphics.Dispose();
                }
    
    
            }
        }
    }
    复制代码

    后台代码是:

    using System;
    using System.Collections.Generic;
    using System.Drawing;
    using System.IO;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows;
    using System.Windows.Data;
    using System.Windows.Documents;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Imaging;
    using System.Windows.Navigation;
    using System.Windows.Shapes;

    namespace SecurityCode
    {
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
    public MainWindow()
    {
    InitializeComponent();
    this.Loaded += MainWindow_Loaded;
    }
    public static string Code = "";
    byte[] englishchar;
    private void MainWindow_Loaded(object sender, RoutedEventArgs e)
    {
    englishchar = EnglishSecurityCode.GraphCode(out Code, 4);
    ImageSecurity = BytesToBitmapImage(englishchar);
    this.image.Source = ImageSecurity;
    }

    public ImageSource ImageSecurity
    {
    get { return (ImageSource)GetValue(ImageSecurityProperty); }
    set { SetValue(ImageSecurityProperty, value); }
    }
    public static readonly DependencyProperty ImageSecurityProperty =
    DependencyProperty.Register("ImageSecurity", typeof(ImageSource), typeof(MainWindow), new PropertyMetadata(null));
    public static BitmapImage BytesToBitmapImage(byte[] buffer)
    {
    var bitmap = new BitmapImage();
    bitmap.CreateOptions = BitmapCreateOptions.IgnoreImageCache;
    bitmap.BeginInit();
    bitmap.StreamSource = new MemoryStream(buffer);
    bitmap.EndInit();
    return bitmap;

    }
    private void Button_Click(object sender, RoutedEventArgs e)
    {
    var str = this.SecurityTxt.Text.ToString().ToLower();
    if (str == Code.ToLower())
    {
    MessageBox.Show("验证正确");
    }
    else
    {
    MessageBox.Show("验证错误");
    }


    }
    }
    }

     
     
     
    好文要顶 关注我
  • 相关阅读:
    UltraEdit 注册机使用说明
    sybase 收集常用sql语句
    过渡模式
    JavaScript 钩子
    自定义过渡的类名
    CSS过渡、CSS动画
    单元素/组件的过渡
    通过 v-once 创建低开销的静态组件
    内联模板、X-Template
    程序化的时间侦听器
  • 原文地址:https://www.cnblogs.com/ExMan/p/5787699.html
Copyright © 2011-2022 走看看