Beispiel #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());
                }
            }
        }
Beispiel #2
0
        private int CalculateSuitableWidth(int width, int bitMatrixWidth)
        {
            FixedCodeSize isize = new FixedCodeSize(width, QuietZoneModule);
            DrawingSize dSize = isize.GetSize(bitMatrixWidth);
            int gap = dSize.CodeWidth - dSize.ModuleSize * (bitMatrixWidth + 2 * (int)QuietZoneModule);

            if (gap == 0)
                return width;
            else if (dSize.CodeWidth / gap < 4)
                return (dSize.ModuleSize + 1) * (bitMatrixWidth + 2 * (int)QuietZoneModule);
            else
                return dSize.ModuleSize * (bitMatrixWidth + 2 * (int)QuietZoneModule);
        }
        /// <summary>
        /// Renders a Quick Reponse code, that can be used with Google Authenticator or compatiple software.
        /// </summary>
        /// <param name="ident">ident of device as shown to user</param>
        /// <param name="key">private key</param>
        /// <param name="type">type of QR code, HOTP or TOTP</param>
        /// <param name="size">pixels height/width of png image</param>
        /// <returns>png image as byte array</returns>
        public static byte[] RenderQrCode(string ident, byte[] key, QrCodeType type, int size=200)
        {
            string url = "otpauth://" + type.ToString().ToLower() + "/" + ident + "?secret=" + Base32.Convert(key);

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

            ISizeCalculation isize = new FixedCodeSize(size, QuietZoneModules.Zero);
            GraphicsRenderer gRenderer = new GraphicsRenderer(isize);

            using (MemoryStream ms = new MemoryStream())
            {
                gRenderer.WriteToStream(qrCode.Matrix, ImageFormat.Png, ms);
                return ms.GetBuffer();
            }
        }