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.6 kB

11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. using ZXing.QrCode.Internal;
  2. using Shadowsocks.Controller;
  3. using Shadowsocks.Properties;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.ComponentModel;
  7. using System.Data;
  8. using System.Drawing;
  9. using System.IO;
  10. using System.IO.Compression;
  11. using System.Linq;
  12. using System.Text;
  13. using System.Windows.Forms;
  14. using Shadowsocks.Model;
  15. namespace Shadowsocks.View
  16. {
  17. public partial class QRCodeForm : Form
  18. {
  19. private string code;
  20. public QRCodeForm(string code)
  21. {
  22. this.code = code;
  23. InitializeComponent();
  24. this.Icon = Icon.FromHandle(Resources.ssw128.GetHicon());
  25. this.Text = I18N.GetString("QRCode");
  26. }
  27. private void GenQR(string ssconfig)
  28. {
  29. string qrText = ssconfig;
  30. QRCode code = ZXing.QrCode.Internal.Encoder.encode(qrText, ErrorCorrectionLevel.M);
  31. ByteMatrix m = code.Matrix;
  32. int blockSize = Math.Max(pictureBox1.Height/m.Height, 1);
  33. Bitmap drawArea = new Bitmap((m.Width*blockSize), (m.Height*blockSize));
  34. using (Graphics g = Graphics.FromImage(drawArea))
  35. {
  36. g.Clear(Color.White);
  37. using (Brush b = new SolidBrush(Color.Black))
  38. {
  39. for (int row = 0; row < m.Width; row++)
  40. {
  41. for (int col = 0; col < m.Height; col++)
  42. {
  43. if (m[row, col] != 0)
  44. {
  45. g.FillRectangle(b, blockSize*row, blockSize*col, blockSize, blockSize);
  46. }
  47. }
  48. }
  49. }
  50. }
  51. pictureBox1.Image = drawArea;
  52. }
  53. private void QRCodeForm_Load(object sender, EventArgs e)
  54. {
  55. var servers = Configuration.Load();
  56. var serverDatas = servers.configs.Select(
  57. server =>
  58. new KeyValuePair<string, string>(ShadowsocksController.GetQRCode(server), server.FriendlyName())
  59. ).ToList();
  60. listBox1.DataSource = serverDatas;
  61. var selectIndex = serverDatas.FindIndex(serverData => serverData.Key.StartsWith(code));
  62. if (selectIndex >= 0) listBox1.SetSelected(selectIndex, true);
  63. }
  64. private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
  65. {
  66. GenQR((sender as ListBox)?.SelectedValue.ToString());
  67. }
  68. }
  69. }