Beispiel #1
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);
        }
Beispiel #2
0
        private QRCodeParameters ParseQRParamsFromQueryString(IReadableStringCollection query)
        {
            QRCodeParameters param = new QRCodeParameters();

            var text = query["text"];
            param.Text = text;

            var size = query["size"];
            string format = query["format"];

            int intSize = 0;
            if (int.TryParse(size, out intSize)) param.Size = intSize;

            if (!string.IsNullOrWhiteSpace(format) && supportedImageFormats.ContainsKey(format.ToLower())) param.ImageFormat = supportedImageFormats[format];

            return param;
        }
Beispiel #3
0
        private void CreateQRCodeImage(HttpContext context, QRCodeParameters param)
        {
            Image img = null;
            if (!cache.TryGetValue(param.ToString(), out img))
            {
                logger.LogInformation("generating new image");
                img = generator.GenerateQRCode(param);
                cache.Set(param.ToString(), img, new MemoryCacheEntryOptions() {SlidingExpiration = DateTime.Now.AddDays(1).TimeOfDay });
            }
            else
            {
                logger.LogInformation("reading from cache");
            }

            context.Response.Headers.Add("content-type", "image/" + param.ImageFormat.ToString().ToLower());
            img.Save(context.Response.Body, param.ImageFormat);
        }