.net生成二维码图片

必须dll       Gma.QrCodeNet.Encoding.dll

 

        /// <summary>
        /// 将data转换为二维码
        /// </summary>
        /// <param name="data"></param>
        private void SaveCodeFile(string data)
        {
            QrEncoder qrEncoder = new QrEncoder(ErrorCorrectionLevel.H);
            QrCode code = new QrCode();
            qrEncoder.TryEncode(data, out code);
            const int modelSizeInPixels = 4;

            //绘制二维码
            GraphicsRenderer render = new GraphicsRenderer(new FixedModuleSize(modelSizeInPixels, QuietZoneModules.Two)
                , Brushes.Black, Brushes.White);

            //保存路径
            string filename = @"C:\Users\Administrator\Desktop\QrCode.Net 0.4 Pre-Release/" + Guid.NewGuid() +".png";

            //执行保存图片
            using (FileStream stream = new FileStream(filename, FileMode.Create))
            {
                render.WriteToStream(code.Matrix, System.Drawing.Imaging.ImageFormat.Png, stream);
            }

            //pictureBox1展示图片
            this.pictureBox1.Image = Image.FromFile(filename); 
      
        }

  

 

posted @ 2017-02-23 12:03  人生为卒  阅读(317)  评论(0编辑  收藏  举报