Пример #1
0
        private void GenQR(string ssconfig)
        {
            string qrText = ssconfig;

            QRCode4CS.Options options = new QRCode4CS.Options();
            options.Text = qrText;
            QRCode4CS.QRCode qrCoded = null;
            bool             success = false;

            foreach (var level in new QRErrorCorrectLevel[] { QRErrorCorrectLevel.H, QRErrorCorrectLevel.Q, QRErrorCorrectLevel.M, QRErrorCorrectLevel.L })
            {
                for (int i = 3; i < 10; i++)
                {
                    try
                    {
                        options.TypeNumber   = i;
                        options.CorrectLevel = level;
                        qrCoded = new QRCode4CS.QRCode(options);
                        qrCoded.Make();
                        success = true;
                        break;
                    }
                    catch
                    {
                        qrCoded = null;
                        continue;
                    }
                }
                if (success)
                {
                    break;
                }
            }
            if (qrCoded == null)
            {
                return;
            }
            int    blockSize = Math.Max(200 / qrCoded.GetModuleCount(), 1);
            Bitmap drawArea  = new Bitmap((qrCoded.GetModuleCount() * blockSize), (qrCoded.GetModuleCount() * blockSize));

            using (Graphics g = Graphics.FromImage(drawArea))
            {
                g.Clear(Color.White);
                using (Brush b = new SolidBrush(Color.Black))
                {
                    for (int row = 0; row < qrCoded.GetModuleCount(); row++)
                    {
                        for (int col = 0; col < qrCoded.GetModuleCount(); col++)
                        {
                            if (qrCoded.IsDark(row, col))
                            {
                                g.FillRectangle(b, blockSize * row, blockSize * col, blockSize, blockSize);
                            }
                        }
                    }
                }
            }
            pictureBox1.Image = drawArea;
        }
Пример #2
0
 private void GenQR(string ssconfig)
 {
     string qrText = ssconfig;
     QRCode4CS.Options options = new QRCode4CS.Options();
     options.Text = qrText;
     QRCode4CS.QRCode qrCoded = null;
     bool success = false;
     foreach (var level in new QRErrorCorrectLevel[]{QRErrorCorrectLevel.H, QRErrorCorrectLevel.Q, QRErrorCorrectLevel.M, QRErrorCorrectLevel.L})
     {
         for (int i = 3; i < 10; i++)
         {
             try
             {
                 options.TypeNumber = i;
                 options.CorrectLevel = level;
                 qrCoded = new QRCode4CS.QRCode(options);
                 qrCoded.Make();
                 success = true;
                 break;
             }
             catch
             {
                 qrCoded = null;
                 continue;
             }
         }
         if (success)
             break;
     }
     if (qrCoded == null)
     {
         return;
     }
     int blockSize = Math.Max(200 / qrCoded.GetModuleCount(), 1);
     Bitmap drawArea = new Bitmap((qrCoded.GetModuleCount() * blockSize), (qrCoded.GetModuleCount() * blockSize));
     using (Graphics g = Graphics.FromImage(drawArea))
     {
         g.Clear(Color.White);
         using (Brush b = new SolidBrush(Color.Black))
         {
             for (int row = 0; row < qrCoded.GetModuleCount(); row++)
             {
                 for (int col = 0; col < qrCoded.GetModuleCount(); col++)
                 {
                     if (qrCoded.IsDark(row, col))
                     {
                         g.FillRectangle(b, blockSize * row, blockSize * col, blockSize, blockSize);
                     }
                 }
             }
         }
     }
     pictureBox1.Image = drawArea;
 }
Пример #3
0
    public System.Drawing.Image CreateQRCode(string inputString)
    {
        QRCode4CS.QRCode qrcode = new QRCode4CS.QRCode(new QRCode4CS.Options(inputString));
        qrcode.Make();
        System.Drawing.Image canvas = new Bitmap(86, 86);
        Graphics             artist = Graphics.FromImage(canvas);

        artist.Clear(Color.White);
        for (int row = 0; row < qrcode.GetModuleCount(); row++)
        {
            for (int col = 0; col < qrcode.GetModuleCount(); col++)
            {
                bool isDark = qrcode.IsDark(row, col);

                if (isDark == true)
                {
                    artist.FillRectangle(Brushes.Black, 2 * row + 10, 2 * col + 10, 2 * row + 15, 2 * col + 15);
                }
                else
                {
                    artist.FillRectangle(Brushes.White, 2 * row + 10, 2 * col + 10, 2 * row + 15, 2 * col + 15);
                }
            }
        }
        artist.FillRectangle(Brushes.White, 0, 76, 86, 86);
        artist.FillRectangle(Brushes.White, 76, 0, 86, 86);

        using (var ms = new System.IO.MemoryStream()) {
            canvas.Save(ms, ImageFormat.Png);
            Response.ClearContent();
            Response.Clear();
            Response.Buffer = true;
            ms.WriteTo(Response.OutputStream);
            Response.ContentType = "image/png";
            Response.Flush();
        }

        artist.Dispose();
        return(canvas);
    }