예제 #1
0
파일: Form1.cs 프로젝트: Hagser/csharp
 private Bitmap GenerateQRCode(string URL, System.Drawing.Color DarkColor, System.Drawing.Color LightColor)
 {
     Form1.ActiveForm.Text = URL;
     var Encoder= new Gma.QrCodeNet.Encoding.QrEncoder(Gma.QrCodeNet.Encoding.ErrorCorrectionLevel.L);
     var Code = Encoder.Encode(URL);
     double hd = getDiff(256, Code.Matrix.Height);
     double wd = getDiff(256, Code.Matrix.Width);
     double fd = Math.Floor(Math.Min(hd, wd));
     int ifd = int.Parse(fd.ToString());
     var TempBMP = new Bitmap(256, 256);
     Graphics g = Graphics.FromImage(TempBMP);
     
     for (int X = 0; X < Code.Matrix.Width; X++)
     {
         for (int Y = 0; Y < Code.Matrix.Height;Y++ )
         {
             if (Code.Matrix.InternalArray[X, Y])
             {
                 g.FillRectangle(new SolidBrush(DarkColor), X * ifd.ToFloat(), Y * ifd.ToFloat(), ifd.ToFloat(), ifd.ToFloat());
             }
             else
             {
                 g.FillRectangle(new SolidBrush(LightColor), X * ifd.ToFloat(), Y * ifd.ToFloat(), ifd.ToFloat(), ifd.ToFloat());
             }
         }
     }
     
     return TempBMP;
 }
예제 #2
0
        public Image GetCode(string text)
        {
            var qr    = new Gma.QrCodeNet.Encoding.QrEncoder();
            var image = qr.GetQrCodeImage(text, 600, 600);

            return(new Bitmap(image, 200, 200));
        }
예제 #3
0
        private Bitmap GenerateQRCode(string URL, System.Drawing.Color DarkColor, System.Drawing.Color LightColor)
        {
            Form1.ActiveForm.Text = URL;
            var      Encoder = new Gma.QrCodeNet.Encoding.QrEncoder(Gma.QrCodeNet.Encoding.ErrorCorrectionLevel.L);
            var      Code    = Encoder.Encode(URL);
            double   hd      = getDiff(256, Code.Matrix.Height);
            double   wd      = getDiff(256, Code.Matrix.Width);
            double   fd      = Math.Floor(Math.Min(hd, wd));
            int      ifd     = int.Parse(fd.ToString());
            var      TempBMP = new Bitmap(256, 256);
            Graphics g       = Graphics.FromImage(TempBMP);

            for (int X = 0; X < Code.Matrix.Width; X++)
            {
                for (int Y = 0; Y < Code.Matrix.Height; Y++)
                {
                    if (Code.Matrix.InternalArray[X, Y])
                    {
                        g.FillRectangle(new SolidBrush(DarkColor), X * ifd.ToFloat(), Y * ifd.ToFloat(), ifd.ToFloat(), ifd.ToFloat());
                    }
                    else
                    {
                        g.FillRectangle(new SolidBrush(LightColor), X * ifd.ToFloat(), Y * ifd.ToFloat(), ifd.ToFloat(), ifd.ToFloat());
                    }
                }
            }

            return(TempBMP);
        }
        /// <summary>
        /// 生成QRCode
        /// </summary>
        /// <param name="code"></param>
        /// <param name="level">容错级别 0-3 Gma.QrCodeNet.Encoding.ErrorCorrectionLevel</param>
        /// <param name="Multiple">倍数</param>
        /// <returns></returns>
        public static Bitmap CreateQRCodeBitmap(string code = "", int level = 2, int Multiple = 1)
        {
            Bitmap bmpRtn = null;

            if (string.IsNullOrWhiteSpace(code))
            {
                return(null);
            }
            try
            {
                // ErrorCorrectionLevel:容错率:容错率越高, 有效像素点就越多,图片也就越大
                var qrEncoder = new Gma.QrCodeNet.Encoding.QrEncoder((Gma.QrCodeNet.Encoding.ErrorCorrectionLevel)level);
                var qrCode    = new Gma.QrCodeNet.Encoding.QrCode();
                qrEncoder.TryEncode(code, out qrCode);

                MemoryStream ms       = new MemoryStream();
                var          imgWidth = qrCode.Matrix.Width; //图片宽度 以生成的码量为宽度
                Bitmap       bmp      = new Bitmap(imgWidth, imgWidth);

                Graphics g = Graphics.FromImage(bmp);

                Pen pB = new Pen(Color.Black, 1f); //黑笔
                Pen pW = new Pen(Color.White, 1f); //白笔

                g.Clear(Color.White);              // 背景色白

                for (int j = 0; j < qrCode.Matrix.Width; j++)
                {
                    for (int i = 0; i < qrCode.Matrix.Width; i++)
                    {
                        //char charToPrint = qrCode.Matrix[i, j] ? '█' : ' ';  //此乃生成的QRCode矩阵
                        //根据QRCode矩阵画点
                        if (qrCode.Matrix[i, j])
                        {
                            bmp.SetPixel(i, j, Color.Black);
                            //g.DrawRectangle(pB, i, j, imgWidth, Multiple);
                        }
                        else
                        {
                            bmp.SetPixel(i, j, Color.White);
                            //g.DrawRectangle(pW, i * Multiple, j * Multiple, Multiple, Multiple);
                        }
                    }
                }
                //把生成的QRCode原图放大,Multiple为倍数
                Bitmap bmpNew = new Bitmap(bmp, new Size(imgWidth * Multiple, imgWidth * Multiple));
                using (ms)
                {
                    bmpRtn = bmpNew;
                    bmp.Dispose();
                    bmpNew.Dispose();
                    return(bmpRtn);
                }
            }
            catch
            {
                return(bmpRtn);
            }
        }
예제 #5
0
        /*
         * public static string GerarQRCode(CamadaDados.Faturamento.PDV.TRegistro_NFCe rNFCe)
         * {
         *  if (rNFCe.St_contingencia)
         *  {
         *      System.Collections.Hashtable hs = new System.Collections.Hashtable();
         *      rNFCe.Chave_acesso = NFCe.GerarXML.TGerarXML.MontarChaveAcessoNfe(rNFCe, true).Trim();
         *      hs.Add("@P_CHAVE", rNFCe.Chave_acesso);
         *      hs.Add("@P_EMPRESA", rNFCe.Cd_empresa);
         *      hs.Add("@P_ID_NFCE", rNFCe.Id_nfcestr);
         *      new CamadaDados.TDataQuery().executarSql("update tb_pdv_nfce set chave_acesso = @P_CHAVE, dt_alt = getdate() " +
         *                                                "where cd_empresa = @P_EMPRESA and id_nfce = @P_ID_NFCE", hs);
         *  }
         *  string dest = string.Empty;
         *  if(rNFCe.rCliente == null ? false : !string.IsNullOrEmpty((rNFCe.rCliente.Nr_cgc + rNFCe.rCliente.Nr_cpf).SoNumero()))
         *  {
         *      if (rNFCe.rCliente.Tp_pessoa.Trim().ToUpper().Equals("F") && !string.IsNullOrEmpty(rNFCe.rCliente.Nr_cpf.SoNumero()))
         *          dest = rNFCe.rCliente.Nr_cpf.SoNumero();
         *      else if (rNFCe.rCliente.Tp_pessoa.Trim().ToUpper().Equals("J") && !string.IsNullOrEmpty(rNFCe.rCliente.Nr_cgc.SoNumero()))
         *          dest = rNFCe.rCliente.Nr_cgc.SoNumero();
         *  }
         *  else if (!string.IsNullOrEmpty(rNFCe.Nr_cgc_cpf.SoNumero()) &&
         *           (rNFCe.Nr_cgc_cpf.SoNumero().Length.Equals(14) ||
         *            rNFCe.Nr_cgc_cpf.SoNumero().Length.Equals(11)))
         *      dest = rNFCe.Nr_cgc_cpf.SoNumero();
         *  string str = "chNFe=" + GerarXML.TGerarXML.MontarChaveAcessoNfe(rNFCe, true).Trim() + "&" +
         *                "nVersao=100&" +
         *                "tpAmb=" + rNFCe.rCfgNFCe.Tp_ambiente_nfce.Trim() + "&" +
         *                (string.IsNullOrEmpty(dest) ? string.Empty : "cDest=" + dest.Trim() + "&") +
         *                "dhEmi=" + rNFCe.Dt_emissao.Value.ToString("yyyy-MM-ddTHH:mm:sszzz").ConvertHexaDecimal().ToLower() + "&" +
         *                "vNF=" + Convert.ToDecimal(string.Format("{0:N2}", rNFCe.Vl_cupom)).ToString(new System.Globalization.CultureInfo("en-US", true)) + "&" +
         *                "vICMS=" + Convert.ToDecimal(string.Format("{0:N2}", rNFCe.lItem.Sum(v => v.Vl_icms))).ToString(new System.Globalization.CultureInfo("en-US", true)) + "&" +
         *                "digVal=" + rNFCe.Digval.ConvertHexaDecimal().ToLower() + "&" +
         *                "cIdToken=" + rNFCe.rCfgNFCe.Id_tokencsc.Trim() + rNFCe.rCfgNFCe.Nr_csc.Trim();
         *  string strQRCode = rNFCe.rCfgNFCe.Url_nfce.Trim() +
         *                     "chNFe=" + GerarXML.TGerarXML.MontarChaveAcessoNfe(rNFCe, true).Trim() + "&" +
         *                     "nVersao=100&" +
         *                     "tpAmb=" + rNFCe.rCfgNFCe.Tp_ambiente_nfce.Trim() + "&" +
         *                     (string.IsNullOrEmpty(dest) ? string.Empty : "cDest=" + dest.Trim() + "&") +
         *                     "dhEmi=" + rNFCe.Dt_emissao.Value.ToString("yyyy-MM-ddTHH:mm:sszzz").ConvertHexaDecimal().ToLower() + "&" +
         *                     "vNF=" + Convert.ToDecimal(string.Format("{0:N2}", rNFCe.Vl_cupom)).ToString(new System.Globalization.CultureInfo("en-US", true)) + "&" +
         *                     "vICMS=" + Convert.ToDecimal(string.Format("{0:N2}", rNFCe.lItem.Sum(v => v.Vl_icms))).ToString(new System.Globalization.CultureInfo("en-US", true)) + "&" +
         *                     "digVal=" + rNFCe.Digval.ConvertHexaDecimal().ToLower() + "&" +
         *                     "cIdToken=" + rNFCe.rCfgNFCe.Id_tokencsc.Trim() + "&" +
         *                     "cHashQRCode=" + Utils.Estruturas.SHA1(str).ToLower().Trim();
         *  //Gerar Imagem QR Code
         *  Gma.QrCodeNet.Encoding.QrCode code = new Gma.QrCodeNet.Encoding.QrCode();
         *  Gma.QrCodeNet.Encoding.QrEncoder encoder = new Gma.QrCodeNet.Encoding.QrEncoder(Gma.QrCodeNet.Encoding.ErrorCorrectionLevel.M);
         *  code = encoder.Encode(strQRCode);
         *  Gma.QrCodeNet.Encoding.Windows.Render.GraphicsRenderer gRender =
         *      new Gma.QrCodeNet.Encoding.Windows.Render.GraphicsRenderer(new Gma.QrCodeNet.Encoding.Windows.Render.FixedModuleSize(4, Gma.QrCodeNet.Encoding.Windows.Render.QuietZoneModules.Zero),
         *                                                                 System.Drawing.Brushes.Black, System.Drawing.Brushes.White);
         *  System.IO.MemoryStream ms = new System.IO.MemoryStream();
         *  gRender.WriteToStream(code.Matrix, System.Drawing.Imaging.ImageFormat.Jpeg, ms);
         *  rNFCe.QR_Code = System.Drawing.Image.FromStream(ms);
         *  return strQRCode;
         * }
         */
        public static string GerarQRCode2(CamadaDados.Faturamento.PDV.TRegistro_NFCe rNFCe)
        {
            string strQRCode = string.Empty;

            if (rNFCe.St_contingencia)
            {
                System.Collections.Hashtable hs = new System.Collections.Hashtable();
                rNFCe.Chave_acesso = NFCe.GerarXML.TGerarXML.MontarChaveAcessoNfe(rNFCe, true).Trim();
                hs.Add("@P_CHAVE", rNFCe.Chave_acesso);
                hs.Add("@P_EMPRESA", rNFCe.Cd_empresa);
                hs.Add("@P_ID_NFCE", rNFCe.Id_nfcestr);
                new CamadaDados.TDataQuery().executarSql("update tb_pdv_nfce set chave_acesso = @P_CHAVE, dt_alt = getdate() " +
                                                         "where cd_empresa = @P_EMPRESA and id_nfce = @P_ID_NFCE", hs);
                string str = GerarXML.TGerarXML.MontarChaveAcessoNfe(rNFCe, true).Trim() + "|" +
                             "2|" +
                             rNFCe.rCfgNFCe.Tp_ambiente_nfce.Trim() + "|" +
                             rNFCe.Dt_emissao.Value.ToString("dd/MM/yyyy").Substring(0, 2) + "|" +
                             Convert.ToDecimal(string.Format("{0:N2}", rNFCe.Vl_cupom)).ToString(new System.Globalization.CultureInfo("en-US", true)) + "|" +
                             rNFCe.Digval.ConvertHexaDecimal() + "|" +
                             int.Parse(rNFCe.rCfgNFCe.Id_tokencsc) + rNFCe.rCfgNFCe.Nr_csc.Trim();
                strQRCode = rNFCe.rCfgNFCe.Url_nfce.Trim() + "?p=" +
                            GerarXML.TGerarXML.MontarChaveAcessoNfe(rNFCe, true).Trim() + "|" +
                            "2|" +
                            rNFCe.rCfgNFCe.Tp_ambiente_nfce.Trim() + "|" +
                            rNFCe.Dt_emissao.Value.ToString("dd/MM/yyyy").Substring(0, 2) + "|" +
                            Convert.ToDecimal(string.Format("{0:N2}", rNFCe.Vl_cupom)).ToString(new System.Globalization.CultureInfo("en-US", true)) + "|" +
                            rNFCe.Digval.ConvertHexaDecimal() + "|" +
                            int.Parse(rNFCe.rCfgNFCe.Id_tokencsc) + "|" +
                            Estruturas.SHA1(str).Trim();
            }
            else
            {
                string str = GerarXML.TGerarXML.MontarChaveAcessoNfe(rNFCe, true).Trim() + "|" +
                             "2|" +
                             rNFCe.rCfgNFCe.Tp_ambiente_nfce.Trim() + "|" +
                             int.Parse(rNFCe.rCfgNFCe.Id_tokencsc) + rNFCe.rCfgNFCe.Nr_csc.Trim();
                strQRCode = rNFCe.rCfgNFCe.Url_nfce.Trim() + "?p=" +
                            GerarXML.TGerarXML.MontarChaveAcessoNfe(rNFCe, true).Trim() + "|" +
                            "2|" + //Versão do QRCode
                            rNFCe.rCfgNFCe.Tp_ambiente_nfce.Trim() + "|" +
                            int.Parse(rNFCe.rCfgNFCe.Id_tokencsc) + "|" +
                            Estruturas.SHA1(str).Trim();
            }
            //Gerar Imagem QR Code
            Gma.QrCodeNet.Encoding.QrCode    code    = new Gma.QrCodeNet.Encoding.QrCode();
            Gma.QrCodeNet.Encoding.QrEncoder encoder = new Gma.QrCodeNet.Encoding.QrEncoder(Gma.QrCodeNet.Encoding.ErrorCorrectionLevel.M);
            code = encoder.Encode(strQRCode);
            Gma.QrCodeNet.Encoding.Windows.Render.GraphicsRenderer gRender =
                new Gma.QrCodeNet.Encoding.Windows.Render.GraphicsRenderer(new Gma.QrCodeNet.Encoding.Windows.Render.FixedModuleSize(4, Gma.QrCodeNet.Encoding.Windows.Render.QuietZoneModules.Zero),
                                                                           System.Drawing.Brushes.Black, System.Drawing.Brushes.White);
            System.IO.MemoryStream ms = new System.IO.MemoryStream();
            gRender.WriteToStream(code.Matrix, System.Drawing.Imaging.ImageFormat.Jpeg, ms);
            rNFCe.QR_Code = System.Drawing.Image.FromStream(ms);
            return(strQRCode);
        }
예제 #6
0
        public static Stream GetQrcodeStream(string qrCode)
        {
            Gma.QrCodeNet.Encoding.QrEncoder Encoder = new Gma.QrCodeNet.Encoding.QrEncoder(Gma.QrCodeNet.Encoding.ErrorCorrectionLevel.H);
            Gma.QrCodeNet.Encoding.QrCode    Code    = Encoder.Encode(qrCode);
            GraphicsRenderer renderer = new GraphicsRenderer(new FixedModuleSize(10, QuietZoneModules.Two), System.Drawing.Brushes.Black, System.Drawing.Brushes.White);
            var stream = new MemoryStream();

            renderer.WriteToStream(Code.Matrix, System.Drawing.Imaging.ImageFormat.Png, stream);
            stream.Position = 0;
            return(stream);
        }
예제 #7
0
        public static void GenerateQrCode(string qrCode)
        {
            Gma.QrCodeNet.Encoding.QrEncoder Encoder = new Gma.QrCodeNet.Encoding.QrEncoder(Gma.QrCodeNet.Encoding.ErrorCorrectionLevel.H);
            Gma.QrCodeNet.Encoding.QrCode    Code    = Encoder.Encode(qrCode);

            GraphicsRenderer renderer = new GraphicsRenderer(new FixedModuleSize(10, QuietZoneModules.Two), System.Drawing.Brushes.Black, System.Drawing.Brushes.White);

            using (FileStream stream = new FileStream($"{Path.Combine(ApplicationPath(), "Images", "QrCodes")}\\{qrCode}.png", FileMode.Create))
            {
                renderer.WriteToStream(Code.Matrix, System.Drawing.Imaging.ImageFormat.Png, stream);
            }
        }
        public recieve_bitcoincash()
        {
            InitializeComponent();

            IBitcoinService bitcoinService = new BitcoinService();
            string          a = bitcoinService.GetNewAddress();

            address_textBox.Text = a;


            Gma.QrCodeNet.Encoding.QrEncoder Encoder = new Gma.QrCodeNet.Encoding.QrEncoder(Gma.QrCodeNet.Encoding.ErrorCorrectionLevel.H);
            Gma.QrCodeNet.Encoding.QrCode    Code    = Encoder.Encode(a);
            Bitmap TempBMP = new Bitmap(Code.Matrix.Width, Code.Matrix.Height);

            for (int X = 0; X <= (Code.Matrix.Width) - 1; X++)
            {
                for (int Y = 0; Y <= (Code.Matrix.Height) - 1; Y++)
                {
                    if (Code.Matrix.InternalArray[X, Y])
                    {
                        TempBMP.SetPixel(X, Y, System.Drawing.Color.Black);
                    }
                    else
                    {
                        TempBMP.SetPixel(X, Y, System.Drawing.Color.White);
                    }
                }
            }

            //TempBMP.Size abc = new System.Drawing.Size(120,120);

            //Console.Write(TempBMP);
            //ictureBox.Image.Size =new  Size(600, 700);
            //empBMP.Size = new Size(TempBMP.Height,TempBMP.Width);
            pictureBox.Size = new Size(100, 75);
            // pictureBox.Image = TempBMP;

            var imageTemp = new Bitmap(TempBMP);

            var image = new Bitmap(imageTemp, new System.Drawing.Size(new System.Drawing.Point(100, 75)));

            //image.Save("file.bmp", ImageFormat.Bmp);

            pictureBox.Image = image;
        }
예제 #9
0
        // QR Code를 bitmap으로 생성하여 리턴한다
        public static Bitmap GenerateQRCode(string text, System.Drawing.Color DarkColor, System.Drawing.Color LightColor)
        {
            Gma.QrCodeNet.Encoding.QrEncoder Encoder = new Gma.QrCodeNet.Encoding.QrEncoder(Gma.QrCodeNet.Encoding.ErrorCorrectionLevel.L);
            Gma.QrCodeNet.Encoding.QrCode    Code    = Encoder.Encode(text);
            Bitmap TempBMP = new Bitmap(Code.Matrix.Width, Code.Matrix.Height);

            for (int X = 0; X <= Code.Matrix.Width - 1; X++)
            {
                for (int Y = 0; Y <= Code.Matrix.Height - 1; Y++)
                {
                    if (Code.Matrix.InternalArray[X, Y])
                    {
                        TempBMP.SetPixel(X, Y, DarkColor);
                    }
                    else
                    {
                        TempBMP.SetPixel(X, Y, LightColor);
                    }
                }
            }
            return(TempBMP);
        }
예제 #10
0
파일: UnitTests.cs 프로젝트: foamliu/QRBa
 public void UT_EncodeTest()
 {
     string url = "http://teal.azurewebsites.net/Event/1/1";
     var encoder = new Gma.QrCodeNet.Encoding.QrEncoder(Gma.QrCodeNet.Encoding.ErrorCorrectionLevel.M);
     var code = encoder.Encode(url);
     var bmp = new Bitmap(code.Matrix.Width, code.Matrix.Height);
     for (int x = 0; x < code.Matrix.Width; x++)
     {
         for (int y = 0; y < code.Matrix.Height; y++)
         {
             if (code.Matrix.InternalArray[x, y])
             {
                 bmp.SetPixel(x, y, Color.Black);
             }
             else
             {
                 bmp.SetPixel(x, y, Color.White);
             }
         }
     }
     bmp.Save("bmp.png", ImageFormat.Png);
 }
예제 #11
0
        public void UT_EncodeTest()
        {
            string url     = "http://teal.azurewebsites.net/Event/1/1";
            var    encoder = new Gma.QrCodeNet.Encoding.QrEncoder(Gma.QrCodeNet.Encoding.ErrorCorrectionLevel.M);
            var    code    = encoder.Encode(url);
            var    bmp     = new Bitmap(code.Matrix.Width, code.Matrix.Height);

            for (int x = 0; x < code.Matrix.Width; x++)
            {
                for (int y = 0; y < code.Matrix.Height; y++)
                {
                    if (code.Matrix.InternalArray[x, y])
                    {
                        bmp.SetPixel(x, y, Color.Black);
                    }
                    else
                    {
                        bmp.SetPixel(x, y, Color.White);
                    }
                }
            }
            bmp.Save("bmp.png", ImageFormat.Png);
        }
예제 #12
0
 static QRDialog()
 {
     encoder = new Gma.QrCodeNet.Encoding.QrEncoder();
 }