-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQrCodeView.xaml.cs
More file actions
101 lines (86 loc) · 2.67 KB
/
QrCodeView.xaml.cs
File metadata and controls
101 lines (86 loc) · 2.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Windows;
using System.Windows.Media.Imaging;
using System.Windows.Threading;
using Upsilon.Apps.Passkey.Core.Utils;
using Upsilon.Apps.Passkey.GUI.WPF.Themes;
using Upsilon.Apps.Passkey.GUI.WPF.ViewModels;
namespace Upsilon.Apps.Passkey.GUI.WPF.Views
{
/// <summary>
/// Interaction logic for QrCodeView.xaml
/// </summary>
public partial class QrCodeView : Window
{
private QrCodeView(string qrCode, int delay)
{
InitializeComponent();
Title = MainViewModel.AppTitle;
_qrCode_I.Source = _getBitmap(qrCode);
if (delay != 0)
{
DispatcherTimer timer = new()
{
Interval = new TimeSpan(0, 0, 0, 0, delay),
IsEnabled = true,
};
timer.Tick += _timer_Elapsed;
}
Loaded += (s, e) => DarkMode.SetDarkMode(this);
}
private void _timer_Elapsed(object? sender, EventArgs e)
{
(sender as DispatcherTimer)?.Stop();
DialogResult = true;
}
public static void ShowQrCode(Window owner, string qrCode, int delay)
{
if (!string.IsNullOrEmpty(qrCode))
{
_ = new QrCodeView(qrCode, delay)
{
Owner = owner
}
.ShowDialog();
}
}
public static void CopyToClipboard(string text)
{
Clipboard.SetText(text);
}
private static BitmapImage _getBitmap(string content)
{
int unit = 20;
bool[,] qrCode = QrCode.Generate(content);
int height = qrCode.GetLength(0);
int width = qrCode.GetLength(1);
Bitmap bitmap = new((height + 2) * unit, (width + 2) * unit);
using (Graphics g = Graphics.FromImage(bitmap))
{
g.FillRectangle(Brushes.White, 0, 0, (height + 2) * unit, (width + 2) * unit);
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
if (qrCode[i, j])
{
g.FillRectangle(Brushes.Black, (i + 1) * unit, (j + 1) * unit, unit, unit);
}
}
}
}
using MemoryStream memory = new();
bitmap.Save(memory, ImageFormat.Png);
memory.Position = 0;
BitmapImage bitmapImage = new();
bitmapImage.BeginInit();
bitmapImage.StreamSource = memory;
bitmapImage.CacheOption = BitmapCacheOption.OnLoad;
bitmapImage.EndInit();
bitmapImage.Freeze();
return bitmapImage;
}
}
}