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 3.2 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
10 years ago
10 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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 and URL");
  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. var qrWidth = m.Width*blockSize;
  34. var qrHeight = m.Height*blockSize;
  35. var dWidth = pictureBox1.Width - qrWidth;
  36. var dHeight = pictureBox1.Height - qrHeight;
  37. var maxD = Math.Max(dWidth, dHeight);
  38. pictureBox1.SizeMode = maxD >= 7*blockSize ? PictureBoxSizeMode.Zoom : PictureBoxSizeMode.CenterImage;
  39. Bitmap drawArea = new Bitmap((m.Width*blockSize), (m.Height*blockSize));
  40. using (Graphics g = Graphics.FromImage(drawArea))
  41. {
  42. g.Clear(Color.White);
  43. using (Brush b = new SolidBrush(Color.Black))
  44. {
  45. for (int row = 0; row < m.Width; row++)
  46. {
  47. for (int col = 0; col < m.Height; col++)
  48. {
  49. if (m[row, col] != 0)
  50. {
  51. g.FillRectangle(b, blockSize*row, blockSize*col, blockSize, blockSize);
  52. }
  53. }
  54. }
  55. }
  56. }
  57. pictureBox1.Image = drawArea;
  58. }
  59. private void QRCodeForm_Load(object sender, EventArgs e)
  60. {
  61. var servers = Configuration.Load();
  62. var serverDatas = servers.configs.Select(
  63. server =>
  64. new KeyValuePair<string, string>(ShadowsocksController.GetServerURL(server), server.FriendlyName())
  65. ).ToList();
  66. listBox1.DataSource = serverDatas;
  67. var selectIndex = serverDatas.FindIndex(serverData => serverData.Key.StartsWith(code));
  68. if (selectIndex >= 0) listBox1.SetSelected(selectIndex, true);
  69. }
  70. private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
  71. {
  72. var url = (sender as ListBox)?.SelectedValue.ToString();
  73. GenQR(url);
  74. textBoxURL.Text = url;
  75. }
  76. private void textBoxURL_Click(object sender, EventArgs e)
  77. {
  78. textBoxURL.SelectAll();
  79. }
  80. }
  81. }