Пример #1
5
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                var content = WebUtility.GetRequestStr("s", "");
                if (!content.StartsWith("http"))
                    content = string.Format("http://{0}:{1}{2}", Request.Url.Host, Request.Url.Port, HttpUtility.UrlDecode(content));

                QrEncoder qrEncoder = new QrEncoder(ErrorCorrectionLevel.H);
                QrCode qrCode = new QrCode();
                if (qrEncoder.TryEncode(content, out qrCode))
                {
                    var fCodeSize = new FixedCodeSize(500, QuietZoneModules.Two);
                    fCodeSize.QuietZoneModules = QuietZoneModules.Four;
                    GraphicsRenderer renderer = new GraphicsRenderer(fCodeSize, Brushes.Black, Brushes.White);
                    MemoryStream ms = new MemoryStream();
                    renderer.WriteToStream(qrCode.Matrix, ImageFormat.Png, ms);

                    Response.Cache.SetNoStore();
                    Response.ClearContent();
                    Response.ContentType = "image/Png";
                    Response.BinaryWrite(ms.ToArray());
                }
            }
        }
Пример #2
2
        private void button1_Click(object sender, EventArgs e)
        {
            /// generar un codigo QR para los elementos de la caja 

            // crear un encoder, codificador
            QrEncoder Codificador = new QrEncoder( ErrorCorrectionLevel.H );

            // crear un codigo QR
            QrCode Codigo = new QrCode();

            // generar generar  un codigo apartir de datos, y pasar el codigo por referencia
            Codificador.TryEncode(textBox1.Text, out Codigo);

            // generar un graficador 
            GraphicsRenderer Renderisado = new GraphicsRenderer(new FixedCodeSize(200, QuietZoneModules.Zero), Brushes.Black, Brushes.White);

            // generar un flujo de datos 
            MemoryStream ms = new MemoryStream();

            // escribir datos en el renderizado
            Renderisado.WriteToStream(Codigo.Matrix, ImageFormat.Png, ms);

            // generar controles para ponerlos en el form
            var ImagenQR = new Bitmap(ms);
            var ImgenSalida = new Bitmap(ImagenQR, new Size(panel1.Width, panel1.Height));

            // asignar la imagen al panel 
            panel1.BackgroundImage = ImgenSalida;
            


                 
        }
Пример #3
0
        protected void Page_Load(object sender, EventArgs e)
        {
            var api = Request["Api"];
            if (String.IsNullOrEmpty(api))
                return;

            var manager = new OAuthHelper(ConfigurationManager.AppSettings["wxappid"]);
            var url = manager.BuildOAuthUrl("http://wx.taogame.com/OAuthUserInfoDemo.aspx",
                api == "snsapi_base" ? OAuthScope.Base : OAuthScope.UserInfo,
                api);


            QrEncoder encoder = new QrEncoder(ErrorCorrectionLevel.M);
            QrCode qrCode;
            encoder.TryEncode(url, out qrCode);

        
            GraphicsRenderer gRenderer = new GraphicsRenderer(
                new FixedModuleSize(2, QuietZoneModules.Two),
                Brushes.Black, Brushes.White);

            MemoryStream ms = new MemoryStream();
            gRenderer.WriteToStream(qrCode.Matrix, ImageFormat.Jpeg, ms);

            Response.BinaryWrite(ms.ToArray());
        }
        public HttpResponseMessage QrCode(string id)
        {
            if (id == null) throw new ArgumentNullException("id");

            if (!ValidUserIdRegex.IsMatch(id))
            {
                return new HttpResponseMessage(HttpStatusCode.BadRequest);
            }

            const int MODULE_SIZE = 12;
            GraphicsRenderer renderer =
                new GraphicsRenderer(new FixedModuleSize(MODULE_SIZE, QuietZoneModules.Zero));

            QrEncoder qrEncoder = new QrEncoder(ErrorCorrectionLevel.L);

            string content = id;
            QrCode qrCode = qrEncoder.Encode(content);

            MemoryStream rendered = new MemoryStream();
            renderer.WriteToStream(qrCode.Matrix, ImageFormat.Gif, rendered);

            HttpResponseMessage responseMessage = new HttpResponseMessage(HttpStatusCode.OK);
            responseMessage.Content = new ByteArrayContent(rendered.ToArray());
            responseMessage.Content.Headers.ContentType = new MediaTypeHeaderValue("image/gif");
            return responseMessage;
        }
Пример #5
0
    public Image DrawQRCode(int qrImageSize, ImageFormat imgFormat)
    {
      QrEncoder qrEncoder = new QrEncoder(ErrorCorrectionLevel.M);
      QrCode qrCode = qrEncoder.Encode(this.BarCodeValue);

      GraphicsRenderer renderer = new GraphicsRenderer(new FixedCodeSize(qrImageSize, QuietZoneModules.Two), Brushes.Black, Brushes.White);
      MemoryStream stream = new MemoryStream();
      renderer.WriteToStream(qrCode.Matrix, imgFormat, stream);
      return Image.FromStream(stream);
    }
Пример #6
0
        public void Render(byte[] data, string fileName)
        {
            encoder.ErrorCorrectionLevel = Settings.ErrorCorrectionLevel;

              var qrCode = encoder.Encode(data);
              var renderer = new GraphicsRenderer(new FixedCodeSize(Settings.Size, QuietZoneModules.Zero), Settings.Background, Settings.Foreground);

              using (var file = new FileStream(fileName, FileMode.Create, FileAccess.Write, FileShare.None))
            renderer.WriteToStream(qrCode.Matrix, ImageFormat.Png, file);
        }
Пример #7
0
        public static void RunSample3()
        {
            QrEncoder qrEncoder = new QrEncoder(ErrorCorrectionLevel.H);
            QrCode qrCode = qrEncoder.Encode("Hello World!");

            GraphicsRenderer renderer = new GraphicsRenderer(new FixedModuleSize(5, QuietZoneModules.Two), Brushes.Black, Brushes.White);
            using (FileStream stream = new FileStream(@"c:\temp\HelloWorld.png", FileMode.Create))
            {
                renderer.WriteToStream(qrCode.Matrix, ImageFormat.Png, stream);
            }
        }
Пример #8
0
        public string generarQR(string codigo)
        {
            var qrEncoder = new QrEncoder(ErrorCorrectionLevel.H);
            var qrCode = qrEncoder.Encode(codigo);

            var renderer = new GraphicsRenderer(new FixedModuleSize(5, QuietZoneModules.Two), Brushes.Black, Brushes.White);
            string path = AppDomain.CurrentDomain.BaseDirectory + "tmp\\"+ codigo +"QR.png";
            using (var stream = new FileStream(path, FileMode.Create))
                renderer.WriteToStream(qrCode.Matrix, ImageFormat.Png, stream);
            return codigo+"QR.png";
        }
     public ActionResult QR(string id)
     {
         QrEncoder encoder = new QrEncoder(ErrorCorrectionLevel.L);
         QrCode qrCode;
         encoder.TryEncode("http://mobiledev.scrzjh.com:9999/l?id=" + id.ToString(), out qrCode);
         MemoryStream ms = new MemoryStream();
         var render = new GraphicsRenderer(new FixedModuleSize(8, QuietZoneModules.Two));
         render.WriteToStream(qrCode.Matrix, ImageFormat.Png, ms);
 
         return File(ms.GetBuffer(), "image/png");
     }
Пример #10
0
        public Image GenerateQRCode(QRCodeParameters param)
        {
            if (param == null) throw new ArgumentNullException();

            QrEncoder encoder = new QrEncoder(ErrorCorrectionLevel.L);
            QrCode qrCode = encoder.Encode(param.Text);

            GraphicsRenderer gRenderer = new GraphicsRenderer(new FixedModuleSize(param.Size, QuietZoneModules.Two), Brushes.Black, Brushes.White);
            MemoryStream ms = new MemoryStream();
            gRenderer.WriteToStream(qrCode.Matrix, param.ImageFormat, ms);
            return Image.FromStream(ms);
        }
Пример #11
0
 private void tsmiCopy_Click(object sender, EventArgs e)
 {
     GraphicsRenderer gRender = new GraphicsRenderer(new FixedModuleSize(30, QuietZoneModules.Four));
     BitMatrix matrix = qrMain.GetQrMatrix();
     using (MemoryStream stream = new MemoryStream())
     {
         gRender.WriteToStream(matrix, ImageFormat.Png, stream, new Point(600, 600));
         using (Image img = Image.FromStream(stream))
         {
             ClipboardHelpers.CopyImage(img);
         }
     }
 }
Пример #12
0
 private bool GenCode(string content, out MemoryStream ms)
 {
     ms = new MemoryStream();
     ErrorCorrectionLevel ecl = ErrorCorrectionLevel.M;//误差水平
     QuietZoneModules qzm = QuietZoneModules.Two;
     int moduleSize = 12;
     QrEncoder encoder = new QrEncoder(ecl);
     QrCode qr;
     if (encoder.TryEncode(content, out qr))//对内容进行编码,并保存生成的矩阵
     {
         var render = new GraphicsRenderer(new FixedModuleSize(moduleSize, qzm));
         render.WriteToStream(qr.Matrix, ImageFormat.Jpeg, ms);
         return true;
     }
     return false;
 }
Пример #13
0
        /// <summary>
        /// Generates an img tag with a data uri encoded image of the QR code from the content given.
        /// </summary>
        /// <param name="html"></param>
        /// <param name="content"></param>
        /// <returns></returns>
        public static IHtmlString QRCode(this HtmlHelper html, string content)
        {
            QrEncoder enc = new QrEncoder(ErrorCorrectionLevel.H);
            var code = enc.Encode(content);

            GraphicsRenderer r = new GraphicsRenderer(new FixedModuleSize(5, QuietZoneModules.Two), Brushes.Black, Brushes.White);

            using (MemoryStream ms = new MemoryStream())
            {
                r.WriteToStream(code.Matrix, ImageFormat.Png, ms);

                byte[] image = ms.ToArray();
                //otpauth://totp/MY_APP_LABEL?secret={0}
                return html.Raw(string.Format(@"<img src=""data:image/png;base64,{0}"" alt=""{1}"" />", Convert.ToBase64String(image), content));
            }
        }
        /// <summary>
        /// Qr Code generation
        /// </summary>
        /// <param name="barcodeText"></param>
        /// <returns></returns>
        public ActionResult BarcodeImage(String barcodeText)
        {
            // generating a barcode here. Code is taken from QrCode.Net library
            QrEncoder qrEncoder = new QrEncoder(ErrorCorrectionLevel.H);
            QrCode qrCode = new QrCode();
            qrEncoder.TryEncode(barcodeText, out qrCode);
            GraphicsRenderer renderer = new GraphicsRenderer(new FixedModuleSize(4, QuietZoneModules.Four), Brushes.Black, Brushes.White);
            Stream memoryStream = new MemoryStream();
            renderer.WriteToStream(qrCode.Matrix, ImageFormat.Png, memoryStream);

            // very important to reset memory stream to a starting position, otherwise you would get 0 bytes returned
            memoryStream.Position = 0;
            var resultStream = new FileStreamResult(memoryStream, "image/png");
            resultStream.FileDownloadName = String.Format("{0}.png", barcodeText);
            return resultStream;
        }
Пример #15
0
        public ActionResult BarcodeImage(String barcodeText)
        {
            QrEncoder qrEncoder = new QrEncoder(ErrorCorrectionLevel.H);
            QrCode qrCode = new QrCode();
            qrEncoder.TryEncode(barcodeText, out qrCode);
            GraphicsRenderer renderer = new GraphicsRenderer(new FixedModuleSize(4, QuietZoneModules.Four), Brushes.Black, Brushes.White);

            Stream memoryStream = new MemoryStream();
            renderer.WriteToStream(qrCode.Matrix, ImageFormat.Png, memoryStream);

            memoryStream.Position = 0;

            var resultStream = new FileStreamResult(memoryStream, "image/png");
            resultStream.FileDownloadName = String.Format("{0}.png", barcodeText);

            return resultStream;
        }
        private void btnShowQRCode_Click(object sender, EventArgs e)
        {
            var qrGenerator = new QrEncoder();
            var qrCode = qrGenerator.Encode(_password);

            var gRenderer = new GraphicsRenderer(
                new FixedModuleSize(6, QuietZoneModules.Two));

            using (var ms = new MemoryStream())
            {
                gRenderer.WriteToStream(qrCode.Matrix, ImageFormat.Png, ms);
                using (pbQrCode.Image)
                {
                    pbQrCode.Image = new Bitmap(ms);
                }
            }
        }
Пример #17
0
    protected void Page_Load(object sender, EventArgs e)
    {
        QrEncoder encoder = new QrEncoder(ErrorCorrectionLevel.M);
        QrCode qrCode;
        encoder.TryEncode("Test", out qrCode);

        GraphicsRenderer gRenderer = new GraphicsRenderer(
         new FixedModuleSize(2, QuietZoneModules.Two),
         Brushes.Black, Brushes.White);

        MemoryStream ms = new MemoryStream();
        gRenderer.WriteToStream(qrCode.Matrix, ImageFormat.Png, ms);

        FileStream file = new FileStream("D:\\qr.png", FileMode.OpenOrCreate);
        ms.WriteTo(file);
        file.Flush();
        file.Close();
    }
        public ActionResult BarcodeImage(string secretKey, string userName)
        {
            string barcodeText = string.Format("otpauth://totp/Dentco%3A%20{0}?secret={1}", userName, secretKey);

            QrEncoder qrEncoder = new QrEncoder(ErrorCorrectionLevel.H);
            QrCode qrCode = new QrCode();
            qrEncoder.TryEncode(barcodeText, out qrCode);
            GraphicsRenderer renderer = new GraphicsRenderer(new FixedCodeSize(200, QuietZoneModules.Zero), Brushes.Black, Brushes.White);

            Stream memoryStream = new MemoryStream();
            renderer.WriteToStream(qrCode.Matrix, ImageFormat.Png, memoryStream);

            memoryStream.Position = 0;

            var resultStream = new FileStreamResult(memoryStream, "image/png");
            resultStream.FileDownloadName = string.Format("{0}.png", barcodeText);

            return resultStream;
        }
        public void GenerateQrCodesForIds(string directory, IEnumerable<string> items)
        {
            QrEncoder encoder = new QrEncoder(ErrorCorrectionLevel.M);

            GraphicsRenderer renderer = new GraphicsRenderer(new FixedModuleSize(5, QuietZoneModules.Two), Brushes.Black, Brushes.White);

            int index = 0;
            foreach (var item in items)
            {
                QrCode qrCode = encoder.Encode(item);

                using (FileStream stream = new FileStream(string.Format("{0}\\{1}.png", directory, index), FileMode.Create))
                {
                    renderer.WriteToStream(qrCode.Matrix, ImageFormat.Png, stream);
                }

                index++;
            }
        }
Пример #20
0
 public static bool GetQRCode(string CodeText,System.IO.MemoryStream ms)
 {
     ErrorCorrectionLevel Ecl = ErrorCorrectionLevel.M; //误差校正水平
     string Content = strContent;//待编码内容
     QuietZoneModules QuietZones = QuietZoneModules.Two;  //空白区域
     int ModuleSize = 12;//大小
     var encoder = new QrEncoder(Ecl);
     QrCode qr;
     if (encoder.TryEncode(Content, out qr))//对内容进行编码,并保存生成的矩阵
     {
         var render = new GraphicsRenderer(new FixedModuleSize(ModuleSize, QuietZones));
         render.WriteToStream(qr.Matrix, ImageFormat.Png, ms);
     }
     else
     {
         return false;
     }
     return true;
 }
Пример #21
0
        public static BitmapImage getQrBitmap(string text, ErrorCorrectionLevel errorCorrection = ErrorCorrectionLevel.M)
        {
            QrCode qrCode;
            QrEncoder qrEncoder = new QrEncoder(errorCorrection);
            qrEncoder.TryEncode(text, out qrCode);

            GraphicsRenderer gRenderer = new GraphicsRenderer(
                new FixedModuleSize(2, QuietZoneModules.Two),
                Brushes.Black, Brushes.White);
            BitmapImage bi = new BitmapImage();
            using (MemoryStream memoryStream = new MemoryStream())
            {
                gRenderer.WriteToStream(qrCode.Matrix, ImageFormat.Png, memoryStream);
                bi.BeginInit();
                bi.CacheOption = BitmapCacheOption.OnLoad;
                bi.StreamSource = memoryStream;
                bi.EndInit();
            }
            return bi;
        }
Пример #22
0
        /// <summary>
        /// 使用Gma.QrCodeNet.Encoding 组件
        /// </summary>
        /// <param name="content"></param>
        /// <returns></returns>
        public static byte[] GetQrCode(string content)
        {
            QrEncoder qr = new QrEncoder();

            Gma.QrCodeNet.Encoding.QrCode code; //保存生成的二维码

            QuietZoneModules QuietZones = QuietZoneModules.Zero;  //空白区域 
            int ModuleSize = 10;//大小

            MemoryStream ms = new MemoryStream();

            if (qr.TryEncode(content, out code))//对内容进行编码,并保存生成的矩阵
            {
                var render = new GraphicsRenderer(new FixedModuleSize(ModuleSize, QuietZones));
                render.WriteToStream(code.Matrix, ImageFormat.Png, ms);//将二维码写入流

                return ms.ToArray(); //返回byte[]
            }

            return null;
        }
Пример #23
0
        private void btnSave_Click(object sender, EventArgs e)
        {
            var fileName = "";
            using (var saveDlg = new SaveFileDialog()) {
                saveDlg.Filter = "*|*.png";
                if (saveDlg.ShowDialog() != DialogResult.OK) {
                    return;
                } else {
                    fileName = saveDlg.FileName;
                }
            }

            var qrCode = qrCodeGraphicControl1.GetQrMatrix();
            var sizeCal = new FixedModuleSize(12, qrCodeGraphicControl1.QuietZoneModule);
            var render = new GraphicsRenderer(sizeCal);
            //new FixedCodeSize(width, m_QuietZoneModule)
            var memoryStream = new MemoryStream();
            render.WriteToStream(qrCode, ImageFormat.Png, memoryStream);

            var image = Image.FromStream(memoryStream);

            image.Save(fileName);
        }
Пример #24
0
        /// <summary>
        /// Méthode retournant un objet d'image Bitmap d'un code QR
        /// </summary>
        /// <param name="text">Le texte à encoder</param>
        /// <param name="errorCorrection">Le niveau de correction d'erreur choisi. Détermine le pourcentage de la structure du codeQR est utilisé pour corriger les erreurs.</param>
        /// <returns>Objet BitmapImage du codeQR</returns>
        public static BitmapImage getQrBitmap(string text, ErrorCorrectionLevel errorCorrection = ErrorCorrectionLevel.M)
        {
            QrCode qrCode;
            QrEncoder qrEncoder = new QrEncoder(errorCorrection);
            //Peut techniquement échouer et le cas n'est pas géré.
            qrEncoder.TryEncode(text, out qrCode);

            //Détermine l'épaisseur des traits, la couleur de fond et la couleur du code QR.
            GraphicsRenderer gRenderer = new GraphicsRenderer(
                new FixedModuleSize(2, QuietZoneModules.Two),
                Brushes.Black, Brushes.White);
            BitmapImage bi = new BitmapImage();
            using (MemoryStream memoryStream = new MemoryStream())
            {
                //Utilise les bytes du codeQR encodé qui se trouve dans l'objet qrCode dans un MemoryStream pour faire une BitmapImage.
                gRenderer.WriteToStream(qrCode.Matrix, ImageFormat.Png, memoryStream);
                bi.BeginInit();
                bi.CacheOption = BitmapCacheOption.OnLoad;
                bi.StreamSource = memoryStream;
                bi.EndInit();
            }
            return bi;
        }
Пример #25
0
        internal static ActionResult QRCodeAction(string id)
        {
            try {
                var encoder = new QrEncoder ();
                var ms = new MemoryStream (4 * 1024);
                var render = new GraphicsRenderer (new FixedCodeSize (300, QuietZoneModules.Zero));
                QrCode code;

                if (!encoder.TryEncode (id, out code))
                    return null;

                render.WriteToStream (code.Matrix, ImageFormat.Png, ms, new Point (300, 300));

                ms.Seek (0, SeekOrigin.Begin);
                var result = new FileStreamResult (ms, "image/png");
                result.FileDownloadName = string.Format ("{0}.png", id);

                return result;
            } catch (Exception ex) {
                System.Diagnostics.Debug.WriteLine (ex);
                return null;
            }
        }
Пример #26
0
        private void Encode_text()
        {
            try
            {
            var qrEncoder = new QrEncoder(ErrorCorrectionLevel.M);
            var qrCode = qrEncoder.Encode(en_txt.Text);
            MemoryStream ms = new MemoryStream();

            var renderer = new GraphicsRenderer(new FixedCodeSize(200, QuietZoneModules.Zero), Brushes.Black, Brushes.White);
            renderer.WriteToStream(qrCode.Matrix, ImageFormat.Jpeg,ms);

              en_bmp = new Bitmap(ms);
            //bm.Save(Application.StartupPath + "//ss.jpg", ImageFormat.Jpeg);
            Qr_img.Image = en_bmp;
            getSliderBack_flag = true;
            }
            catch (Exception)
            {

            }
        }
Пример #27
0
        //[OutputCache(Duration = 60 * 60 * 24, Location = System.Web.UI.OutputCacheLocation.Downstream)]
        public async Task<ActionResult> QR(string slug, int width=8)
        {
            QrEncoder encoder = new QrEncoder(ErrorCorrectionLevel.L);
            
            using (var db = new SportDataContext())
            {
                var ev = await EventService.GetEvent(slug, db);
                if (ev == null)
                    return HttpNotFound();

                var url = "http://afspb.org.ru/timeline/";
                if(ev.Season!=2014) 
                    url += ev.Season + "/";
                url += ev.Slug + "/Guide";
                var qrCode = encoder.Encode(url);

                GraphicsRenderer dRenderer = new GraphicsRenderer(
                        new FixedModuleSize(width, QuietZoneModules.Two),
                        Brushes.Black, Brushes.White);

                using(var ms = new MemoryStream())
                {
                    dRenderer.WriteToStream(qrCode.Matrix, ImageFormat.Png, ms);
                    return File(ms.ToArray(), "image/png");
                }
            }

            
        }
Пример #28
0
        private void buttonSave_Click(object sender, EventArgs e)
        {
            SaveFileDialog saveFileDialog = new SaveFileDialog();
            saveFileDialog.Filter = @"PNG (*.png)|*.png|Bitmap (*.bmp)|*.bmp|Encapsuled PostScript (*.eps)|*.eps|SVG (*.svg)|*.svg";
            saveFileDialog.FileName = Path.GetFileName(GetFileNameProposal());
            saveFileDialog.DefaultExt = "png";

            if (saveFileDialog.ShowDialog() != DialogResult.OK)
            {
                return;
            }

            if (saveFileDialog.FileName.EndsWith("eps"))
            {
                BitMatrix matrix = qrCodeGraphicControl1.GetQrMatrix();

                // Initialize the EPS renderer
                var renderer = new EncapsulatedPostScriptRenderer(
                    new FixedModuleSize(6, QuietZoneModules.Two), // Modules size is 6/72th inch (72 points = 1 inch)
                    new FormColor(Color.Black), new FormColor(Color.White));

                using (var file = File.Open(saveFileDialog.FileName, FileMode.CreateNew))
                {
                    renderer.WriteToStream(matrix, file);
                }
            }
            else if (saveFileDialog.FileName.EndsWith("svg"))
            {
                BitMatrix matrix = qrCodeGraphicControl1.GetQrMatrix();

                // Initialize the EPS renderer
                var renderer = new SVGRenderer(
                    new FixedModuleSize(6, QuietZoneModules.Two), // Modules size is 6/72th inch (72 points = 1 inch)
                    new FormColor(Color.FromArgb(150, 200, 200, 210)), new FormColor(Color.FromArgb(200, 255, 155, 0)));

                using (var file = File.OpenWrite(saveFileDialog.FileName))
                {
                    renderer.WriteToStream(matrix, file, false);
                }
            }
            else
            {

                //DrawingBrushRenderer dRender = new DrawingBrushRenderer(new FixedModuleSize(5, QuietZoneModules.Four));
                //BitMatrix matrix = qrCodeGraphicControl1.GetQrMatrix();
                //using (FileStream stream = new FileStream(saveFileDialog.FileName, FileMode.Create))
                //{
                //    dRender.WriteToStream(matrix, ImageFormatEnum.PNG, stream);
                //}

                //WriteableBitmapRenderer wRender = new WriteableBitmapRenderer(new FixedModuleSize(15, QuietZoneModules.Four));
                //BitMatrix matrix = qrCodeGraphicControl1.GetQrMatrix();
                //using (FileStream stream = new FileStream(saveFileDialog.FileName, FileMode.Create))
                //{
                //    wRender.WriteToStream(matrix, ImageFormatEnum.PNG, stream);
                //}

                GraphicsRenderer gRender = new GraphicsRenderer(new FixedModuleSize(30, QuietZoneModules.Four));
                BitMatrix matrix = qrCodeGraphicControl1.GetQrMatrix();
                using (FileStream stream = new FileStream(saveFileDialog.FileName, FileMode.Create))
                {
                    gRender.WriteToStream(matrix, ImageFormat.Png, stream, new Point(600, 600));
                }
            }
        }
Пример #29
0
        private void button1_Click(object sender, EventArgs e)
        {
            SaveFileDialog saveFileDialog = new SaveFileDialog();
            saveFileDialog.Filter = @"PNG (*.png)|*.png|Bitmap (*.bmp)|*.bmp|Encapsuled PostScript (*.eps)|*.eps|SVG (*.svg)|*.svg";
            saveFileDialog.FileName = Path.GetFileName(GetFileNameProposal());
            saveFileDialog.DefaultExt = "png";

            if (saveFileDialog.ShowDialog() != DialogResult.OK)
            {
                return;
            }

            QrEncoder encoder = new QrEncoder();
            QrCode qrCode;
            byte[] byteArray = new byte[] { 34, 54, 90, 200 };
            if (!encoder.TryEncode(byteArray, out qrCode))
                return;
            if (saveFileDialog.FileName.EndsWith("eps"))
            {
                BitMatrix matrix = qrCode.Matrix;

                // Initialize the EPS renderer
                var renderer = new EncapsulatedPostScriptRenderer(
                    new FixedModuleSize(6, QuietZoneModules.Two), // Modules size is 6/72th inch (72 points = 1 inch)
                    new FormColor(Color.Black), new FormColor(Color.White));

                using (var file = File.Open(saveFileDialog.FileName, FileMode.CreateNew))
                {
                    renderer.WriteToStream(matrix, file);
                }
            }
            else if (saveFileDialog.FileName.EndsWith("svg"))
            {
                BitMatrix matrix = qrCode.Matrix;

                // Initialize the EPS renderer
                var renderer = new SVGRenderer(
                    new FixedModuleSize(6, QuietZoneModules.Two), // Modules size is 6/72th inch (72 points = 1 inch)
                    new FormColor(Color.FromArgb(150, 200, 200, 210)), new FormColor(Color.FromArgb(200, 255, 155, 0)));

                using (var file = File.OpenWrite(saveFileDialog.FileName))
                {
                    renderer.WriteToStream(matrix, file, false);
                }
            }
            else
            {
                GraphicsRenderer gRender = new GraphicsRenderer(new FixedModuleSize(30, QuietZoneModules.Four));
                BitMatrix matrix = qrCode.Matrix;
                using (FileStream stream = new FileStream(saveFileDialog.FileName, FileMode.Create))
                {
                    gRender.WriteToStream(matrix, ImageFormat.Png, stream, new Point(600, 600));
                }
            }
        }
Пример #30
-1
        /// <summary>
        /// 生成二维码
        /// </summary>
        /// <param name="code">字符串</param>
        /// <param name="eSize">二维码尺寸</param>
        /// <param name="img">小图</param>
        /// <param name="iSize">小图尺寸</param>
        /// <param name="bColor">二维码背景颜色</param>
        /// <param name="fColor">二维码颜色</param>
        /// <returns></returns>
        public static Image GetQrcode(string code, int eSize, Image img, int iSize, Brush bColor, Brush fColor)
        {
            var qrcoder = new QrEncoder(ErrorCorrectionLevel.H);
            var qrCode = qrcoder.Encode(code);

            var render = new GraphicsRenderer(new FixedModuleSize(5, QuietZoneModules.Four), fColor, bColor);

            using (Stream stream = new MemoryStream())
            {
                eSize = eSize > 0 ? eSize : 200;

                render.WriteToStream(qrCode.Matrix, ImageFormat.Png, stream, new Point(10, 10));

                var be = new Bitmap(stream);
                be = ResizeImage(be, eSize, eSize);
                Graphics ge = Graphics.FromImage(be);

                if (img != null)
                {
                    iSize = iSize > 0 ? iSize : 50;
                    img = ResizeImage(img, iSize, iSize);

                    //小图白色边框
                    var bi = new Bitmap(iSize + 10, iSize + 10);
                    Graphics gi = Graphics.FromImage(bi);
                    gi.Clear(Color.White);
                    gi.DrawImage(img, 5, 5, iSize, iSize);

                    //将小图插入到二维码图片中
                    ge.DrawImage(bi, (eSize - iSize) / 2, (eSize - iSize) / 2, iSize, iSize);
                }
                return be;
            }
        }