zoukankan      html  css  js  c++  java
  • 【WPF】生成二维码

    第一步,下载Google的ZXing类库,以便引用;

      BitMatrix bitMatrix;
        
            private void Button_Click_1(object sender, RoutedEventArgs e)
            {
                string content = this.txtChat.Text;
    
    
                    Dictionary<EncodeHintType, object> hints = new Dictionary<EncodeHintType, object>();
                    hints.Add(EncodeHintType.CHARACTER_SET, "UTF-8");
                    
                    bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, 200, 200);
                    this.img.Stretch = Stretch.Fill;
                    this.img.Source = toImage(bitMatrix);
            }

    下面代码是不同的转换

     private BitmapImage toImage(BitMatrix matrix)
            {
                try
                {
                    int width = matrix.Width;
                    int height = matrix.Height;
    
                    Bitmap bmp = new Bitmap(width, height);
                
                   // byte[] pixel = new byte[width * height];
    
                    for (int x = 0; x <height ; x++)
                    {
                        for (int y = 0; y < width; y++)
                        {
                            if (bitMatrix[x, y])
                            {
                                bmp.SetPixel(x,y,System.Drawing.Color.Black);
                            }
                            else
                            {
                                bmp.SetPixel(x, y, System.Drawing.Color.White);
                            }
                        }
                    }
    
                    return ConvertBitmapToBitmapImage(bmp);
                }
                catch (Exception ex)
                {
                    throw ex;
                }
               
            }
            private static BitmapImage ConvertBitmapToBitmapImage(Bitmap wbm)
            {
                BitmapImage bimg = new BitmapImage();
                
                using (MemoryStream stream = new MemoryStream())
                {
                    wbm.Save(stream , System.Drawing.Imaging.ImageFormat.Png);
    
                    bimg.BeginInit();
                    stream.Seek(0, SeekOrigin.Begin);
                    bimg.StreamSource = stream;
                    bimg.CacheOption = BitmapCacheOption.OnLoad;
                    bimg.EndInit();
                }
                return bimg;
    
            }

     希望对你有一点点帮助!

  • 相关阅读:
    常用数据结构的应用场景
    数组与链表的对比
    [LeetCode 293] Flip Game
    [Leetcode] Palindrome Permutation 回文变换
    九大排序算法再总结
    query函数的可查询数据
    Column常用的参数
    sqlalchemy的常用字段
    sqlalchemy基本的增删改查
    sqlalchemy映射数据库
  • 原文地址:https://www.cnblogs.com/wywnet/p/3559917.html
Copyright © 2011-2022 走看看