Пример #1
0
        private void Draw()
        {
            int typeNumber;
            if (_text.Length < 26)
                typeNumber = 2;
            else if (_text.Length < 72)
                typeNumber = 5;
            else if (_text.Length < 125)
                typeNumber = 7;
            else if (_text.Length < 203)
                typeNumber = 10;
            else if (_text.Length < 298)
                typeNumber = 12;
            else if (_text.Length < 407)
                typeNumber = 15;
            else if (_text.Length < 534)
                typeNumber = 17;
            else if (_text.Length < 669)
                typeNumber = 20;
            else
            {
                Close();
                return;
            }

            QRCode qr;
            try
            {
                qr = new QRCode(typeNumber, QRErrorCorrectLevel.M);
                qr.AddData(_text);
                qr.Make();
            }
            catch (Exception)
            {
                Close();
                return;
            }

            var c = qr.GetModuleCount();
            var b = new Bitmap(c, c);

            for (var row = 0; row < c; row++)
            {
                for (var col = 0; col < c; col++)
                {
                    var isDark = qr.IsDark(row, col);
                    b.SetPixel(row, col, isDark ? _darkColour : _backColour);
                }
            }

            var newImage = new Bitmap(_size, _size, PixelFormat.Format24bppRgb);
            var g = Graphics.FromImage(newImage);
            g.InterpolationMode = InterpolationMode.NearestNeighbor;
            g.CompositingQuality = CompositingQuality.HighQuality;
            g.SmoothingMode = SmoothingMode.None;
            if (_border)
            {
                g.DrawImage(b, _borderWidth, _borderWidth, _size - _borderWidth * 2, _size - _borderWidth * 2);
                RectangleF[] r = {new RectangleF(0, 0, _size, _borderWidth), // Top
                    new RectangleF(0, 0, _borderWidth, _size), // Left
                    new RectangleF(_size - _borderWidth, 0, _borderWidth, _size), // Right
                    new RectangleF(0, _size - _borderWidth, _size, _borderWidth)}; // Bottom
                g.DrawRectangles(new Pen(_borderColour, _borderWidth), r);
            }
            else
                g.DrawImage(b, 0, 0, _size, _size);
            panel1.BackgroundImage = newImage;
        }