WPF 本地生成验证码

1、效果如下图,点击图片可更新验证码(其实图片就是一个Button的背景图)。

2、主要使通过用户控件创建,UCVerificationCode.xaml代码如下。

XML 复制代码
<UserControl x:Class="UC.UCVerificationCode"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:local="clr-namespace:RegisterWPF.UC"
             xmlns:hc="https://handyorg.github.io/handycontrol"
             mc:Ignorable="d" 
             Width="Auto"
             Height="Auto" Loaded="UCVerificationCode_OnLoaded">
    <Grid>     
        <Button x:Name="btnVerificationCode" Width="100" Height="40" BorderThickness="0" Click="BtnVerificationCode_OnClick"/>
    </Grid>
</UserControl>

3、UCVerificationCode.xaml.cs代码如下。

cs 复制代码
    /// <summary>
    /// UCVerificationCode.xaml 的交互逻辑
    /// </summary>
    public partial class UCVerificationCode : UserControl
    {
        public string VerificationCode { get; set; }
        public UCVerificationCode()
        {
            InitializeComponent();
        }
        /// <summary>
        /// 用户控件加载完成
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void UCVerificationCode_OnLoaded(object sender, RoutedEventArgs e)
        {
            GetVerificationCode();
        }
        /// <summary>
        /// 获取验证码
        /// </summary>
        public void GetVerificationCode()
        {
            using (MemoryStream outStream = new MemoryStream())
            {
                Bitmap bitmap = CreateVerificationCode(out string code);
                bitmap.Save(outStream, ImageFormat.Bmp);
                BitmapImage bitmapImage = new BitmapImage();
                bitmapImage.BeginInit();
                bitmapImage.StreamSource = new MemoryStream(outStream.ToArray());
                bitmapImage.EndInit();
                ImageBrush imgBrush = new ImageBrush(bitmapImage);
                btnVerificationCode.Background = imgBrush;
                VerificationCode = code;
                Debug.WriteLine(code);
                Debug.WriteLine(VerificationCode);
            }
        }
        /// <summary>
        /// 生成验证码图片
        /// </summary>
        /// <param name="code"></param>
        /// <returns></returns>
        public static Bitmap CreateVerificationCode(out string code)
        {
            //建立Bitmap对象,绘图
            Bitmap bitmap = new Bitmap(200, 60);
            Graphics graph = Graphics.FromImage(bitmap);
            graph.FillRectangle(new SolidBrush(System.Drawing.Color.White), 0, 0, 200, 60);
            Font font = new Font(System.Drawing.FontFamily.GenericSansSerif,  48,System.Drawing.FontStyle.Bold, GraphicsUnit.Pixel);
            Random r = new Random();
            string letters = "ABCDEFGHIJKLMNPQRSTUVWXYZ0123456789";

            StringBuilder sb = new StringBuilder();

            //添加随机的五个字母
            for (int x = 0; x < 4; x++)
            {
                string letter = letters.Substring(r.Next(0, letters.Length - 1), 1);
                sb.Append(letter);
                graph.DrawString(letter, font, new SolidBrush(Color.Black), x * 38, r.Next(0, 15));
            }
            code = sb.ToString();

            //混淆背景
            Pen linePen = new Pen(new SolidBrush(Color.Black), 2);
            for (int x = 0; x < 6; x++)
                graph.DrawLine(linePen, new Point(r.Next(0, 199), r.Next(0, 59)), new Point(r.Next(0, 199), r.Next(0, 59)));
            return bitmap;
        }

        /// <summary>
        /// 刷新验证码
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void BtnVerificationCode_OnClick(object sender, RoutedEventArgs e)
        {
            GetVerificationCode();
        }
    }

4、引用时。

XML 复制代码
<localuc:UCVerificationCode VerticalAlignment="Center" HorizontalContentAlignment="Center" Margin="0" Padding="0" BorderThickness="0"/>
相关推荐
军训猫猫头7 小时前
5.浏览本地文件获取路径与文件名称 C#例子 WPF例子
开发语言·c#·wpf
SEO-狼术14 小时前
DevExtreme Complete Crack,responsive UI widgets
ui
步、步、为营15 小时前
.NET + WPF框架开发聊天、网盘、信息发布、视频播放功能
.net·wpf·音视频
ui设计前端开发老司机18 小时前
从数据洞察到设计创新:UI前端如何利用数字孪生提升产品交互体验?
前端·ui·交互
code bean1 天前
【WPF实战】MVVM中如何从数据模型反查自定义控件实例(ImageView + Halcon)
wpf
lph19721 天前
ValueConverter转换器WPF
wpf
Sally璐璐1 天前
Memcache核心技术解析与实战应用
运维·wpf·memcached
悟能不能悟1 天前
Dubbo跨越分布式事务的最终一致性陷阱
分布式·wpf·dubbo
I'mSQL1 天前
C#与FX5U进行Socket通信
运维·服务器·自动化·wpf
前端开发与ui设计的老司机2 天前
从UI设计到数字孪生实战应用:构建智慧金融的风险评估与预警平台
ui·金融