C#生成二维码

 /*
         * 二维码生成方法(编程语言C#,java语言与其类似)
         * author:LiuC
          **/
        protected void Button2_Click(String fileName1, String QRCodePath, String filePath, String fileSaveName)
        {
            /*
             * QRCodePath--二维码保存路径,filePath---二维码对应的文件路径,fileSaveName--文件名和二维码保存名称
             * */
            QRCodeEncoder qrCodeEncoder = new QRCodeEncoder();
            qrCodeEncoder.QRCodeEncodeMode = QRCodeEncoder.ENCODE_MODE.BYTE;//
            qrCodeEncoder.QRCodeScale = 4;//设置扫描二维码识别的类型
            qrCodeEncoder.QRCodeVersion = 8;//
            qrCodeEncoder.QRCodeErrorCorrect = QRCodeEncoder.ERROR_CORRECTION.M;
            String data = "http://101.200.194.50:4580/"+filePath + fileSaveName;//获取文件的下载路径
            Response.Write(data);
            try//将文件下载路径写进二维码,扫描即可下载文件
            {
                System.Drawing.Bitmap image = qrCodeEncoder.Encode(data);
                System.IO.MemoryStream mStream = new System.IO.MemoryStream();//文件流
                image.Save(mStream, System.Drawing.Imaging.ImageFormat.Png);
                Response.ClearContent();
                Response.ContentType = "image/png";
                Response.BinaryWrite(mStream.ToArray());
                Directory.CreateDirectory(Server.MapPath(QRCodePath));
                byte[] by = mStream.ToArray();
                image.Save(Server.MapPath(QRCodePath) + fileName1);//保存生成的二维码照片
            }
            catch (UnauthorizedAccessException e1)
            {
                Page.ClientScript.RegisterStartupScript(this.GetType(), "message", "alert('程序异常')", true);//弹窗提示
                Console.WriteLine(e1.Message);
            }
            catch (System.IndexOutOfRangeException e3)
            {
                Page.ClientScript.RegisterStartupScript(this.GetType(), "message", "alert('程序异常')", true);//弹窗提示
                Console.WriteLine(e3.Message);
            }
            catch (ExternalException e2) {
                Page.ClientScript.RegisterStartupScript(this.GetType(), "message", "alert('程序异常')", true);//弹窗提示
                Console.WriteLine(e2.Message);
            }
        }

你可能感兴趣的:(二维码,c#)