• C# WPF使用ZXing生成二维码ImageSource


    介绍:

    如果需要实在WPF窗体程序中现类似如下的二维码图片生成功能,可以通过本文的方法实现

    添加步骤:

    1、在http://zxingnet.codeplex.com/站点上下载ZXing .Net的第三方库

    2、下载后解压可以看到有针对不同.Net版本的dll文件,在你的工程中引用正确的dll

    3、然后再你的工程中引用System.Drawing程序集

    4、在你需要生成二维码的Window中,加入一下代码

    // 注销对象方法API
    [DllImport("gdi32")]
    static extern int DeleteObject(IntPtr o);
    /**
     * 创建二维码图片
     */
    private ImageSource createQRCode(String content, int width, int height)
    {
        EncodingOptions options;//包含一些编码、大小等的设置
        BarcodeWriter write = null;//用来生成二维码,对应的BarcodeReader用来解码
        options = new QrCodeEncodingOptions
        {
            DisableECI = true,
            CharacterSet = "UTF-8",
            Width = width,
            Height = height,
            Margin = 0
        };
        write = new BarcodeWriter();
        write.Format = BarcodeFormat.QR_CODE;
        write.Options = options;
        Bitmap bitmap = write.Write(content);
        IntPtr ip = bitmap.GetHbitmap();
        BitmapSource bitmapSource = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(
            ip, IntPtr.Zero, Int32Rect.Empty,
            System.Windows.Media.Imaging.BitmapSizeOptions.FromEmptyOptions());
        DeleteObject(ip);
        return bitmapSource;
    }

    5、调用createQRCode即可完成二维码的ImageSource生成,然后使用Image即可显示

  • 相关阅读:
    V2EX 上收藏Top200
    在heroku上部署gost代理服务端
    nano编辑器使用教程
    Amazon EC2免费VPS防止超额被扣钱三大方法:流量 硬盘读写 运行时长
    Go语言开发环境配置
    HTML5 and CSS3 开发
    使用 Eclipse PhoneGap 构建 Android 应用程序入门
    脚本之家
    CSS网页布局全精通
    使用面向 iOS 的本机插件扩展 PhoneGap
  • 原文地址:https://www.cnblogs.com/halfmanhuang/p/4077908.html
Copyright © 2020-2023  润新知