You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

QRCodeForm.cs 2.8 kB

10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. using QRCode4CS;
  2. using Shadowsocks.Properties;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.ComponentModel;
  6. using System.Data;
  7. using System.Drawing;
  8. using System.IO;
  9. using System.IO.Compression;
  10. using System.Text;
  11. using System.Windows.Forms;
  12. namespace Shadowsocks.View
  13. {
  14. public partial class QRCodeForm : Form
  15. {
  16. private string code;
  17. public QRCodeForm(string code)
  18. {
  19. this.code = code;
  20. InitializeComponent();
  21. }
  22. private void GenQR(string ssconfig)
  23. {
  24. string qrText = ssconfig;
  25. QRCode4CS.Options options = new QRCode4CS.Options();
  26. options.Text = qrText;
  27. QRCode4CS.QRCode qrCoded = null;
  28. bool success = false;
  29. foreach (var level in new QRErrorCorrectLevel[]{QRErrorCorrectLevel.H, QRErrorCorrectLevel.Q, QRErrorCorrectLevel.M, QRErrorCorrectLevel.L})
  30. {
  31. for (int i = 3; i < 10; i++)
  32. {
  33. try
  34. {
  35. options.TypeNumber = i;
  36. options.CorrectLevel = level;
  37. qrCoded = new QRCode4CS.QRCode(options);
  38. qrCoded.Make();
  39. success = true;
  40. break;
  41. }
  42. catch
  43. {
  44. qrCoded = null;
  45. continue;
  46. }
  47. }
  48. if (success)
  49. break;
  50. }
  51. if (qrCoded == null)
  52. {
  53. return;
  54. }
  55. int blockSize = Math.Max(200 / qrCoded.GetModuleCount(), 1);
  56. Bitmap drawArea = new Bitmap((qrCoded.GetModuleCount() * blockSize), (qrCoded.GetModuleCount() * blockSize));
  57. using (Graphics g = Graphics.FromImage(drawArea))
  58. {
  59. g.Clear(Color.White);
  60. using (Brush b = new SolidBrush(Color.Black))
  61. {
  62. for (int row = 0; row < qrCoded.GetModuleCount(); row++)
  63. {
  64. for (int col = 0; col < qrCoded.GetModuleCount(); col++)
  65. {
  66. if (qrCoded.IsDark(row, col))
  67. {
  68. g.FillRectangle(b, blockSize * row, blockSize * col, blockSize, blockSize);
  69. }
  70. }
  71. }
  72. }
  73. }
  74. pictureBox1.Image = drawArea;
  75. }
  76. private void QRCodeForm_Load(object sender, EventArgs e)
  77. {
  78. GenQR(code);
  79. }
  80. }
  81. }