Esempio n. 1
0
        /// <summary>
        /// Gets the QR code containing the data of the network component.
        /// </summary>
        /// <returns>The QR code image.</returns>
        public Bitmap GetQRCode( )
        {
            QrEncoder qrEncoder = new QrEncoder( ErrorCorrectionLevel.H );
            byte[ ] ipBytes = IP.GetAddressBytes( );
            QrCode qrCode = qrEncoder.Encode( $"{string.Join( ",", ipBytes )},{Port}" );


            const int moduleSizeInPixels = 25;
            GraphicsRenderer renderer = new GraphicsRenderer( new FixedModuleSize( moduleSizeInPixels, QuietZoneModules.Two ), Brushes.Black, Brushes.White );

            DrawingSize dSize = renderer.SizeCalculator.GetSize( qrCode.Matrix.Width );

            Bitmap bmp = new Bitmap( dSize.CodeWidth, dSize.CodeWidth );
            using ( Graphics graphics = Graphics.FromImage( bmp ) )
                renderer.Draw( graphics, qrCode.Matrix );

            return bmp;
        }
Esempio n. 2
0
        public static void RunSample2()
        {
            const string helloWorld = "Hello World!";

            QrEncoder qrEncoder = new QrEncoder(ErrorCorrectionLevel.H);
            QrCode qrCode = qrEncoder.Encode(helloWorld);

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

            Panel panel = new Panel();
            Point padding = new Point(10, 16);
            DrawingSize dSize = renderer.SizeCalculator.GetSize(qrCode.Matrix.Width);
            panel.AutoSize = false;
            panel.Size = new Size(dSize.CodeWidth, dSize.CodeWidth) + new Size(2*padding.X, 2*padding.Y);

            using (Graphics graphics = panel.CreateGraphics())
            {
                renderer.Draw(graphics, qrCode.Matrix, padding);
            }

        }
Esempio n. 3
0
        Bitmap ComputeQRCode(int slice)
        {
            var qrEncoder = new QrEncoder(FErrorCorrectionLevel[slice]);
            var qrCode = new QrCode();
            if (qrEncoder.TryEncode(FText[slice], out qrCode))
            {
                var fc = FForeColor[slice];
                var bc = FBackColor[slice];
                fc = new RGBAColor(fc.B, fc.G, fc.R, fc.A);
                bc = new RGBAColor(bc.B, bc.G, bc.R, bc.A);
                using (var fore = new SolidBrush(fc.Color))
                using (var back = new SolidBrush(bc.Color))
                {
                    var renderer = new GraphicsRenderer(new FixedModuleSize(FPixelSize[slice], FQuietZoneModules[slice]), fore, back);
                    DrawingSize dSize = renderer.SizeCalculator.GetSize(qrCode.Matrix.Width);
                    var bmp = new Bitmap(dSize.CodeWidth, dSize.CodeWidth);
                    using (var g = Graphics.FromImage(bmp))
                        renderer.Draw(g, qrCode.Matrix);

                    return bmp;
                }
            }
            else
                return null;
        }
Esempio n. 4
0
        /// <summary>
        /// 根据文字内容等信息构建相应QRCode格式二维码图片
        /// </summary>
        /// <param name="content">二维码内容信息</param>
        /// <param name="level">纠错级别</param>
        /// <param name="width">图片宽度,如小于0则使用默认尺寸</param>
        /// <param name="height">图片高度,如小于0则使用默认尺寸</param>
        /// <param name="inline">内嵌图片,如为Null则不做内嵌</param>
        /// <returns>二维码图片</returns>
        public static Image BuildQRCode(String content, ErrorCorrectionLevel level, int width, int height, Image inline)
        {
            QrEncoder encoder = new QrEncoder(level);
            QrCode code = null;
            if (!encoder.TryEncode(content, out code))
                return null;

            GraphicsRenderer render = new GraphicsRenderer(new FixedModuleSize(ModuleSizeInPixels, QuietZoneModules.Two), Brushes.Black, Brushes.White);
            DrawingSize cSize = render.SizeCalculator.GetSize(code.Matrix.Width);
            Size oSize = new Size(cSize.CodeWidth, cSize.CodeWidth) + new Size(2 * PaddingSizeInPixels, 2 * PaddingSizeInPixels);

            Image img = new Bitmap(oSize.Width, oSize.Height);

            using (Graphics g = Graphics.FromImage(img))
            {
                g.CompositingQuality = CompositingQuality.HighQuality;
                g.InterpolationMode = InterpolationMode.HighQualityBicubic;

                render.Draw(g, code.Matrix);
                if (inline != null)
                {
                    int iw = (int)(oSize.Width * InlineSizeInProportion);
                    int ih = (int)(oSize.Height * InlineSizeInProportion);
                    int il = (oSize.Width - iw) / 2;
                    int it = (oSize.Height - ih) / 2;
                    g.DrawImage(inline, it, il, iw, ih);
                    Pen pen = new Pen(Color.White, 1);
                    using (GraphicsPath path = CreateRoundedRectanglePath(new Rectangle(it - 1, il - 1, iw + 1, ih + 1), 4))
                    {
                        g.DrawPath(pen, path);
                    }

                }
            }

            if (width > 0 && height > 0)
            {
                int w = width > 0 ? width : code.Matrix.Width;
                int h = height > 0 ? height : code.Matrix.Height;
                Image imgCode = new Bitmap(width, height);
                using (Graphics g = Graphics.FromImage(imgCode))
                {
                    g.CompositingQuality = CompositingQuality.HighQuality;
                    g.InterpolationMode = InterpolationMode.HighQualityBicubic;
                    g.DrawImage(img, 0, 0, width, height);
                }

                img.Dispose();
                img = imgCode;

            }

            return img;
        }