zoukankan      html  css  js  c++  java
  • C#工具:WPF生成图片验证码

    1.使用ImageFormatConvertHelper

    using System;
    using System.Collections.Generic;
    using System.Drawing;
    using System.Linq;
    using System.Runtime.InteropServices;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows;
    using System.Windows.Media;
    using System.Windows.Media.Imaging;
    
    namespace YongAn_WPF
    {
        public class ImageFormatConvertHelper
        {
            [DllImport("gdi32.dll", SetLastError = true)]
            private static extern bool DeleteObject(IntPtr hObject);
            /// <summary>  /// 
            /// 从bitmap转换成ImageSource  /// 
            /// </summary>  /// 
            /// <param name="icon"></param>
            /// <returns></returns>  
            public static ImageSource ChangeBitmapToImageSource(Bitmap bitmap)
            { //Bitmap bitmap = icon.ToBitmap();  
                IntPtr hBitmap = bitmap.GetHbitmap();
                ImageSource wpfBitmap = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(hBitmap, IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions());
                if (!DeleteObject(hBitmap))
                { throw new System.ComponentModel.Win32Exception(); }
                return wpfBitmap;
            }
        }
    }

    2.使用VerifyCodeHelper

    using System;
    using System.Collections.Generic;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace YongAn_WPF
    {
        public class VerifyCodeHelper
        {
            public static Bitmap CreateVerifyCode(out string code)
            { //建立Bitmap对象,绘图 
                Bitmap bitmap = new Bitmap(200, 60); Graphics graph = Graphics.FromImage(bitmap);
                graph.FillRectangle(new SolidBrush(Color.White), 0, 0, 200, 60);
                Font font = new Font(FontFamily.GenericSerif, 48, FontStyle.Bold, GraphicsUnit.Pixel);
                Random r = new Random(); string letters = "ABCDEFGHIJKLMNPQRSTUVWXYZ0123456789";
                StringBuilder sb = new StringBuilder();
                //添加随机的五个字母
                for (int x = 0; x < 5; 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;
            }
        }
    }

    3.调用方式 返回Code(验证码内容)

            public string GetImage()
            {
                string code = "";
                Bitmap bitmap = VerifyCodeHelper.CreateVerifyCode(out code);
                ImageSource imageSource = ImageFormatConvertHelper.ChangeBitmapToImageSource(bitmap);
                img.Source = imageSource;
                return code;
            }
  • 相关阅读:
    百度地图 如何制作泡泡放大镜?
    百度地图 孪生姐妹地图
    百度地图Api进阶教程-点聚合9.html
    百度地图Api进阶教程-实例高级操作8.html
    百度地图Api进阶教程-用户自定义数据(标记和搜索)7.html
    百度地图Api进阶教程-地图鼠标左右键操作实例和鼠标样式6.html
    百度地图Api进阶教程-弹出信息窗口5.html
    百度地图Api进阶教程-点击生成和拖动标注4.html
    app设计需注意的
    JS移动客户端--触屏滑动事件 banner图效果
  • 原文地址:https://www.cnblogs.com/liuyuanjiao/p/10623265.html
Copyright © 2011-2022 走看看