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.

ServerSharingViewModel.cs 3.4 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. using ReactiveUI;
  2. using ReactiveUI.Fody.Helpers;
  3. using Shadowsocks.Model;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Drawing;
  7. using System.IO;
  8. using System.Linq;
  9. using System.Reactive;
  10. using System.Windows;
  11. using System.Windows.Media.Imaging;
  12. namespace Shadowsocks.WPF.ViewModels
  13. {
  14. public class ServerSharingViewModel : ReactiveObject
  15. {
  16. /// <summary>
  17. /// The view model class for the server sharing user control.
  18. /// </summary>
  19. public ServerSharingViewModel()
  20. {
  21. _config = Program.MainController.GetCurrentConfiguration();
  22. Servers = _config.configs;
  23. SelectedServer = Servers[0];
  24. this.WhenAnyValue(x => x.SelectedServer)
  25. .Subscribe(_ => UpdateUrlAndImage());
  26. CopyLink = ReactiveCommand.Create(() => Clipboard.SetText(SelectedServerUrl));
  27. }
  28. private readonly Configuration _config;
  29. public ReactiveCommand<Unit, Unit> CopyLink { get; }
  30. [Reactive]
  31. public List<Server> Servers { get; private set; }
  32. [Reactive]
  33. public Server SelectedServer { get; set; }
  34. [Reactive]
  35. public string SelectedServerUrl { get; private set; }
  36. [Reactive]
  37. public BitmapImage SelectedServerUrlImage { get; private set; }
  38. /// <summary>
  39. /// Called when SelectedServer changed
  40. /// to update SelectedServerUrl and SelectedServerUrlImage
  41. /// </summary>
  42. private void UpdateUrlAndImage()
  43. {
  44. // update SelectedServerUrl
  45. SelectedServerUrl = SelectedServer.GetURL(_config.generateLegacyUrl);
  46. // generate QR code
  47. var qrCode = ZXing.QrCode.Internal.Encoder.encode(SelectedServerUrl, ZXing.QrCode.Internal.ErrorCorrectionLevel.L);
  48. var byteMatrix = qrCode.Matrix;
  49. // paint bitmap
  50. int blockSize = Math.Max(1024 / byteMatrix.Height, 1);
  51. Bitmap drawArea = new Bitmap((byteMatrix.Width * blockSize), (byteMatrix.Height * blockSize));
  52. using (var graphics = Graphics.FromImage(drawArea))
  53. {
  54. graphics.Clear(Color.White);
  55. using (var solidBrush = new SolidBrush(Color.Black))
  56. {
  57. for (int row = 0; row < byteMatrix.Width; row++)
  58. {
  59. for (int column = 0; column < byteMatrix.Height; column++)
  60. {
  61. if (byteMatrix[row, column] != 0)
  62. {
  63. graphics.FillRectangle(solidBrush, blockSize * row, blockSize * column, blockSize, blockSize);
  64. }
  65. }
  66. }
  67. }
  68. }
  69. // transform to BitmapImage for binding
  70. BitmapImage bitmapImage = new BitmapImage();
  71. using (MemoryStream memoryStream = new MemoryStream())
  72. {
  73. drawArea.Save(memoryStream, System.Drawing.Imaging.ImageFormat.Bmp);
  74. memoryStream.Position = 0;
  75. bitmapImage.BeginInit();
  76. bitmapImage.StreamSource = memoryStream;
  77. bitmapImage.CacheOption = BitmapCacheOption.OnLoad;
  78. bitmapImage.EndInit();
  79. }
  80. SelectedServerUrlImage = bitmapImage;
  81. }
  82. }
  83. }